How To Install LAMP Stack on Ubuntu

This tutorial will help you to install Apache, MariaDB and PHP 7.1 (LAMP) on Ubuntu 17.10. You can follow this tutorial on a VPS (Virtual Private Server) or on a local Ubuntu computer.

Installing LAMP on Ubuntu has four basic steps:

  1. Update Software Packages
  2. Install Apache Web Server
  3. Install MariaDB Database Server
  4. Install PHP 7.1

Step 1: Update Software Packages

Before you install the LAMP stack, it is a good to update repository and software packages. Run the following commands:

sudo apt update

sudo apt upgrade

Step 2: Install Apache Web Server

Enter the following command to install Apache Web server.

sudo apt install apache2 apache2-utils

Enter y when terminal will ask "Do you want to continue?".

After it is installed, Apache should be automatically started. You can check its status with systemctl.

systemctl status apache2

If it is not running, use systemctl to start it.

sudo systemctl start apache2

To enable Apache to automatically start at boot time.

sudo systemctl enable apache2

To check Apache version:

apache2 -v

Now, type the public IP address of your server (or localhost) in the browser address bar. You should see "It works!" Web page, which means Apache Web server is running properly. If you are installing LAMP on your localcomputer, then type or localhost in the browser address bar.

Now we need to set www-data (Apache user) as the owner of document root. By default it’s owned by the root user.

sudo chown www-data:www-data /var/www/html/ -R

Step 3: Install MariaDB Database Server

MariaDB is a drop-in replacement for MySQL. Enter the following command to install it.

sudo apt install mariadb-server mariadb-client

After it is installed, MariaDB server should be automatically stared. Use systemctl to check its status.

systemctl status mariadb

If it is not running, start it with this command.

sudo systemctl start mariadb

To enable MariaDB to automatically start at boot time, run

sudo systemctl enable mariadb

Now, run the post installation security script.

sudo mysql_secure_installation

When it asks you to enter MariaDB root password, press Enter key as the root password isn’t set yet. Then enter y to set the root password for MariaDB server.

Next, you can press Enter to answer all remaining questions, which will remove anonymous user, disable remote root login and remove test database. This step is a basic requirement for MariaDB database security.

To check MariaDB server version information.

mariadb --version

Step 4: Install PHP 7.1

Enter the following command to install PHP 7.1.

sudo apt install php7.1 libapache2-mod-php7.1 php7.1-mysql php-common php7.1-cli php7.1-common php7.1-json php7.1-opcache php7.1-readline

Enable the Apache php7.1 module and then restart Apache Web server.

sudo a2enmod php7.1

sudo systemctl restart apache2

To check PHP version information.

php --version

To test PHP scripts with Apache server, you need to create a info.php file in the document root directory.

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

Paste the following PHP code into the file.

<?php phpinfo(); ?>

Save and close the file.

Now, in the browser address bar, enter server-ip-address/info.php. If you follow this tutorial on your local computer, then type or localhost/info.php. You should see your server’s PHP information. This means PHP scripts can run properly with Apache web server.

For your server’s security, you should delete info.php file now.

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


Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive