While it is super easy to create your own server, it’s a pain to look after it. For all your PHP hosting needs, I will always recommend fortrabbit – it’s cheaper and easier. But I’ll leave this post up for posterity.
It’s remarkably easy to build your own web server, it’s just a case of knowing how. (Like everything?) Maybe you want a server home to build on, or maybe you’re trying out a cloud server – either way, this article will help you setup a LAMP (Linux, Apache, MySQL, PHP) server, then introduce you to virtual hosts.
First up, you need a server. Rackspace offer cloud servers from 1.5¢ or 1p per hour. Or you can set one up yourself on a box at home. This tutorial assumes you’re using Ubuntu. I’ve chosen Ubuntu because of the great community behind it. Download Ubuntu Server for yourself if you haven’t already, and install it.
Connecting to your server
We’re going to be working in the command line. The commands I’m using assume you’re using Linux or Mac OS X. They may differ slightly if you’re using Windows.
If you’re working on a box in your home you can just plug a keyboard and monitor in, but it’s probably going to be easier to SSH in.
Open Terminal from the Utilities folder on Linux or OS X, or open the Command Prompt on Windows by opening Start, Run, type
cmd and press Enter.
Either use the IP address of your server, or use your local box’s IP.
$ ssh firstname.lastname@example.org
I’m logging in as root in this example, but you might not have a root user. It’s often better not to have a root login. Instead, login in using the administrator account you created and prepend everything with
sudo, apart from where noted.
If this is the first time you’ve connected via SSH to your server, you’ll receive a warning similar to this
The authenticity of host '123.456.78.90 (123.456.78.90)' can't be established.
RSA key fingerprint is 1a:22:33:4b:cc:5d:6e:f7:89:gg:0h:12:3h:45:67:ij.
Are you sure you want to continue connecting (yes/no)?
yes and press Enter.
The server will now ask for your password. Nothing’ll appear on the screen as you type the password out. Backspace does still work, but you’ll have to use your head to work out what you’re doing.
If you make a mistake use Control+C to cancel out of the current action.
Apache, MySQL and PHP are arguably the most popular open-source web server tools around. They work well together and there’s a lot of support. We’re going to install each one now, and their dependancies.
Your command prompt should look something similar to this
My server is called bubbles, your server is probably something more sensible/boring.
From here on, if I start a command with
#, that’s because I’m referring to this point, the command prompt.
Let’s start with Apache.
Just incase you’re not familiar, Apache is the HTTP server. It takes a request from the internet, finds the file, and returns the page to the user. It’s often referred to as
At the command prompt, type the following (this is where you’ll need to prepending things with
sudo if you’re not logged in as root).
# apt-get install apache2
Then hit Enter. The following code will quickly appear on your screen.
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common libapr1
libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap perl perl-modules
www-browser apache2-doc apache2-suexec apache2-suexec-custom ufw perl-doc
libterm-readline-gnu-perl libterm-readline-perl-perl make openssl-blacklist
The following NEW packages will be installed:
apache2 apache2-mpm-worker apache2-utils apache2.2-bin apache2.2-common
libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap perl
0 upgraded, 12 newly installed, 0 to remove and 0 not upgraded.
Need to get 11.2 MB of archives.
After this operation, 47.2 MB of additional disk space will be used.
Do you want to continue [Y/n]?
If it doesn’t, you’ll need to troubleshoot and find out what’s causing the problem.
If you haven’t installed anything using the command line before, Ubunutu makes use of a very useful service called the Advanced Packaging Tool, which is what you just saw at work. You type in
apt-get install, then the name of the package, and the tool will do the rest.
It’s asking you a question, after letting you know how much space it needs. Type in
Y, then hit Enter.
Apache will now be installed on your server, and many lines of code will appear. Some look good, and some look bad. Not to worry because we’re only interested in the last bit.
* Starting web server apache2
apache2: Could not reliably determine the server's fully qualified domain
name, using 123.456.78.90 for ServerName [ OK ]
Setting up apache2 (2.2.17-1ubuntu1) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
So long as the response is something similar to that, that’s just fine for now. If it says something like “failed to start” on the last four lines, you’ll need to troubleshoot from here.
Hopefully you’re still with me, in which case time to move onto PHP.
This is probably the reason you’re here, but just incase you’re not sure, PHP is a server-side scripting language mostly designed for web development. It’ll let you make your web pages dynamic.
To install is as almost as easy as Apache.
# apt-get install php5 libapache2-mod-php5
The only difference is there’s two packages in a row here, one called
php5, the other
libapache2-mod-php5. The former is relatively obvious, the latter is a library for PHP and Apache to talk to each other.
Again, it’ll ask if you want to continue once it’s explained how much space it’ll take up.
At this point you may get an error. It’s normal, so don’t panic.
Failed to fetch http://...
404 Not Found [IP: 12.34.567.890 80]
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
This simply means that your copy of what the URLs are is out of sync with where they actually are. We’re going to go ahead with the suggestion to get that sorted.
# apt-get update
Remember to prepend it with
sudo if you’re not logged in as root.
Once the list has updated, you can try again. (SuperSecretShortcut™: press the Up arrow key to cycle through recently used commands.)
About a dozen lines from from the bottom, after it’s finished installing, you should see something similar to this.
Creating config file /etc/php5/apache2/php.ini with new version
* Reloading web server config apache2
In which case it’s installed fine, and helpfully told you where your
php.ini file is. If not, you’ll need to troubleshoot.
Most web applications make use of a database, and MySQL is normally a popular choice. It too plays well with Apache, and also PHP. This’ll require a little more effort on your part, but should be quite straight forward.
# apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql
This time you’ll see something a bit different half-way through.
For now, leave the password blank and press Enter. You may need to do this three times.
Setting a blank password for the root MySQL use is for development and ease while you setup the server. It is highly recommended to use a password on a live production server. I’ll go through to set that up later.
Let’s check we’ve got everything.
Well I’d call that a LAMP server if I’d ever seen one. Let’s test it out to make sure it’s working.
Open up your favourite web browser and type in the IP address into the address bar. For example, you might be working locally on
192.168.1.56 or on a server elsewhere
80.123.456.789. Hit Enter.
Here’s a screenshot of what you should see.
If you see something other than that, you’ve either got the wrong IP address or something has gone wrong.
One troubleshooting tip is to try and restart Apache.
# /etc/init.d/apache2 restart
Remember to include
sudo if necessary. This should restart Apache and tell you if there are any problems.
We’re going to load the PHP function
phpinfo() to have a look around and see if PHP is working.
At the command prompt, navigate to
/var/www/. You don’t need to use
sudo for this.
# cd /var/www/
ls and hit Enter to see what’s in the directory, no need for
It’s probably just the one file for now, which is the one we saw earlier. Let’s work with it. First, rename it to
# mv index.html index.php
Prepend it with
sudo if you’re not root.
Now we’re going to edit
index.php using Nano, a really easy-to-use command line editor.
# nano index.php
There’s some familiar HTML! The version of Nano I’m using has highlighted a few words and tags, don’t worry if yours is different.
Let’s put a new line at the top. The cursor should already be at the top, like on the screenshot above, so just hit Enter and type this onto the first line.
<?php phpinfo(); ?>
To save the file in Nano (called Writing Out), use Control+O, show by the symbol
^O. A white bar will appear near the bottom.
File Name to Write: index.php
Hit Enter and Nano will save the file. You can use that opportunity to save it as a different file name.
Go back to your web browser and refresh the page you had earlier. You’ll now hopefully see a familiar sight.
If you can see the PHP information function at work, you’ve done good. If not, here’s what you might see.
- Still seeing the same screenshot as before? Check you’ve named it
- If you see the words
<?php phpinfo()displayed in the browser, PHP hasn’t be installed properly.
If you’ve made it through, switch back to Nano and remove the line you added starting with
Never leave a file containing
phpinfo() unattended. You never know who might see it. Soon as you’re done, remove it, even if nobody else knows the IP address or URL.
Use Control+O to save it again. Keep the HTML that was there before, it will be useful in the future to make sure that everything’s still working. You can come back and jazz it up later if you wish.
To exit Nano, hit Control+X, you’ll be back at the command prompt.
You can use the
www directory to share public files.
Setup for Development or Production
A development server is one that that you use internally to test applications on. It never stores sensitive data, and is often only available on a local network. A production server has live data on it and anybody can see it.
In order to get to one of those standards, there’s a few things you must do first.
Fully qualified domain name
When we were setting up Apache, you might have seen an error along the lines of
Could not determine the server's fully qualified domain name. We’re going to correct this error now.
# nano /etc/apache2/conf.d/fqdn
I’m using an absolute address so it doesn’t matter which folder you’re in when you type this in. Remember to use
sudo if you’re into that now.
You’ll probably get a blank file, with instruction
[ New File ] at the bottom. That’s okay, it wasn’t there before. Type the following line.
Use Control+O then Enter to save, Control+X to exit back to the command prompt.
Restart Apache to find out if it’s worked.
# /etc/init.d/apache2 restart
You should now get no errors, just a friendly
[ OK ].
Another thing you’ll want to do is get PHP configured.
# nano /etc/php5/apache2/php.ini
php.ini is a big file, you might want to use Control+V to jump one screen at a time, and Control+Y to back up again.
Most of the information you need to know is helpfully written in comments at the beginning and throughout the file.
After a few screens from the top you’ll find a section labeled
; Quick Reference ;
This shows you different settings recommended for development and production. For example, if you want PHP to print errors.
; Default Value: On
; Development Value: On
; Production Value: Off
By default this feature is on, but it’s recommended you turn it off for production. Remember that PHP errors can potentially show up security flaws in your applications, this is why it’s best to turn it off.
Read through the recommendations and decide what you want for your server. If in doubt, use the production value.
After the reference comes the file values. Scroll through until you find one you want to change. As before, use Control+O to save, Control+X to exit.
I’m going to assume you’re using the server for testing and development, otherwise you’ll want to seriously look into PHP hardening to make sure your server is secure.
You can use the
/var/www directory for one site, but it’ll make it awkward if you have more than one.
(It is worth mentioning if you’re only going to use your server for one site, you can just the default setup using
/var/www as the document root. In which case, edit your site information.)
# nano /etc/apache2/sites-available/default
Apache has a system called Virtual Hosts, that allows you to have multiple domains pointing at your server, which are then kept in separate directories around the server. You can then give them unique permissions and control.
Ubuntu is part of a family of Linux distributions that make virtual hosts easy. Create your new virtual host like so.
1. Create the Virtual Host file
I’m going to create a site called purple.com. Keep the virtual host file short and sweet, such as
# nano /etc/apache2/sites-available/purple
You should get the notice
[ New File ] at the bottom.
2. Add server details
You can find out all the options available to you in the Apache documentation, but for development, this is sufficient.
ServerAlias *.purple.com purple.co.uk *.purple.co.uk
It’s easiest to copy and paste this into a text editor
ServerAdminemail address of the system admin (you)
ServerNamemain domain, I’d recommend keeping it as simple as possible
ServerAliasextra domains, such as
*.for a catch-all – this is useful for separating out sub-domains
DocumentRootthe root directory for the domain
LogLevelsee LogLevel Directive, but
warnis normally pretty good or
noticefor a little more detail
ErrorLogis the location where the error log is saved
Save Control+O and exit Control+X.
3. Create the directories
Create each of the directories that we referred to in the virtual host file.
# mkdir /var/purple
# mkdir /var/purple/www
# mkdir /var/purple/logs
4. Activate the site
This is the clever bit that Ubuntu does for you.
# a2ensite purple
This’ll do something very clever in the background and your site is now activated. If you need to disable it quickly, simply run
a2dissite purple. The advantage is that you won’t loose the file you made, you just turn it ‘on and off’.
5. Restart Apache
As we’ve made a change to the configuration, restart Apache.
# /etc/init.d/apache2 restart
And repeat as necessary
For each site you’d like to make, go back to 1 and create a new file. You can then enable and disable them as you please. You don’t have to follow the directory structure I’ve set, but anything you put in
/var/www will be viewable when you visit the IP.
Deleting a site
Work backwards like so
# a2dissite purple
# rm -rf /var/purple
# rm -rf /etc/apache2/sites-available/purple
Then restart Apache to make sure there’s no problems.
# /etc/init.d/apache2 restart
Be careful using
rm -rf, it will delete the directory without a warning.
Virtual Hosts with user accounts
Using the method above, you’ll still login as root to access all files. It might be preferable to create separate accounts, with different usernames and passwords, which restrict access.
I’m writing this part of the tutorial, so send me Tweet if you’d like to know when I’ve written more.