Linux, Nginx, MySQL, and PHP are what is known as a LEMP stack. It is similar to a LAMP stack (Linux, Apache, MySQL, and PHP). However, instead of using Apache as the HTTP web server, this stack replaces it with Nginx. Nginx was developed after Apache and because of that fixed many scaling issues that other HTTP webservers were known to have. In this article, we will be going over the installation of a LEMP stack on Ubuntu 20.04.
Prerequisite
Before moving on, if you would like to access your LEMP stack remotely to use as a fully functional web server you will need to purchase a public IP. Click on the link below to do so.
How to Purchase and Activate a Dedicated Public IP Address for your Shell™
Installing Nginx
Let's open up a terminal by selecting and searching for it in the search bar and clicking on the Terminal icon to open a session and update our system's packages.
sudo apt update
Next, we will install, start and enable nginx with the following commands below.
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
Once completed, let's check the status of Nginx
sudo systemctl status nginx
or
Open up your web browser and type localhost. You should see the following below:
If you have purchased a public IP make sure to download and enable your firewall and open port HTTP port 80.
sudo apt install ufw
sudo ufw enable
sudo ufw allow http
Once you allow HTTP port 80, let's check the status of the firewall.
sudo ufw status
Installing MySQL
Let's go ahead and install MySQL so that we can have a place to store our web server data.
sudo apt install mysql-server
After that, let's create a MySQL root password using the command below following the steps instructed to create a root password.
sudo mysql_secure_installation
Now, let's test if we can access Mysql.
sudo mysql
If you get the mysql> prompt you are go to go. Let's Type exit to exit MySQL.
Installing PHP-fpm
Finally, we will install PHP
sudo apt install php-fpm php-mysql
Once complete, let's start, enable and check the status of PHP.
sudo systemctl start php7.4-fpm
sudo systemctl enable php7.4-fpm
systemctl status php7.4-rpm
Configuring Nginx server block
Let's first remove the default server block for Nginx. Nginx server block is like a virtual host on Apache. This allows the use of creating more than one website on a domain. We are removing the default server block because it may not work properly with PHP.
sudo rm /etc/nginx/sites-enabled/default
After we have removed the default server block, we can create a brand new server block using your favorite terminal text editor. I will be using nano.
sudo nano /etc/nginx/conf.d/default.conf
And paste the following below into the new file and save it once done. In server_name we are using a catch-all. If you had a domain name you would replace _ with that domain name.
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html/;
index index.php index.html index.htm index.nginx-debian.html;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
include snippets/fastcgi-php.conf;
}
# A long browser cache lifetime can speed up repeat visits to your page
location ~* \.(jpg|jpeg|gif|png|webp|svg|woff|woff2|ttf|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
# disable access to hidden files
location ~ /\.ht {
access_log off;
log_not_found off;
deny all;
}
}
Next, we will test to see if the default.conf file has any errors.
sudo nginx -t
If there are no errors you should see the following message below:
After we confirmed the config file has no errors, we will restart Nginx
sudo systemctl restart nginx
Testing to see if PHP is working with Nginx
Let’s create an info.php file
sudo nano usr/share/nginx/html/info.php
Paste the following in the file and save the file.
<?php phpinfo(); ?>
Once you have the file saved you can test to see if PHP is working by going to http://localhost/info.php or http://your_ipaddress_or_domain_name/info.php if you have public IP or domain name. You should see the following below if Nginx and PHP are communicating.
Once verified that Nginx is working with PHP you can now remove the info.php file
sudo rm usr/share/nginx/html/info.php
Conclusion
And now you have a LEMP stack installed. You can now start creating your own website and administer it!
Comments
0 comments
Please sign in to leave a comment.