Skip to main content

Building Your Web Server Installing Apache, MySQL, PHP (LAMP) Stack on Ubuntu 12.04

· 6 min read
Yashwin Shankar
Front End Developer @ Tech4Biz Solutions Pvt Ltd.
What is LAMP?
LAMP stack is a bundle of free software that helps set up web servers. The name stands for Linux, Apache, MySQL, and PHP. Since your virtual private server is already using Ubuntu (which is a type of Linux), you've got one part covered. Now, let's see how to install the rest.
Getting Started
To follow this tutorial, you'll need to have administrator access on your VPS. If you're unsure how to set this up, check out the "Initial Server Setup" guide for instructions on getting started.
Setting Up Apache
Apache is a popular, free software used to run many websites.

To install Apache, follow these steps:
  1. Open the terminal on your computer.
  2. Type in the following commands:
sudo apt-get update sudo apt-get install apache2

That's all there is to it! To make sure Apache is installed correctly, just open your web browser and enter your server's IP address (e.g., http ://12.34.56.789). If everything's set up right, you'll see a page with the words "It works!" on it.
How to Check Your Server's IP Address
To find out your server's IP address, you can use the following command:
ifconfig eth0 | grep inet | awk '{ print $2 }'

MySQL Setup
MySQL is a system that helps you manage your data, making it easier to organize and find information.

To install MySQL, follow these steps in your terminal:
sudo apt-get install mysql-server libapache2-mod-auth-mysql php5-mysql

After the installation, MySQL will prompt you to set a root password. If you didn't set the password during the installation, don't worry. You can easily set it later from inside the MySQL shell.
Once MySQL is installed, let's activate it with this command:
sudo mysql_install_db

To complete the process, run the MySQL setup script:
sudo /usr/bin/mysql_secure_installation

You'll be asked to enter your current root password. Just type it in.
Enter current password for root (enter for none): OK, successfully used password, moving on…
Next, the prompt will inquire if you wish to change the root password. Select "N" to keep the current password and proceed to the following steps.

It's simplest to say "Yes" to all the options. Once you've done that, MySQL will reload and apply the updates.
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] y ... Success! By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y ... Success! Cleaning up...

After completing those steps, you can wrap up by installing PHP.
Getting PHP installed
PHP is an open source web scripting language that is widely used to build dynamic web pages.
To add PHP to your system, open your terminal and type in this command.
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

Once you've confirmed "yes" to the prompt twice, PHP will install itself automatically.

You might also want to include PHP in the directory index, so it can serve the appropriate PHP index files:
sudo nano /etc/apache2/mods-enabled/dir.conf

Make sure to include "index.php" at the start of your index files. Your page should now appear like this:
<IfModule mod_dir.c> DirectoryIndex index.php index.html index.cgi index.pl index.php index.xhtml index.htm </IfModule>

PHP Extensions
In addition to its core functionality, PHP offers various libraries and extensions that you can install on your virtual server. You can view the available extensions to enhance PHP's capabilities.
apt-cache search php5-

The terminal will show you a list of modules you can choose from. It will look something like this:
php5-cgi - server-side, HTML-embedded scripting language (CGI binary) php5-cli - command-line interpreter for the php5 scripting language php5-common - Common files for packages built from the php5 source php5-curl - CURL module for php5 php5-dbg - Debug symbols for PHP5 php5-dev - Files for PHP5 module development php5-gd - GD module for php5 php5-gmp - GMP module for php5 php5-ldap - LDAP module for php5 php5-mysql - MySQL module for php5 php5-odbc - ODBC module for php5 php5-pgsql - PostgreSQL module for php5 php5-pspell - pspell module for php5 php5-recode - recode module for php5 php5-snmp - SNMP module for php5 php5-sqlite - SQLite module for php5 php5-tidy - tidy module for php5 php5-xmlrpc - XML-RPC module for php5 php5-xsl - XSL module for php5 php5-adodb - Extension optimising the ADOdb database abstraction library php5-auth-pam - A PHP5 extension for PAM authentication [...]

When you've chosen the module you want to install, type the following command:
sudo apt-get install name of the module

You can install multiple libraries simultaneously by listing each module's name with a space in between.

Great job! Your droplet now has a fully functional LAMP stack!
Your PHP server's results - Viewing PHP
Even though LAMP is installed, let's confirm by creating a simple PHP info page. To do this, start by making a new file:
sudo nano /var/www/info.php

Now, include the following line:
<?php phpinfo(); ?>

After that, save your changes and close the file.

Next, restart Apache to make sure all the changes are applied:
sudo service apache2 restart

Finally, open your web browser and go to your PHP info page. Make sure to replace "12.34.56.789" with the correct IP address of your server: http://your_server_ip_address/info.php

Conclusion -
Awesome job! You've built a solid foundation for your website using LAMP on Ubuntu. With Apache for serving pages, MySQL for managing data, and PHP for dynamic content, your server is ready to go. To do even more with your server, you can try adding new features with PHP extensions or explore different tools and frameworks that work well with LAMP.

Excited to start creating your website or app? Let's dive in!