Install LEMP on AWS EC2 Ubuntu 16.04/18.04

In this post we are going to cover how to setup and install LEMP(Linux Nginx Mysql PHP) on AWS EC2 Ubuntu 16.04/18.04. In this post we have presented step by step tutorial explaining the installation process of lemp stack on a ubuntu system in an easy way. This process stands good on AWS EC2, Digital ocean droplets, linode or any other cloud or non managed vps providers.

STEP 1: Install Nginx

Update the package list from repositories for packages that needs upgrading.

sudo apt-get update

Upgrade the packages that needs upgrading.

sudo apt-get upgrade

Install Nginx

sudo apt-get install nginx

After installing nginx use below commands to stop, start and enable nginx to run as a service so that it can start automatically after server reboots.

sudo systemctl stop nginx.service
sudo systemctl start ngiinx.service
sudo systemctl enable nginx.service

To test if nginxis installed and running. Open your browser and run – http://ip-address-of-aws-ec2-instance
nginx install

STEP 2: Install PHP7.2 and other PHP Extensions

Add Repository to download and install PHP7.2 and extensions.

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:ondrej/php

Update package list.

sudo apt-get update

Install PHP and related extensions.

sudo apt install php7.2-fpm php7.2-common php7.2-mysql php7.2-gmp php7.2-curl php7.2-intl php7.2-mbstring php7.2-xmlrpc php7.2-gd php7.2-bcmath php7.2-xml php7.2-cli php7.2-zip

Let’s tune up some php settings. Run below command

sudo nano /etc/php/7.2/fpm/php.ini

Adjust parameters according to below settings, and the save and exit.

file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
cgi.fix_pathinfo = 0
upload_max_filesize = 100M
max_execution_time = 360

Do the same settings in cli as well. Run below commands and update the parameters as per above list.

sudo nano /etc/php/7.2/cli/php.ini

Inorder to make above settings take effect restart nginxand php-fpm. Run below commands to do so

sudo service php7.2-fpm restart
sudo systemctl restart nginx.service

To test successful php installation create a new phpinfo file under /var/www/html directory

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

Add below contents, save and exit.


Now open your browser and type in host name or ip address of server followed by phpinfo.php

STEP 3: Install Mariadb/Mysql

Use below commands to install mysql or mariadb

sudo apt-get install mariadb-server mariadb-client

For ubuntu 16.04-

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

For ubuntu 18.04

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Secure database server with root password

sudo mysql_secure_installation

When prompted, answer below questions:

When prompted, answer the questions below by following guidelines.

Enter current password for root (enter for none): Just press the Enter
Set root password? [Y/n]: Y
New password: Enter root password
Re-enter new password: Repeat root password
Remove anonymous users? [Y/n]: Y
Disallow root login remotely? [Y/n]: Y
Remove test database and access to it? [Y/n]:  Y
Reload privilege tables now? [Y/n]:  Y

Test database server is installed and if able to be connected to database or not

sudo mysql -u root -p

When prompted enter the root password
If you get below screen, then consider mysql is installed and working.
mysql ubuntu

Add a Comment

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