Install LEMP(Linux, Nginx, MySql, PHP) Stack – Install Nginx On EC2
What is LEMP stack?
LEMP is a set of open source software which is used to serve dynamic web pages and dynamic content.LEMP stands for Linux, NGINX, MySql, PHP. Linux, as we all know, is an open source operating system and the server used to serve web pages in this stack is Nginx web server which is a very fast and lightweight web server if you want to know more about Nginx read this post.The database engine is based on MySql database and the processing of data and logic is handled by PHP which is a server-side scripting language widely used for web development.There is another variation of LEMP stack which uses Apache as the web server and is called LAMP stack (Linux, Apache, MySql, PHP).Today more and more companies and individuals are migrating to LEMP stack from LAMP stack and even this website is also running on LEMP stack. In this post, we will learn how to Install Nginx, MySql, And PHP step by step on your EC2 instance which already has an Ubuntu 16.04 operating system up and running and if you want to know how to set up EC2 instance read this post.
Step 1 – Install Nginx
Before starting you need an EC2 instance with Ubuntu 16.04 (Linux) operating system installed if you don’t know how to setup EC2 instance follow this post.
Nginx web server is required to serve your dynamic content to the users. we will be using Ubuntu’s default package repositories for installing all the software in this post so we will use apt package management tool to complete the installation.let’s start with updating the local package index, open your terminal and connect to EC2 via SSH and type following commands in the terminal.
sudo apt-get update sudo apt-get install nginx
Next, we need to add a rule to ufw firewall settings to allow connections to Nginx.Since we are not setting up SSL so we will only allow traffic on port 80 which is the default port for HTTP(Hyper Text Transfer Protocol) traffic.Type the below command to do the same.
sudo ufw allow 'Nginx HTTP'
And you can check the status with the following command.
sudo ufw status
After adding this new firewall rule you can test if the server is running or not by opening a browser and entering the domain name of the server or public IP of the server which you can find in the EC2 dashboard.if you have done everything right you will see the Nginx default welcome screen which looks like this.
Step 2 – Install Mysql
MySql is an open source relational database management system and we will use it to store and manage data on our website.Let’s start MySql installation by typing the following command in terminal.
sudo apt-get install mysql-server
While installing MySql, the system will ask you to enter the password for the root user(admin), enter the password and remember it or save it somewhere safe because we will need this password while logging in to the system.MySql is now installed on the system now but to configure it we need to follow some additional steps which are related to the security, you can skip if you want but I strongly recommend it type the following command in the terminal to configure.
You will be asked certain questions like to change the root password, setup Validate Password Plugin(answer no), remove anonymous user accounts(answer Y), remove test databases (answer Y) , disable root logins outside of localhost(answer Y).You can read more about it on MySql Official Documentation.
Step 3 -Install PHP
We installed Nginx which will be used to serve our web pages and we installed MySql to manage and store our data now we need to install PHP which we will be using to generate dynamic content.To run PHP on Nginx we need to install php-fpm(fastCGI process manager) .we will configure Nginx to pass all the PHP requests through this software.We will also install another package(php-mysql) which we will be used to communicate to our MySql database.Type the following commands in the terminal.
sudo apt-get install php-fpm php-mysql
By executing above commands we will install PHP successfully on our system but we need to make some changes in the php.ini file which is the configuration file for PHP. This step is highly recommended so don’t skip this step or your server’s security will be compromised.Open the configuration file by typing following command.
sudo vi /etc/php/7.0/fpm/php.ini
Search for this line in the file cgi.fix_pathinfo, this line will be commented out with the help of a semi-colon(;) and its default value will be 1. Change the value to 0 and un-comment the line by removing the semi-colon.Save the file and restart the PHP processor by typing the following command.
sudo service php7.0-fpm restart
This will save and reflect the changes that we just made.By completing this step we have installed PHP successfully on our system.
In the above post we learned to install LEMP stack and now our system is ready to host a website with dynamic content but wait you must be thinking where I will upload my files, there must be some kind of dashboard or CPanel that hosting companies provide you with, but unfortunately C Panel is paid instead we will use an open source panel which is compatible with Nginx which we will install and configure in the next post.
Have a good day!