A "LAMP" stack is a group of open source software that is typically installed together to enable a server to host dynamic websites and web apps. This term is actually an acronym which represents the Linux operating system, with the Apache web server. The site data is stored in a MySQL database, and dynamic content is processed by PHP.
In this guide, we'll get a LAMP stack installed on an Ubuntu 16.04 Droplet. Ubuntu will fulfill our first requirement: a Linux operating system.
Step 1: Install Apache and Allow in Firewall
#sudo apt-get update
#sudo apt-get install apache2
Set Global ServerName to Suppress Syntax Warnings
Next, we will add a single line to the /etc/apache2/apache2.conf file to suppress a warning message. While harmless, if you do not set ServerName globally, you will receive the following warning when checking your Apache configuration for syntax errors:
Open up the main configuration file with your text edit:
#sudo nano /etc/apache2/apache2.conf
Adjust the Firewall to Allow Web Traffic
Next, assuming that you have followed the initial server setup instructions to enable the UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can make sure that UFW has an application profile for Apache like so:
#sudo ufw app list
If you look at the Apache Full profile, it should show that it enables traffic to ports 80 and 443:
#sudo ufw app info "Apache Full"
Step 2: Install MySQL
Now that we have our web server up and running, it is time to install MySQL. MySQL is a database management system. Basically, it will organize and provide access to databases where our site can store information.
#sudo apt-get install mysql-server
When the installation is complete, 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:
Warning: Enabling this feature is something of a judgment call. If enabled, passwords which don't match the specified criteria will be rejected by MySQL with an error. This will cause issues if you use a weak password in conjunction with software which automatically configures MySQL user credentials, such as the Ubuntu packages for phpMyAdmin. It is safe to leave validation disabled, but you should always use strong, unique passwords for database credentials.
Step 3: Install PHP
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.
#sudo apt-get install php libapache2-mod-php php-mcrypt php-mysql
This should install PHP without any problems. We'll test this in a moment.
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:
#sudo nano /etc/apache2/mods-enabled/dir.conf
After this, we need to restart the Apache web server in order for our changes to be recognized. You can do this by typing this:
#systemctl restart apache2
Install PHP Modules
To enhance the functionality of PHP, we can optionally install some additional modules.
To see the available options for PHP modules and libraries, you can pipe the results of apt-cache search into less, a pager which lets you scroll through the output of other commands:
#apt-cache search php- | less
Use the arrow keys to scroll up and down, and q to quit.
For example, to find out what the php-cli module does, we could type this:
apt-cache show php-cli
If, after researching, you decide you would like to install a package, you can do so by using the apt-get install command like we have been doing for our other software.
If we decided that php-cli is something that we need, we could type:
# sudo apt-get install php-cli
Step 4: Test PHP Processing on your Web Server
n 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:
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: