Beta 42

Research and Development

Menu

Install LAMP Stack on Linux Box

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.

This tutorial was tested on Linux systems with Ubuntu 18.04, 19.10 and 20.04, but should also work with other Debian-like distributions as well.

Prerequisites

In order to complete this tutorial, you will need to have an Ubuntu 18.04 server (or newser) with a non-root sudo-enabled user account and a basic firewall.

Automated Install with tasksel

sudo apt install tasksel
sudo tasksel install lamp-server

Manual Install

Install Apache

The Apache web server is among the most popular web servers in the world. It’s well-documented and has been in wide use for much of the history of the web, which makes it a great default choice for hosting a website.

sudo apt update
sudo apt install apache2

Adjust the Firewall

Next, assuming that you have followed the initial server setup instructions and enabled the UFW firewall, make sure that your firewall allows HTTP and HTTPS traffic. You can check that UFW has an application profile for Apache like so:

sudo ufw app list
sudo ufw app info "Apache Full"

Allow incoming HTTP and HTTPS traffic for this profile:

sudo ufw allow in "Apache Full"

You can do a spot check right away to verify that everything went as planned by visiting your server’s private IP address in your web browser. Also this would be a good moment to redirect the public IP address and the domain name.

Install MySQL

sudo apt install mysql-server

When the installation is complete, run a simple security script that comes pre-installed with MySQL which will remove some dangerous defaults and lock down access to your database system. Start the interactive script by running:

sudo mysql_secure_installation

Note that in Ubuntu systems running MySQL 5.7 and later the root MySQL user is set to authenticate using the auth_socket plugin by default rather than with a password. This allows for some greater security and usability in many cases, but it can also complicate things when you need to allow an external program (e.g., phpMyAdmin) to access the user.

If you prefer to use a password when connecting to MySQL as root, you will need to switch its authentication method from auth_socket to mysql_native_password. To do this, open up the MySQL prompt from your terminal:

sudo mysql

Next, check which authentication method each of your MySQL user accounts use with the following command:

SELECT user,authentication_string,plugin,host FROM mysql.user;

To configure the root account to authenticate with a password, run the following ALTER USER command. Be sure to change password to a strong password of your choosing:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';

Then, run FLUSH PRIVILEGES which tells the server to reload the grant tables and put your new changes into effect:

FLUSH PRIVILEGES;

Check the authentication methods employed by each of your users again to confirm that root no longer authenticates using the auth_socket plugin:

SELECT user,authentication_string,plugin,host FROM mysql.user;

Install PHP

sudo apt install php libapache2-mod-php php-mysql

Edit the dir.conf file:

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

and move the PHP index file to the first position after the DirectoryIndex specification, like this:

<IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.cgi index.pl index.xhtml index.htm
</IfModule>

After this, restart the Apache web server in order for your changes to be recognised:

sudo systemctl restart apache2

You can also check on the status of the apache2 service using systemctl:

sudo systemctl status apache2

Additional PHP Modules

To see the available options for PHP modules and libraries, pipe the results of apt search into less:

apt search php- | less

Look at the long description of the package by typing:

apt show package_name

For example, check out module php-cli:

apt show php-cli