My ongoing experiences with Ubuntu, and later Mythbuntu, as a media center with MythTV. I'm also using the system for a virtual machine server, a mediawiki server and a general all around home infrastructure base.

Saturday, February 7, 2009

Installing Apache and PHP on my Ubuntu laptop and configuring a virtual host

I wanted to do some web development, so here's the process I used to install apache w/php and configure a virtual host on my Ubuntu laptop.

First, I followed these old directions as a starting point for installing Apache and php. But I found that the extra repositories were already added, Apache was already installed and after installing php5, libapach2-mod-php5 was already install, so all I really had to was:

# apt-get install php5
I test php functionality by creating a quick test file:
# vi /var/www/test.php
# cat /var/www/test.php
And pointing my browser at http://localhost/test.php and seeing "Hello World".

On thing I noticed is that I was getting the following error when restarting:
# apache2ctl restart
apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
I fixed this by adding a ServerName directive to the Apache configuration (I actually don't think it matters what name you give):
# vi /etc/apache2/apache2.conf
# grep ServerName /etc/apache2/apache2.conf
ServerName localhost
Now, to install the virtual host so that I can test using code in my home directory. There are a number of sites out there if you google "apache vhost" and I didn't find any particularly good, so here is what I did.

For the sake of these directions, I'm going to use the name vhost.example.com and I'm going to put the name into /etc/hosts. This will allow me to see things exactly as they will appear on the live web site.

First I edit /etc/hosts and add an entry for the vhost:
# vi /etc/hosts
# tail -1 /etc/hosts
127.0.0.1 vhost.example.com
Now it's time to edit the apache configuration:

# vi /etc/apache2/sites-available/myvhost.conf
# cat /etc/apache2/sites-available/myvhost.conf
NameVirtualHost vhost.example.com

<VirtualHost vhost.example.com>
DocumentRoot "/path/to/vhost/directory"
</VirtualHost>

<Directory /path/to/vhost/directory>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Depending on what you need to do, you may not need the block. I have some local php configuration, so I did.

Now enable the vhost:

# a2ensite myvhost.conf
Enabling site myvhost.conf.
Run '/etc/init.d/apache2 reload' to activate new configuration!# apache2ctl restart
One last snag, probably particular to how I handled my php, but I had the following .htaccess file in my vhost root:
AddHandler php-html .html
Action php-html /php/process.php
Which caused the following error whenever I tried to access the directory:
Invalid command 'Action', perhaps misspelled or defined by a module not included in the server configuration
To fix this, I enabled the actions module:
# a2enmod actions
Enabling module actions.
Run '/etc/init.d/apache2 restart' to activate new configuration!
Now, just restart Apache:
# apache2ctl restart
And point my browser at http://vhost.example.com and everything worked.

Update 2/21/09: If you want to be able to run 'php' from the command-line, you should also install the php5-cli package (kudos):

# apt-get install php5-cli

No comments: