Set Up LAMP Stack (Linux, Apache, MySQL, PHP) on Ubuntu 19.04

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 will show you how to set up a LAMP stack on Ubuntu 19.04.

In order to complete this guide, you will need to have an Ubuntu 19.04 server with a non-root sudo-enabled user account and a basic firewall.

Installing 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 -y apache2

It will ask you for your regular user's password to verify your intentions. Once you've entered your password, apt will tell you which packages it plans to install and how much extra disk space they'll take up.

Configure Firewall to Allow Web Traffic
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

Available applications:
  Apache Full
  Apache Secure

Allow incoming SSH, HTTP and HTTPS traffic using the following commands:

sudo ufw allow in 'Apache Full'
sudo ufw allow in 'OpenSSH'

Installing MySQL
MySQL is a database management system. Basically, it will organize and provide access to databases where your website or app can store information.

Again, use apt to acquire and install this software:

sudo apt install -y 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

This will ask if you want to configure the VALIDATE PASSWORD PLUGIN.

Answer Y for yes, or anything else to continue without enabling.

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?

Press y|Y for Yes, any other key for No: y

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 2
Please set the password for root here.

New password:

Re-enter new password:

Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y

Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production

Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
 - Dropping test database...

 - Removing privileges on test database...

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y

All done!

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

ALTER USER 'root'@'localhost' INDENTIFIED WITH mysql_native_password BY 'Password';



Installing PHP
PHP is the component of your setup that will process code to display dynamic content. It can run scripts, connect to your MySQL databases to get information, and hand the processed content over to your web server to display.

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

This should install PHP without any problems.

In most cases, you will 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 the web server to prefer PHP files over others, so 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

It will look like this:

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

Move the PHP index file (highlighted above) to the first position after the DirectoryIndex specification, like this:

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

When you are finished, save and close the file by pressing CTRL+X. Confirm the save by typing Y and then hit ENTER to verify the file save location.

After this, restart the Apache web server in order for your changes to be recognized. Do this by typing this:

sudo systemctl restart apache2
sudo systemctl status apache2

apache2.service - The Apache HTTP Server
   Loaded: loaded (/lib/systemd/system/apache2.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2019-04-16 06:21:14 UTC; 9s ago
  Process: 11855 ExecStart=/usr/sbin/apachectl start (code=exited, status=0/SUCCESS)
 Main PID: 11869 (apache2)
    Tasks: 6 (limit: 2277)
   Memory: 11.1M
   CGroup: /system.slice/apache2.service
           ├─11869 /usr/sbin/apache2 -k start
           ├─11870 /usr/sbin/apache2 -k start
           ├─11871 /usr/sbin/apache2 -k start
           ├─11872 /usr/sbin/apache2 -k start
           ├─11873 /usr/sbin/apache2 -k start
           └─11874 /usr/sbin/apache2 -k start

Apr 16 06:21:14 ubuntu1904 systemd[1]: Starting The Apache HTTP Server...
Apr 16 06:21:14 ubuntu1904 apachectl[11855]: AH00558: apache2: Could not reliably determine the ser
Apr 16 06:21:14 ubuntu1904 systemd[1]: Started The Apache HTTP Server.

Press Q to exit this status output.

Testing PHP Processing
In order to test that your system is configured properly for PHP, create a very basic PHP script called info.php. In order for Apache to find this file and serve it correctly, it must be saved to a very specific directory, which is called the "web root".

In Ubuntu 19.04, this directory is located at /var/www/html/. Create the file at that location by running:

sudo nano /var/www/html/info.php

This will open a blank file. Add the following text, which is valid PHP code, inside the file:


When you are finished, save and close the file.

Now you can test whether your web server is able to correctly display content generated by this PHP script. To try this out, visit this page in your web browser. You'll need your server's public IP address again.

The address you will want to visit is:


The page that you come to should look something like this:

This page provides some basic information about your server from the perspective of PHP. It is useful for debugging and to ensure that your settings are being applied correctly.

If you can see this page in your browser, then your PHP is working as expected.

You probably want to remove this file after this test because it could actually give information about your server to unauthorized users. To do this, run the following command:

sudo rm /var/www/html/info.php

You can always recreate this page if you need to access the information again later.

Wrapping up
Now that you have a LAMP stack installed, you have many choices for what to do next. Basically, you've installed a platform that will allow you to install most kinds of websites and web software on your Ubuntu 19.04 server.



  2. Great explanation, works perfectly. Thanks

  3. HI, thanks for the article. Just spotted a type on the password update
    `ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Password';`


Powered by Blogger.