RHEL6 apache httpd virtual host the proper way

My recipie for name based virtual hosts in separate directories on RHEL:

We place all the virtual hosts under a new directory tree /var/www/vhosts:

# yum install httpd
# mkdir /var/www/vhosts
# semanage fcontext -a -t httpd_sys_content_t /var/www/vhosts
# restorecon -Rv /var/www/vhosts
# mkdir -p /var/www/vhosts/{site1,site2,site3}/{logs,htdocs}
# chown -R apache:apache /var/www/vhosts

I recommend using the FQDN of each site instead of the words “site1”, “site2”, in these examples.

Create the file /etc/httpd/conf.d/vhosts.conf with appropriate content such as:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName site1
  DocumentRoot /var/www/vhosts/site1/htdocs
  CustomLog "/var/www/vhosts/site1/logs/access.log" common
  ErrorLog "/var/www/vhosts/site1/logs/error.log"

  <Directory "/var/www/vhosts/site1/htdocs">
     Options None
     AllowOverride All
     Order Deny,Allow
     Allow from 127.0.0.1
  </Directory>
</VirtualHost>

<VirtualHost *:80>
  ServerName site2
  DocumentRoot /var/www/vhosts/site2/htdocs
  CustomLog "/var/www/vhosts/site2/logs/access.log" common
  ErrorLog "/var/www/vhosts/site2/logs/error.log"

  <Directory "/var/www/vhosts/site2/htdocs">
     Options None
     AllowOverride All
     Order Deny,Allow
     Allow from 127.0.0.1
  </Directory>
</VirtualHost>

and so on

(Dont forget to set the Directory permissions properly. Above is just an example!)

Then activate the goodness:

# apachectl restart

Why is this method good?

1. Creating the vhosts.conf in conf.d doesn’t modify any vendor-supplied files, which means that we won’t lose them if we reinstall the package.

2. Keeping each virtual host and its logs under its own directory tree makes maintenance a breeze and permissions can be separated to give developers access to specific vhosts only.

Howto enable Permalinks in WordPress on LAMP

In my last post about setting up WordPress on a LAMP, I omitted how to successfully enable permalinks, something you of course want to have on your blog.

Permalinks in WordPress is a way to have pretty URL:s to your posts to make it easy to link directly to them, and give them a human-readable format.

For it to work in a LAMP environment, you need to have mod_rewrite enabled in apache, and the tricky part when it comes to WordPress, is to enable mod_rewrite on the directory where your blog resides.

Most linux distros default to rather sane settings, and typically have something like this in them:

    <Directory "/var/www/html">
        Options FollowSymLinks MultiViews
        AllowOverride None
        Order allow,deny
        Allow from all
    </Directory>

that means (if /var/www/html is your DocumentRoot), that WordPress’ way of using .htaccess to control the rewriting with mod_rewrite, will not work as intended (due to the AllowOverride None directive).

The fix is easy. Just add something like this to your apache config:

<Directory "/var/www/html/wordpress">
    AllowOverride All
</Directory>

And you’ll be all set! (replace /var/www/html/wordpress above with the directory where you have wordpress installed. The same directory where wordpress created the .htaccess file when you enabled permalinks)

Step-by-step guide to set up wordpress on an existing LAMP system

0. If you get permission denied on the shell commands, try prefixing them with “sudo ”

1. Log in as root in mysql:

lamp$ mysql -u root -p

2. Create a database and MySQL user dedicated to wordpress:

mysql> CREATE DATABASE blogdb CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE USER 'blogger'@'localhost' IDENTIFIED BY 'b1ogpw';
Query OK, 0 rows affected (0.05 sec)

mysql> GRANT ALL PRIVILEGES ON blogdb.* TO 'blogger'@'localhost';
Query OK, 0 rows affected (0.63 sec)

3. download and unpack wordpress into a directory served by apache:

lamp$ cd /var/www/html
lamp$ wget http://wordpress.org/latest.tar.gz
lamp$ tar zxf latest.tar.gz
lamp$ rm latest.tar.gz
lamp$ cd wordpress

4. configure wordpress with the database details and secret keys (edit all occurrences of the word “here”). Use the online generator to get good values for the secret keys:

lamp$ vi wp-config-sample.php
lamp$ mv wp-config-sample.php wp-config.php

5. run the wordpress installation script from a web browser. The URL will be like this:

http://your-lamp/wordpress/wp-admin/install.php

6. Add the name for your blog, a user name (admin), password, your email address, and click the “Install WordPress” button att the bottom.

7. Done! Now log in, delete the sample post and sample page, and start customising your wordpress site.

Good luck!