A step-by-step guide to installing and configuring a LAMP stack on Ubuntu 14.04, including Apache web server, MySQL database, and PHP scripting language.

Installing Linux, Apache, MySQL, PHP (LAMP) on Ubuntu 14.04
6 mins

Installing Linux, Apache, MySQL, PHP (LAMP) on Ubuntu 14.04h1

This guide will walk you through installing a LAMP stack on Ubuntu 14.04. LAMP stands for Linux, Apache, MySQL, and PHP, which together form a powerful platform for developing and deploying web applications.

Prerequisitesh2

Before starting, ensure you have:

  • Ubuntu 14.04 installed on your server
  • Root or sudo access to your server
  • A stable internet connection
  • Basic knowledge of Linux command line

Step One - Install Apacheh2

The Apache web server is currently the most popular web server in the world, which makes it a great default choice for hosting a website.

We can get started by typing these commands:

Terminal window
sudo apt-get update
sudo apt-get install apache2

Since we are using a sudo command, these operations get executed with root privileges. It will ask you for your regular user’s password to verify.

Afterwards, your web server is installed.

Verify Apache Installationh3

You can do a spot check right away to verify that everything went as planned by visiting your server’s public IP address in your web browser:

http://your_server_IP_address

You will see the default Ubuntu 14.04 Apache web page, which indicates that Apache is working correctly.

How To Find Your Server’s Public IP Addressh3

If you don’t know your server’s public IP address, you can find it using this command:

Terminal window
ip addr show eth0 | grep inet | awk '{ print $2; }' | sed 's/\/.*$//'

This command will display your server’s IP address, which you can use to access your web server.

Step Two - Install MySQLh2

Now that we have our web server up and running, it is time to install MySQL. MySQL is a database management system that will organize and provide access to databases where our site can store information.

Install MySQL and the PHP MySQL module by running:

Terminal window
sudo apt-get install mysql-server php5-mysql

During the installation, your server will ask you to select and confirm a password for the MySQL “root” user. This is an administrative account in MySQL that has increased privileges. Think of it as being similar to the root account for the server itself (the one you are configuring now is a MySQL-specific account however).

Important: Choose a strong password and remember it, as you’ll need it for database administration tasks.

Set Up MySQL Database Structureh3

When the installation is complete, we need to run some additional commands to get our MySQL environment set up securely.

First, we need to tell MySQL to create its database directory structure where it will store its information. You can do this by typing:

Terminal window
sudo mysql_install_db

Secure MySQL Installationh3

Afterwards, we want to run a simple security script that will remove some dangerous defaults and lock down access to our database system a little bit. Start the interactive script by running:

Terminal window
sudo mysql_secure_installation

You will be asked to enter the password you set for the MySQL root account. Next, it will ask you if you want to change that password. If you are happy with your current password, type “n” for “no” at the prompt.

For the rest of the questions, you should simply hit the “ENTER” key through each prompt to accept the default values. This will remove some sample users and databases, disable remote root logins, and load these new rules so that MySQL immediately respects the changes we have made.

Step Three - Install PHPh2

PHP is the component of our setup that will process code to display dynamic content. It can run scripts, connect to our MySQL databases to get information, and hand the processed content over to our web server to display.

We’re going to include some helper packages as well:

Terminal window
sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

This should install PHP without any problems. We’ll test this in a moment.

Configure Apache to Prioritize PHP Filesh3

In most cases, we’ll want to modify the way that Apache serves files when a directory is requested. Currently, if a user requests a directory from the server, Apache will first look for a file called index.html. We want to tell our web server to prefer PHP files, so we’ll make Apache look for an index.php file first.

To do this, type this command to open the dir.conf file in a text editor with root privileges:

Terminal window
sudo nano /etc/apache2/mods-enabled/dir.conf

You will see something like this:

DirectoryIndex index.html index.cgi index.pl index.php index.xhtml index.htm

We want to move the PHP index file highlighted above to the first position after the DirectoryIndex specification, like this:

DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm

When you are finished, save and close the file by pressing “CTRL-X”. You’ll have to confirm the save by typing “Y” and then hit “ENTER” to confirm the file save location.

Restart Apacheh3

After this, we need to restart the Apache web server in order for our changes to be recognized. You can do this by typing:

Terminal window
sudo service apache2 restart

Step Four - Test PHP Processing on Your Web Serverh2

In order to test that our system is configured properly for PHP, we can create a very basic PHP script. We will call this script info.php.

In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the “web root”. In Ubuntu 14.04, this directory is located at /var/www/html/.

We can create the file at that location by typing:

Terminal window
sudo nano /var/www/html/info.php

This will open a blank file. We want to put the following text, which is valid PHP code, inside the file:

<?php
phpinfo();
?>

When you are finished, save and close the file.

Access the PHP Info Pageh3

Now we can test whether our web server can correctly display content generated by a PHP script. To try this out, we just have to visit this page in our web browser. You’ll need your server’s public IP address again.

The address you want to visit will be:

http://your_server_IP_address/info.php

The page that you come to should look something like a detailed PHP configuration page. This page basically gives you information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are being applied correctly.

If this was successful, then your PHP is working as expected.

Remove the Test Fileh3

You probably want to remove this file after this test because it could actually give information about your server to unauthorized users. To do this, you can type:

Terminal window
sudo rm /var/www/html/info.php

You can always recreate this page if you need to access the information again later.

Verification Checklisth2

After completing all steps, verify your LAMP installation:

  • Apache web server is running and accessible
  • MySQL is installed and secured
  • PHP is installed and processing correctly
  • Apache is configured to prioritize PHP files
  • Test PHP file displays correctly

Additional PHP Modules (Optional)h2

You may want to install additional PHP modules depending on your application requirements. Some commonly used modules include:

Terminal window
sudo apt-get install php5-cli php5-curl php5-gd php5-json php5-xml

To see what PHP modules are available, you can use:

Terminal window
apt-cache search php5

Troubleshootingh2

Apache Not Startingh3

If Apache fails to start, check the error logs:

Terminal window
sudo tail -f /var/log/apache2/error.log

MySQL Connection Issuesh3

If you have trouble connecting to MySQL, verify the service is running:

Terminal window
sudo service mysql status

PHP Not Processingh3

If PHP files are being downloaded instead of processed:

  • Verify that libapache2-mod-php5 is installed
  • Check that Apache has been restarted after PHP installation
  • Ensure the dir.conf file has index.php listed first

Security Considerationsh2

  • Remove test files like info.php after testing
  • Change default passwords for MySQL root user
  • Keep software updated with sudo apt-get update && sudo apt-get upgrade
  • Configure firewall to restrict access to necessary ports only
  • Use strong passwords for all database users

Next Stepsh2

Now that you have a LAMP stack installed, you can:

  1. Create your web application files in /var/www/html/
  2. Set up virtual hosts for multiple websites
  3. Configure SSL certificates for HTTPS
  4. Install additional PHP extensions as needed
  5. Set up database users and permissions
  6. Configure Apache modules and settings

Conclusionh2

You now have a fully functional LAMP stack installed on your Ubuntu 14.04 server. Apache is serving your pages, MySQL is ready to store your data, and PHP is processing your scripts. You’re ready to start developing and deploying web applications!


Note: This guide is for educational purposes only. Always follow security best practices when setting up production servers. Keep your system and software updated regularly.