Want the instant gratification solution? Scroll down to “All In One” below.
Several of the Linux (Debian Squeeze) / Apache2 / MySQL / PHP (LAMP) configs I’ve seen on the ‘net include complexity you just don’t need (like suEXEC or suPHP). If you’re setting up a basic server (physical, VPS, Xen, OpenVZ, whatever) and you’re going to be the only person running it, then setting up a LAMP environment is trivial.
This post assumes your server boots and you can log in and that it’s a basic Squeeze install with none of the relevant software pre-installed. I hate sudo, so I’m going to assume you have either su’d to root (su – root) or that you’ve switched to root via sudo (sudo su – root). If not, you’ll need to prefix all of these commands with sudo (after setting sudo up).
Although this little guide is for Debian, I think it’ll work unchanged on Ubuntu as well. This guide also assumes that your server hosts a single website, so nothing fancy with any Apache2 VirtualHosts.
All of the follow steps can be combined, but I like to install by stages so that I can test each element on it’s own, and not get overwhelmed.
One of the big mistakes people new to Debian or Ubuntu do, is they think they have to manually edit config files to get things working. The Debian packagers spend a lot of time making Debian packages work properly with each other with the absolute minimum manual effort.
While Apache2 might be a bloated-warthog in the eyes of some system administrators, it’s ubiquitous, reliable, feature rich, well documented and well supported. You can play with lighttpd, nginx and other options later, but this is LAMP, so let’s install the A.
The default Apache2 install on Debian will try and use the Worker MPM for Apache2. We want the Prefork MPM when we use PHP5 later, so we’ll specify that straight away.
apt-get install apache2 apache2-mpm-prefork
And that’s it. Assuming that runs successfully (errors are outside the scope of this walkthrough), you should be able to connect to the web server of your server on port 80 (iptables is also outside the scope of this, but a blank Debian install won’t have any firewalling in the way anyway).
If the install worked, connecting should return a page which says “It works!” and some information about this being the default web page.
Again, PHP gets bad press these days and maybe rightly so, but again it’s well used, well supported and well understood. It’s probably easier to install it along with Apache2 in reality, because Debian does all the post config work at once then, but if you do it later, you are at least in control of the various stages.
We need to install two things, PHP5 and the Apache2 PHP module (on Debian, this package is libapache2-mod-php).
apt-get install php5 libapache2-mod-php5
and then restart Apache2 (I use the following command, any method that reloads the Apache2 config file works)
That’s it. You don’t need to edit anything, enable anything, configure anything. Debian handles all that for you. The default install of Apache2 creates a /var/www directory which it uses as the root for your web site. If you go to that directory and create a file called test.php, and put this into it,
<?php phpinfo(); ?>
You can then test the PHP5 install has worked by connecting to your web server and requesting the file test.php. If it asks you to save it, something went wrong (or you didn’t restart Apache2), otherwise you should get a full list of the PHP settings.
NB: Debian installs Suhosin by default. This should be okay, but it can cause issues with some features of WordPress and phpMyAdmin.
The MySQL install is the most complex, because you’ll have to create a password! Otherwise, it’s as simple as the above steps. As well as MySQL we need to remember to install the PHP5 MySQL module, otherwise we won’t be able to interact with the database.
apt-get install mysql-server php5-mysql
After some packages are downloaded and installed, you’ll be asked to set a password for the MySQL root user. The dialog says this is not mandatory. I think it should be. You should absolutely set this password. It does not need to match the Linux root user password, and as in all cases, it should actually differ significantly.
Restart Apache2 for good measure, and you’re done. MySQL will be started, the relevant libraries are installed, and Apache2 / PHP5 / MySQL can all communicate as required.
By default, MySQL won’t be listening on any external interfaces, which is a good thing, so only your website can communicate with it. Some guides recommend installing phpMyAdmin at this point, and you can if you want, although I prefer not to.
In a default Debian install, Apache2 and PHP5 run as the www-data user. By default, the permissions on /var/www are
drwxr-xr-x root root
That means the web server can’t create any files or directories in /var/www. That’s a problem when installing things like WordPress which want to create their own config files or .htaccess files. Because we’re not worried about multiple users on our server, or different customers, it’s safe to set the owner of /var/www to www-data:www-data, and do the same for all files in that directory. This advice is only true for a server where you don’t mind all the websites running as the same user, but that’s the point of this example anyway!
The following command will install the whole thing in one go, and all you need to do is set the MySQL password.
apt-get install apache2 apache2-mpm-prefork php5 libapache2-mod-php5 mysql-server php5-mysql
Follow Up Steps
Later on you might want to install additional PHP modules (such as php5-curl, php5-gd, php5-mcrypt), and for sites which you expect to be busy on servers with not much memory you might want to look at using apache2-mpm-worker and FastCGI.