How to install LAMP stack on Ubuntu 18.04?

Cropped image of It specialist working on code

To run dynamic websites on the Ubuntu VPS or dedicated server, you need to install Apache, PHP and MySQL on the server. In this article, we will learn how to install Apache 2, PHP 7.3 and MySQL on Ubuntu 18.04.

first of all, login to your VPS by following command:

ssh root@XXX.XXX.XXX.XXX

Here root is the SSH user and XXX.XXX.XXX.XXX is the IP address of the VPS. Don’t forget to replace it with your server’s IP address.
You can use “root” user for the installation. However it will be good if you have a non-root user which can perform sudo tasks.

Run the above command and hit enter. It will ask for a password. Type the password and hit enter.

Step 1: Install Apache

Before we start to install Apache, Update/Upgrade software packages by running following command

sudo apt update
sudo apt upgrade

Now install Apache by running command given below

sudo apt install apache2 apache2-utils

It will ask to confirm Apache installation. Press “Y” and hit enter.

Install curl

sudo apt install curl

Verify the successfull installation by the command given below. It should return the IP address.


Set Apache user (www-data) as an owner of web root directory

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

Step 2: Install MySQL

Run command given below to install MySQL database server on Ubuntu 18.04 VPS

sudo apt install mariadb-server mariadb-client

It will ask to confirm MySQL server installation. Press “Y” and hit enter.

Now MySQL is installed on the server with default settings. These default setting are not secure and sometimes it makes the server vulnerable to attacks. We can secure the server by running a single command.

sudo mysql_secure_installation

Now it will ask few question.
prompts and answers

login to database by following command

sudo mysql -u root -p

Type database password and hit enter

Sometimes there is a permission issue. To avoid it run the commands given below and after that flush the privileges.

USE mysql;
UPDATE user SET plugin='mysql_native_password' WHERE User='root';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost';

Step 3: Install PHP 7.3

Install commands given below one by one to install PHP.

sudo apt install apt-transport-https lsb-release ca-certificates
sudo wget -O /etc/apt/trusted.gpg.d/php.gpg
sudo sh -c 'echo "deb $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list'
sudo apt update
sudo apt-cache search php7.3
sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install -y php7.3

Install required PHP extensions by running following command

sudo apt install php7.3 libapache2-mod-php7.3 php7.3-mysql php-common php7.3-cli php-xml php7.3-common php7.3-json php7.3-opcache php7.3-readline -y php7.3-gd php7.3-intl php7.3-xsl php7.3-mbstring php7.3-zip php7.3-curl

Set and configure locale by command given below

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales

Enable PHP 7.3, mode rewrite and restart apache service

sudo a2enmod php7.3
sudo a2enmod rewrite
sudo systemctl restart apache2

Step 4: Install Composer, Git and Unzip

sudo apt update
sudo apt install git unzip
cd ~
curl -sS -o composer-setup.php
sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer

Here we have installed Git globally.

Step 5: Setup Virtual Host

To setup the virtual host, we will update /etc/apache2/sites-available/000-default.conf file. To edit that file run the command given below:

sudo nano /etc/apache2/sites-available/000-default.conf

Delete all lines and add the following lines:

<VirtualHost *:80>
  ServerName domain.tld
  ServerAlias www.domain.tld
  DocumentRoot /var/www/html/project_name/public
  <Directory /var/www/html/project_name>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Require all granted
    Allow from All

    <IfModule mod_rewrite.c>
      Options -MultiViews
      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ index.php [QSA,L]

  ErrorLog /var/log/apache2/domain.tld_error.log
  CustomLog /var/log/apache2/domain.tld_access.log combined

   # optionally set the value of the environment variables used in the application
   	SetEnv APP_ENV ${APP_ENV}

Don’t forget to replace domain.tld with your domain name and project_name with your project name.
This configuration is for Laravel and Symfony projects.

Edit environment file:

sudo nano /etc/environment

and add the line given below

export APP_ENV=prod

Don’t forget to replace all values according to your project.

Edit the envvars file by

sudo nano /etc/apache2/envvars

and add a line at the end of a file to load all environment variables

. /etc/environment

press control+O to write a file and hit enter to save the file.

After installation, restart VPS once.

init 6

Leave a Reply

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