Tech

Install LEMP Stack (Linux, Nginx, MySQL, PHP)

Are you trying to deploy PHP based website on Linux server. If your answer is yes then you’re at right place. In this article we will learn to install LEMP Stack (Linux, Nginx(pronounced as engine-x, hence the E in the acronym), MySQL, PHP) which is essential to deploy PHP based websites.

Check this link if you want to get free ubuntu machine.

Lets Start.

Step 1: Installation of Nginx Web Server

Nginx is most widely used web server along with Apache.  Nginx full documentation here.

To install run below

$ sudo apt update
$ sudo apt install nginx

When prompted, enter Y to confirm. Nginx web server will be active and running on your Ubuntu after installation. If firewall is enabled on our machine, then we have allow connection to NGINX 

$ sudo ufw app list
Output
Available applications:
  Nginx Full
  Nginx HTTP
  Nginx HTTPS
  OpenSSH

Allow connection to NGINX by running below:

$ sudo ufw allow 'Nginx HTTP'
$ sudo ufw status
Output
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
Nginx HTTP                 ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
Nginx HTTP (v6)            ALLOW       Anywhere (v6)

Next update your iptables configuration to allow HTTP/HTTPS traffic. To update iptables, run the following commands.

$ sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT
$ sudo netfilter-persistent save
$ sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 443 -j ACCEPT $ sudo netfilter-persistent save

Now, we can go to “http://puplic_ip_or_domain” and it should reflect default nginx page

 

Step 2: MySQL installation

We will install database now to store and manage data of our website.  MYSQL docs are here.

Run below command to install mysql:

$ sudo apt install mysql-server

When prompted confirm with Y. 

Now run below script (optional but recommended to run), it will remove some insecure default settings

$ sudo mysql_secure_installation

It will start interactive screen. We can complete this as per message shown.

Regardless of previous step we will be asked to  select and confirm mysql root password here. Post that few interactive question will be asked to complete the setup. Once done run below

$ sudo mysql

It will connect to mysql server install on your server. Now run below command to create database and database user.

Create a user for MySQL. 
mysql> CREATE USER '<your-user-name>'@'localhost' IDENTIFIED BY '<your-password>';
Query OK, 0 rows affected (0.01 sec)

Make the user an admin. 
mysql> GRANT ALL PRIVILEGES ON *.* TO '<your-user-name>'@'localhost';
Query OK, 0 rows affected (0.01 sec)

Create your database. 
mysql> create database <new-db-name>;
Query OK, 1 row affected (0.01 sec)

Check the result. 
mysql>show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| <new-db-name>      |
+--------------------+
5 rows in set (0.00 sec)

Flush privileges to clear cached memory. 
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

 

Step 3: PHP Installation

Nginx requires an external program to handle PHP processing and act as a bridge between the PHP interpreter itself and the web server. This allows for a better overall performance in most PHP-based websites, but it requires additional configuration. We’ll need to install php-fpm, which stands for “PHP fastCGI process manager”, and tell Nginx to pass PHP requests to this software for processing. Additionally, we’ll need php-mysql, a PHP module that allows PHP to communicate with MySQL-based databases. Core PHP packages will automatically be installed as dependencies.

To install the php-fpm and php-mysql packages, run:

$ sudo apt install php-fpm php-mysql

When prompted, type Y and ENTER to confirm installation.

You now have your PHP components installed.

Step 4: NGINX configuration for PHP

On Ubuntu 20.04, Nginx has one server block enabled by default and is configured to serve documents out of a directory at /var/www/html

Instead of modifying /var/www/html, we’ll create a directory structure within /var/www for the your_domain website. For that create root directory for your domain and provide ownership.

$ sudo mkdir /var/www/<your_domain>
$ sudo chown -R $USER:$USER /var/www/<your_domain>

Create new configuration file for your domain

$ sudo nano /etc/nginx/sites-available/your_domain

This will create a new blank file. Paste following  configuration:

server {
    listen 80;
    server_name your_domain www.your_domain;
    root /var/www/your_domain;

    index index.html index.htm index.php;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
     }

    location ~ /\.ht {
        deny all;
    }

}

We can read more about these blocks in nginx docs.

Save this file and activate your configuration by linking to the config file from Nginx’s sites-enabled directory:

$ sudo ln -s /etc/nginx/sites-available/your_domain /etc/nginx/sites-enabled/

    Then, unlink the default configuration file from the /sites-enabled/ directory:

$ sudo unlink /etc/nginx/sites-enabled/default

You can test your configuration for syntax errors by typing:

$ sudo nginx -t

If any errors are reported, go back to your configuration file to review its contents before continuing. When you are ready, reload Nginx to apply the changes:

$ sudo systemctl reload nginx

Your new website is now active, but the web root /var/www/<your_domain> is still empty. Create an index.html file in that location.

Now, we can go to “http://puplic_ip_or_domain” and it should reflect default content of index.html file

Installation of LEMP stack completed. Please let us know through comment if you face any problem. We will reply ASAP.  Please share on social media, if you liked it.

Thank You.                                                                                                                                                                                         Elite Tribune

Leave a Reply

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