Blog

How To Set Up An Ubuntu Development Server (Part 2, LAMP)

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.

Apache

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.

ifconfig

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

MySQL

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:

mkdir ~/public_html

Then to add the virtual host:

sudo nano /etc/apache2/sites-enabled/mysite.conf
Listen 8080
<VirtualHost *:8080>
DocumentRoot /home/your_login_name/public_html
</VirtualHost>

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.

Other posts from How To Set Up An Ubuntu Development Server

Part 1 – Setting Up Ubuntu
Part 3 – Postgresql, SSH, BIND, and Name-Based Apache Hosts

Comments
  1. Isa - vacances thalasso tunisie
    March 8, 2011 at 8:14 am

    I now have doubts about the mySQL database ever since oracle took over it. Maybe you could include postgres instructions in a next post?

  2. Jeremy Steele
    March 8, 2011 at 11:08 am

    Yep, I can do that.

    Thanks for reading :)

  3. Lee H
    March 20, 2011 at 12:09 pm

    Thanks for the write up. I already have a server running on Ubuntu desktop, but will be turning a older laptop into a development server. Since it has low specs I will be avoiding the bloat and going right for the server addition. I’ve been reading your two articles while downloading. So now I know what to expect during install time.
    Have you tried the “Host Admin” Firefox plugin? You can set up your Apache virtual acct for your registered domain. Then when you activate the plugin your browser will point to your development box as if it was your real live domain. Pretty slick stuff.

  4. Jeremy Steele
    March 21, 2011 at 1:45 am

    Very nice. I was using a mix of virtual machines on my workstation and an old iBook for testing code but recently built a little intel atom server with ubuntu and leave it running 24/7. It is definitely a bit more eco-friendly to have 25 watts running 24/7 than 130 watts, that’s for sure.

    The next article will be up this week – just been insanely busy with coding jobs. It’ll cover installing bind, postgresql, and git, along with other tidbits that I’ve missed.

    Thanks for reading :)

  5. Pingback: Nusuni – How To Set Up An Ubuntu Development Server (Part 3, Postgresql, SSH, BIND, and Name-Based Apache Hosts)

  6. Pingback: Nusuni – How To Set Up An Ubuntu Development Server (Part 1)

  7. gug
    April 11, 2012 at 1:47 am

    I use SuPHP, and I have 500 error in all php files, if I change permissin to 755 is ok, but I need to do this for every single file went I create the file, is a better way to do this?

  8. Jeremy Steele
    April 11, 2012 at 10:28 pm

    hmm, you should also be able to use 644 (suphp doesn’t require the execute bit to be set). The suphp configuration file in /etc/suphp/suphp.conf does have a couple of options for permissions.

    By default it doesn’t execute php scripts with file group writeable and file other writeable permissions. On my dev box I enabled allow group writeable so I wouldn’t need to chmod the files every time (ubuntu uses 664 by default). The options are in the “security options” section of the above mentioned configuration file:

    The default options:

    ; Security options
    allow_file_group_writeable=false
    allow_file_others_writeable=false
    allow_directory_group_writeable=false
    allow_directory_others_writeable=false

    Simply change the group writeable:

    ; Security options
    allow_file_group_writeable=true
    allow_file_others_writeable=false
    allow_directory_group_writeable=false
    allow_directory_others_writeable=false

    Then restart apache and you should be set :D

Leave a Reply

Please enter your name and email. Your email address will be kept private.