In this article, we will have explained the necessary steps to install and configure the LEMP Stack on Ubuntu 18.04 LTS. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.
LEMP is a variation of the ubiquitous LAMP stack used for developing and deploying web applications. Traditionally, LAMP consists of Linux, Apache, MySQL, and PHP. Due to its modular nature, the components can easily be swapped out. With LEMP, Apache is replaced with the lightweight yet powerful Nginx.
Install LEMP Stack on Ubuntu
Step 1. First, before you start installing any package on your Ubuntu server, we always recommend making sure that all system packages are updated.
sudo apt update sudo apt upgrade
Step 2. Install Nginx.
Install NGINX from the package repository:
sudo apt install nginx
Once installed, start Nginx and enable it to start automatically after a reboot with these two commands:
sudo systemctl start nginx sudo systemctl enable nginx
You can verify that the Nginx web server is running by opening a web browser and visiting your server IP address (http://server_ip_address). You should get the Nginx welcome page.
Step 3. Install MariaDB.
Now to install mysql-server package run below command:
sudo apt install mariadb-server
After completing installation MariaDB will start automatically. Check MariaDB version by typing:
sudo systemctl status mysql
Then, issue the mysql_secure_installation command to improve the security of the MariaDB installation:
sudo mysql_secure_installation
Step 4. Install PHP.
Ubuntu 18.04 LTS ships with the latest and greatest PHP version 7.2. To install PHP and several other most common PHP modules type:
sudo apt install php libapache2-mod-php php-opcache php-cli php-gd php-curl php-mysql php-fpm
Once the PHP packages are installed, restart the Nginx service with:
sudo systemctl restart nginx
Step 5. Configure Nginx for PHP.
The next step you that need to complete is to modify the Nginx configuration file:
cd /etc/nginx/sites-available/ nano /etc/nginx/sites-available/your_domain.com.conf
Paste the following content:
server { listen 80; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name your_domain.com; location / { try_files $uri $uri/ =404; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; } location ~ /\.ht { deny all; } }
To enable the server configuration that we just created, run the following command:
ln -s /etc/nginx/sites-available/your_domain.com.conf /etc/nginx/sites-enabled/your_domain.com.conf
Restart PHP and reload the NGINX configuration:
sudo systemctl restart php-fpm sudo nginx -s reload
Step 6. Configure Firewall.
It is recommended that you enable the ufw firewall and add a rule for Nginx:
sudo ufw allow OpenSSH sudo ufw allow 'Nginx HTTP' sudo ufw enable
Step 7. Testing PHP
Now you should create info.php file to test php to do so type following:
echo "<?php phpinfo(); ?>" | sudo tee /var/www/html/info.php
Then open the following link in your web browser and we will be able to see all the information about PHP and its other configurations: http://your-domain/phpinfo.php
(replacing your IP address with the one above).
That’s all you need to do to install LEMP Stack on Ubuntu 18.04. I hope you find this quick tip helpful. If you have questions or suggestions, feel free to leave a comment below.