Install LEMP Stack On Ubuntu
LEMP stack stands for Linux, Nginx, MariaDB, and PHP. Here in LAMP stack which stands for Linux, Apache, MariaDB and PHP, all components are not tightly coupled. So by replacing Apache With Nginx, we are installing LEMP stack. This tutorial outlines how to install LEMP stack on Ubuntu 18.04.
Prerequisites
Before you start to install LEMP on Ubuntu 18.04. You must have a non-root user account on your server with sudo privileges.
1. Install NGINX
To install Nginx first update local package index to access most recent package listing by typing
sudo apt update
Now install Nginx by typing
sudo apt install nginx
You can check the status of Nginx by typing
systemctl status nginx
The output should be:
Output:
● nginx.service – A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Fri 2018-07-01 16:08:19 UTC; 1 days ago
Docs: man:nginx(8)
Main PID: 2369 (nginx)
Tasks: 2 (limit: 1153)
CGroup: /system.slice/nginx.service
├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2380 nginx: worker process
2. Install MariaDB
To install MariaDB and related MySQL php extension type following command.
sudo apt install mariadb-server php-mysql
To setup secure installation enter following command, you will be prompted for the password which is not set previously that time press ENTER only.
sudo mysql_secure_installation
3. Install PHP
By default Nginx does not support native PHP processing. So you will need to install php-fpm (“fastCGI process manager”) package to install PHP. Now you can install php-fpm by typing following command.
sudo apt install php-fpm
You can check the status where it is correctly installed or not by following command
systemctl status php7.2-fpm
4. Set up nginx configuration file
Create directory inside var/www/html named example.com (you can use your domain name).
sudo mkdir -p /var/www/html/example.com
Now you should remove the default configuration file provided. To remove default Nginx configuration file type following.
sudo rm -f /etc/nginx/sites-enabled/default
Configuration files for the website are stored inside /etc/nginx/sites-available directory so you need to create configuration file inside this directory named example.com.conf (you can use your domain name). Then enter following code inside that file by replacing example.com with your domain name.
/etc/nginx/sites-available/example.com.conf
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com;
root /var/www/html/example.com;
index index.php;
location / {
try_files $uri $uri/ =404;
}
location ~* .php$ {
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
}
}
Create a link of above configuaration file inside /etc/nginx/sites-enabled/ directory by typing
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
5. Testing LEMP stack
Ensure your domain reaching at your server by configuring DNS records of your domain.
Now you need to restart PHP and reload Nginx configuration file as you have made changes in Nginx configuration directory. Type following command to restart PHP and reload Nginx.
sudo systemctl restart php7.2-fpmsudo nginx -s reload
You can check the status of Nginx by typing following.
sudo nginx -t
Create an index.php file inside /var/www/html/example.com directory and enter following code inside the file.
<html>
<head>
<h2>Index Page</h2>
</head>
<body>
<?php
echo ‘<p>Hello,</p>’;
// Define PHP variables for the MySQL connection.
$servername = “localhost”;
$username = “test_user”;
$password = “password”;
// Creating a MySQL connection.
$conn = mysqli_connect($servername, $username, $password);
// Show if the connection fails or is successful.
if (!$conn) {
exit(‘<p>Your connection has failed.<p>’ . mysqli_connect_error());
}
else {
echo ‘<p>You have connected successfully.</p>’;
}
?>
</body>
</html>
Conclusion
In this tutorial you have successfully learned how to install LEMP stack on Ubuntu 18.04. If you have any queries regarding this please don’t forget to comment below.