How to Install LAMP Stack on Ubuntu 15.04

December 10, 2015 in Tutorial4 minutes

Learn how to set up a LAMP stack on Ubuntu 15.04. This comprehensive tutorial covers the installation and configuration of Apache, MySQL, and PHP, including securing your MySQL server and managing PHP modules.

What is a LAMP Stack?

A LAMP stack is a popular open-source software bundle used for web development. It consists of four key components:

  • Linux: The operating system that forms the foundation of the stack.
  • Apache: The web server responsible for handling HTTP requests.
  • MySQL: The relational database management system used for storing application data.
  • PHP: The scripting language used to create dynamic web content.

While MySQL can be substituted with alternatives like MariaDB or PerconaDB, and PHP with Perl or Python, this guide will focus on the traditional LAMP setup on Ubuntu 15.04.

Step 1: Update Your System

Before installing new packages, it’s essential to update your system to ensure you have the latest versions and security patches.

sudo apt-get update
sudo apt-get upgrade

Step 2: Install Apache

With your system updated, the first step is to install the Apache web server.

sudo apt-get install apache2

After the installation is complete, you can verify that Apache is running by navigating to your server’s IP address (http://your_server_ip) in a web browser. You should see the default Apache welcome page.

To check the status of the Apache service, run the following command:

sudo service apache2 status

You should see an output indicating that the service is active and running.

● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2)
   Active: active (running) since Wed 2015-12-09 18:12:23 EST; 17min ago
     Docs: man:systemd-sysv-generator(8)
 CGroup: /system.slice/apache2.service
         ├─15957 /usr/sbin/apache2 -k start
         ├─15960 /usr/sbin/apache2 -k start
         └─15961 /usr/sbin/apache2 -k start

You can also check which port Apache is listening on with the netstat command:

sudo netstat -naptu | grep LISTEN

The output should show that apache2 is listening on port 80.

Step 3: Configure Apache

Apache’s main configuration file is located at /etc/apache2/apache2.conf. However, on Ubuntu, the configuration is modular, with different settings split into separate files. Key configuration directories include:

  • ports.conf: Specifies the ports Apache listens on.
  • mods-enabled/: Contains modules that are currently enabled.
  • conf-enabled/: Holds additional configuration files.
  • sites-enabled/: Contains virtual host configurations.

Changing the Default Index File

By default, Apache serves files from the /var/www/html directory. To create a custom homepage, you can replace the default index.html file.

First, back up the original file:

sudo mv /var/www/html/index.html /var/www/html/index.html.bak

Next, create a new index.html file with your desired content:

<!DOCTYPE html>
<html>
  <head>
    <title>LAMP Stack on Ubuntu 15.04</title>
  </head>
  <body>
    <h1>Welcome to My LAMP Server!</h1>
  </body>
</html>

Now, when you refresh your browser, you should see your new custom page.

Step 4: Install PHP

To serve dynamic content, you need to install PHP. The following command installs PHP and the Apache PHP module:

sudo apt-get install php5 libapache2-mod-php5

To test the PHP installation, create a new file named phpinfo.php in the /var/www/html directory:

<?php
phpinfo();
?>

When you access http://your_server_ip/phpinfo.php in your browser, you should see a page displaying detailed information about your PHP configuration.

Configuring PHP

The main PHP configuration file for Apache is located at /etc/php5/apache2/php.ini. If you make any changes to this file, you’ll need to restart Apache for them to take effect:

sudo service apache2 restart

Installing PHP Modules

PHP has a wide range of modules to extend its functionality. You can search for available modules using apt-cache:

sudo apt-cache search php5-

To enable MySQL support in PHP, install the php5-mysql module:

sudo apt-get install php5-mysql

Step 5: Install MySQL

The final component of the LAMP stack is MySQL. Since MySQL 5.6 is the default version in Ubuntu 15.04, you can install it with the following command:

sudo apt-get -y install mysql-server

During the installation, you will be prompted to set a root password for MySQL.

After the installation is complete, you can check the status of the MySQL service:

sudo service mysql status

The output should confirm that the MySQL server is running.

Step 6: Secure MySQL

By default, a fresh MySQL installation is not secure. To harden your MySQL server, run the mysql_secure_installation script:

mysql_secure_installation

This script will guide you through several security-related options, including:

  • Changing the root password: Since you already set a password, you can skip this.
  • Removing anonymous users: It’s recommended to remove anonymous users for better security.
  • Disallowing remote root login: Restricting root access to localhost is a crucial security measure.
  • Removing the test database: The test database is for demonstration purposes and should be removed in a production environment.

Step 7: Configure MySQL (Optional)

The main MySQL configuration file is /etc/mysql/my.cnf. By default, MySQL only listens for connections from localhost. If you need to allow remote connections, you can change the bind-address directive in /etc/mysql/mysql.conf.d/mysqld.cnf from 127.0.0.1 to 0.0.0.0.

After making this change, restart the MySQL service:

sudo service mysql restart

You can verify the change by checking which address MySQL is listening on:

sudo netstat -naptu | grep LISTEN | grep mysql

TL;DR Version

If you prefer a quicker setup, you can install the entire LAMP stack with a single command:

sudo apt-get install lamp-server^

Summary

You have successfully installed and configured a LAMP stack on your Ubuntu 15.04 server. With Apache, MySQL, and PHP up and running, you are now ready to host dynamic web applications.