In part 1 of this series we covered how to install and upgrade the Ubuntu server edition. Today we will cover how to set up Apache, PHP (using SuPHP) and MySQL. Apache is one of the most popular http servers in the world. It is highly customizable, scalable, and it is easy to use. Like Apache, MySQL is incredibly popular and is used by numerous websites as the backend database. PHP, on the other hand, is a scripting language used to develop rich, dynamic, web applications. The reason I chose these three packages is simple – most Linux web hosts out there use them.
This guide is simply meant to be a crash course to get you comfortable with using Ubuntu Server for testing and developing your web apps – whether you use the traditional LAMP setup or LightHTTP, PostgreSQL, Ruby, Perl, C++, etc.
The Apache installation is pretty straightforward. From the command prompt type the following:
sudo apt-get install apache2
Hit enter, then type in your password. I would then highly recommend enabling mod_rewrite – since quite a few web applications rely on it:
sudo a2enmod rewrite
Whenever you enable/disable modules or change the Apache configuration you will have to restart the web sever:
sudo /etc/init.d/apache2 restart
And that’s it for the Apache install. To test it out all you need to do is get the server’s IP address and load it up on your favorite web browser.
By default the files are stored in /var/www
PHP (via SuPHP)
For this guide I will cover the installation of SuPHP instead of the traditional apache module or php-cgi. In a nutshell, SuPHP will let Apache run your PHP scripts as the user who owns the script. This is good for a few reasons – it keeps your system secure by not letting poorly written scripts erase or modify other users’ files, and it makes dealing with file permissions a lot easier (if you allow file uploads, cache pages, etc). This is also one way shared hosts keep users’ scripts from messing up the entire server.
To set up SuPHP do the following:
sudo apt-get install libapache2-mod-suphp
This will automatically install suphp, php5, and restart Apache. Just like the Apache install, there is one extra package I would install since a lot of scripts rely on it. GD is used by many scripts for image manipulation, and is a must-have for virtually any PHP install.
sudo apt-get install php5-gd
And last but not least, MySQL:
sudo apt-get install php5-mysql
That will install both the php and client libraries for MySQL. Most likely you will be running MySQL on the same box, and that can be installed via this command:
sudo apt-get install mysql-server
When prompted for the root database password pick something secure and don’t forget it!
Virtual Hosts & Testing
So far we have set up Apache, PHP, and MySQL. The only thing left is to set up virtual hosts for your server. For now we’ll do a simple ip & port based host, instead of a domain:
Do this to make a public_html folder in your home directory:
Then to add the virtual host:
sudo nano /etc/apache2/sites-enabled/mysite.conf
Just remember to change “your_login_name” to your login name for ubuntu. To save the file hit control + o and then enter, then control + x to exit nano. The first line tells Apache to listen on port 8080, the second line tells it to define a virtual host for 8080. The next line just tells Apache where to find the files for said virtual host.
All that is left is to issue the following command to restart Apache:
sudo /etc/init.d/apache2 restart
And create a file in your public_html directory:
echo "<?php phpinfo();?>" > ~/public_html/index.php
Then load up the server’s ip in your web browser followed by :8080 and the php info page should show up if it all went well!
Just for kicks, one last thing I do is make sure SuPHP is working correctly:
echo "<?php echo system('/usr/bin/whoami'); ?>" > ~/public_html/whoami.php
When you load whoami.php in your browser it should pop up with your Ubuntu login name:
And that’s it for now. As always, if you have any questions feel free to ask away! Since this is only a crash-course there are some topics I will expand upon at a later date.