DevOps Zone is brought to you in partnership with:

Andy is a freelance journalist based in Coventry, United Kingdom. He also codes web applications. Andy is a DZone MVB and is not an employee of DZone and has posted 26 posts at DZone. You can read more from them at their website. View Full User Profile

Setting Up a LAMP Stack on Ubuntu

07.19.2013
| 4003 views |
  • submit to reddit

Local web development on a Ubuntu Linux powered computer needs a LAMP stack installation. That is, the Apache web server, PHP for coding, and the MySQL database. Here is a method for setting it all up for the latest version of Ubuntu Linux.

Just before we begin, you might be wondering why you would bother with Linux at all.

The answer is simple. It is very easy to set up a virtual machine (VM), with Linux running inside it. Then, you can mess round with code, such as trying out WordPress, or building web applications, without having to mess with your main computer. If something goes wrong, you can reset the VM, and nothing is lost (apart from some time to set it back up again).

Right, let’s crack on.

Installing the LAMP Stack on Ubuntu

I wrote this up from a clean installation of Ubuntu 12.10. There are various components we need:

  • Apache web server
  • PHP
  • MySQL

Ubuntu makes it easy to get the entire LAMP stack in one hit. But you have use the command line. In fact, we’ll be using the command line a fair bit. It’s not as scary as you might think though.

Open up the Terminal app up, and enter the following command:

1
2
<code>sudo apt-get install lamp-server^
</code>

If this looks totally alien to you, I suggest you spent some time on Google, finding resources about the basic use of the command line with Linux.

What the command actually does, is fetch and prepare for installation, all the files needed to setup your LAMP stack:

Installing LAMP

Most of the process is automated, but when it gets to MySQL, you will be prompted to add a password:

MySQL installation

Once the process completes, you can open your browser and go to http://localhost:

localhost

Next, we need a way to administer the MySQL database. There is a great tool called phpMyAdmin that will do the job nicely.

Back on the command line do:

1
2
<code>sudo apt-get install phpmyadmin
</code>

During the installation process, you will be asked which web server you want to set up. We’re using Apache:

phpMyAdmin

Once installed, it will be available at:

http://localhost/phpmyadmin.

So far then, we have: Apache, PHP, MySQL, and phpMyAdmin installed and ready to go. The next thing we need to do, is create a virtual host so that we can use our home folder to develop in.

Warning: this next bit contains command line shenanigans aplenty…

Virtual Hosts

First, we must make sure we are in the correct directory:

1
2
<code>cd/etc/apache2/sites-available
</code>

Just before we start creating a virtual host, we’ll make sure that Apache can serve pretty url’s. To do that, we’ll install mod rewrite:

1
2
<code>sudo a2enmod rewrite
</code>

Next, we’ll create our first virtual host. Apache comes with a default one, so we’ll copy that, and edit it to suit our purposes:

1
2
<code>sudo cp defaultyourname.dev
</code>

Then do:

1
2
<code>sudo gedit yourname.dev
</code>

Add the following to the file (replacing yourname with your actual name, the same name you gave the file)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<code>
ServerAdmin webmaster@localhost
ServerName you.dev
DocumentRoot/home/you/www
Options FollowSymLinks
AllowOverride All
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</code>

Save and close the file. Next, we must enable the site we have created. That is done in 2 steps:

  1. Type sudo a2ensite yourname.dev. Ubuntu will respond telling you to restart the Apache service. No need to yet though. We’ll do the other step first.
  2. Do sudo edit /etc/hosts and add this line: 127.0.0.1 yourname.dev. Save and close the file

Now you can restart Apache:

1
2
<code>sudo service apache2 restart
</code>

Before you can try this out, you’ll need to create the www folder in your home directory:

www folder

Now we can try out our new host. Create a new file using Gedit or your favourite code editor and add:

Then save the file in as: info.php in the /home/www folder. In your browser, go to: http://yourname.dev/info.php. You should be greeted with the info file that shows you the PHP configuration:

PHP Info

Finally…

So now, you can build a web application and store and test it from your WWW folder. You can of course, create other virtual hosts in the same way, for different projects.

Published at DZone with permission of Andy Hawthorne, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)