How to install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04 and Ubuntu 16.10

HomeOther ContentHow to install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04 and Ubuntu 16.10
How to install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04 and Ubuntu 16.10
How to install Linux, Apache, MySQL, PHP (LAMP) stack on Ubuntu 16.04 and Ubuntu 16.10
Introduction

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 applications. This term is actually an acronym that represents the Linux operating system, with the Apache web server. Site data is stored in a MySQL database and dynamic content is processed by PHP.

In this guide, we will install a LAMP stack on an Ubuntu 16.04 droplet. Ubuntu will meet 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. Although this is harmless, if you do not set ServerName globally, you will receive the following warning when checking your Apache configuration for syntax errors:

Open the main configuration file with your text modification:

#sudo nano /etc/apache2/apache2.conf

Adjust the firewall to allow web traffic

Next, assuming you followed the initial server setup instructions to enable the UFW firewall, make sure your firewall allows HTTP and HTTPS traffic. You can ensure that UFW has an application profile for Apache as follows:

List of #sudo ufw applications

If you look at the Apache Full profile, it should show that it allows traffic to ports 80 and 443:

#sudo ufw application information /"Apache Full/"

Step 2: Install MySQL

Now that our web server is up and running, it's time to install MySQL. MySQL is a database management system. Basically, it will organize and provide access to the databases in which our site can store information.

#sudo apt-get install mysql server

Once the installation is complete, we want to run a simple security script that will remove some dangerous default settings and somewhat lock down access to our database system. Start the interactive script by running:

Warning: Enabling this feature is somewhat a matter of judgment. If enabled, passwords that do not match the specified criteria will be rejected by MySQL with an error. This will cause problems if you use a weak password in conjunction with software that automatically configures MySQL user credentials, such as the Ubuntu packages for phpMyAdmin. It's safe to leave validation disabled, but you should always use strong, unique passwords for database credentials.

#sudo mysql_secure_installation

Step 3: Install PHP

PHP is the component of our setup that will process the code to display dynamic content. It can run scripts, connect to our MySQL databases to obtain information and transmit the processed content to our web server for 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 will want to change the way 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 that, we need to restart the Apache web server for our changes to be recognized. You can do this by typing this:

#systemctl restart apache2

Install PHP modules

To improve the functionality of PHP, we can optionally install additional modules.

To see the options available for PHP modules and libraries, you can pipe the apt-cache search results to less, a pager that 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 exit.

For example, to find out what the php-cli module does, we could type this:

apt-cache shows php-cli

If, after searching, you decide to install a package, you can do so using the apt-get install command as we have done for our other software.

If we decide that php-cli is something we need, we could type:

# sudo apt-get install php-cli

Step 4: Test PHP Processing on Your Web Server
In order to test that our system is correctly configured for PHP, we can create a very basic PHP script.

We will call this script info.php. For Apache to find the file and serve it correctly, it must be saved in a very specific directory, called /"web root/".

In Ubuntu 14.04 this directory is located in /var/www/html/. We can create the file in this 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:

Please take the opportunity to connect and share this video with your friends and family if you find it useful.

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *