How To Install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04
LAMP Stack is a group of open source softwares that are installed on a server to host dynamic websites and web apps.
LAMP is an acronym which represents
L stands for Linux Operating System.
A stands for Apache Web Server.
M stands for MySQL Database.
P stands for PHP programming language.
Note: For this article I am going to create a fresh DigitalOcean droplet with Ubuntu 16.04.3 LTS linux distribution image.
Before you begin installing LAMP stack on fresh server, you need to perform some basic configuration steps as part of as an early basic setup. This will increase security and usability of your server. You can learn how to do this by completing the steps from our initial server setup blog.
Step 1: Install Apache
Apache is the most widely used web server software. It is developed and maintained by Apache Software Foundation, Apache is an open source software available for free.
We can install Apache easily using Ubuntu’s package manager, apt. Ubuntu package manager allows us to install most software painlessly from a repository maintained by Ubuntu. You can learn more about apt from here.
Before installing Apache first run update and upgrade command using command line on terminal.
Since we are using sudocommand, these operations are executed with root privileges. It will ask you for you regular user’s password to verify your identity.
Once you’ve entered your password, apt will tell you which packages it plans to install and how much disk space is required. Press Y and hit Enter to continue the installation.
Set Global ServerName to Suppress Syntax Warnings
Now, we need to add a single line to /etc/apache2/apache2.conf file to suppress warning message. If you don’t set ServerName globally, you will receive the following warning message when checking you server Apache configuration for syntax errors:
$ sudo apache2ctl configtest
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
Open the main configuration file with you favourite text editor, I will use nano for this example:
$ sudo nano /etc/apache2/apache2.conf
Inside at the bottom of the file, add ServerName directive that will point to your primary domain name. If you don’t have a domain name associated with your server, you can also use your server’s public IP Address:
Save and close the file when you have added the ServerName directive.
Now check for syntax errors by typing the following command:
$ sudo apache2ctl configtest
Now restart Apache to implement your changes:
sudo systemctl restart apache2
Adjusting the Firewall to Allow Web Traffic
Assuming that you have followed our Initial Server Setup blog instructions to enable UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can list application profile for Apache using the following command:
$ sudo ufw app list
Now look at Apache Full profile, it shows that it enables the traffic to ports 80 and 443:
$ sudo ufw app info "Apache Full"
Profile: Apache Full
Title: Web Server (HTTP,HTTPS)
Description: Apache v2 is the next generation of the omnipresent Apache web
Now allow incoming traffic for this profile:
$ sudo ufw allow in "Apache Full"
You can verify on the spot to check whether things go as planned by visiting your server’s public IP address in your web browser.
If you see this page, then your web server is correctly installed.
Step 2: Install MySQL
Now our web server is up and running, it’s time to install MySQL. MySQL is a an open source Relational Database Management System.
We can use apt to install mysql.
$ sudo apt-get install mysql-server
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 heightened privileges similar to the root account of the server itself. Make sure the password is strong and unique and don’t leave it blank.
When the installation is complete, we have to run a simple security script that will remove some dangerous defaults and lock down access to our database system a little bit. To run the script type in the terminal:
After the script is completed, your database setup is completed and we proceed further.
Step 3: Install PHP
PHP (recursive acronym for “PHP: Hypertext Preprocessor” is a widely used, open source general purpose scripting language. We will be using PHP to dynamically displaying the web pages by leveraging the data from MySQL database.
We again use APT to install our components. We are also going to install some helper packages as well, so that PHP code can run under Apache server and talk to MySQL database:
Now, we have our PHP installed on our server. We will test it in a moment.
We want to modify the way apache server files when a directory is requested. Currently, if a user requests a directory from the server, Apache first first looks for index.html. We want to tell our server to prefer PHP files, so we’ll make apache look for PHP files first.
To do this, open the dir.conf file using the command below: