Tag Archives: LEMP stack

How to Install NGINX, MySQL, PHP v7 (LEMP) Stack on CentOS 7

In this guide we go through the steps for building the LEMP stack on CentOS 7.

All Commands – without sudo

yum update -y
yum install epel-release -y 
yum install nginx -y
systemctl start nginx
systemctl enable nginx
firewall-cmd – permanent – add-service=http
firewall-cmd – permanent – add-service=https
firewall-cmd – reload
yum install mariadb-server mariadb -y
systemctl start mariadb
systemctl enable mariadb 
mysql_secure_installation
yum install yum-utils http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y
yum-config-manager –enable remi-php73 
yum install php php-mysql php-fpm -y 
sed -i '12s#.*#listen = /run/php-fpm/www.sock#' /etc/php-fpm.d/www.conf 
sed -i '31s#.*#listen.owner = nginx#' /etc/php-fpm.d/www.conf
sed -i '32s#.*#listen.group = nginx#' /etc/php-fpm.d/www.conf
sed -i '39s#.*#user = nginx#' /etc/php-fpm.d/www.conf
sed -i '41s#.*#group = nginx#' /etc/php-fpm.d/www.conf 
chown -R root:nginx /var/lib/php 
systemctl start php-fpm 
systemctl enable php-fpm 
echo -e "\nserver {\n\tlisten 80;\n\tserver_name your_server_ip;\n\n\troot /usr/share/nginx/html;\n\tindex index.php index.html index.htm;\n\n\tlocation / {\n\t\ttry_files \$uri \$uri/ =404;\n\t}\n\terror_page 404 /404.html;\n\terror_page 500 502 503 504 /50x.html;\n\tlocation = /50x.html {\n\t\troot /usr/share/nginx/html;\n\t}\n\n\tlocation ~ \.php$ {\n\\t\ttry_files \$uri =404;\n\t\tfastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;\n\t\tfastcgi_index index.php;\n\t\tfastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n\t\t include fastcgi_params;\n\t}\n}" | tee /etc/nginx/conf.d/default.conf
systemctl restart nginx

All Commands  – with sudo

sudo yum update -y 
sudo yum install epel-release -y  
sudo yum install nginx -y 
sudo systemctl start nginx 
sudo systemctl enable nginx 
sudo firewall-cmd – permanent – add-service=http 
sudo firewall-cmd – permanent – add-service=https 
sudo firewall-cmd – reload 
sudo yum install mariadb-server mariadb -y 
sudo systemctl start mariadb 
sudo systemctl enable mariadb  
sudo mysql_secure_installation 
sudo yum install yum-utils http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y 
sudo yum-config-manager –enable remi-php73  
sudo yum install php php-mysql php-fpm -y 
sudo sed -i '12s#.*#listen = /run/php-fpm/www.sock#' /etc/php-fpm.d/www.conf 
sudo sed -i '31s#.*#listen.owner = nginx#' /etc/php-fpm.d/www.conf
sudo sed -i '32s#.*#listen.group = nginx#' /etc/php-fpm.d/www.conf
sudo sed -i '39s#.*#user = nginx#' /etc/php-fpm.d/www.conf
sudo sed -i '41s#.*#group = nginx#' /etc/php-fpm.d/www.conf
sudo chown -R root:nginx /var/lib/php  
sudo systemctl start php-fpm  
sudo systemctl enable php-fpm  
echo -e "\nserver {\n\tlisten 80;\n\tserver_name your_server_ip;\n\n\troot /usr/share/nginx/html;\n\tindex index.php index.html index.htm;\n\n\tlocation / {\n\t\ttry_files \$uri \$uri/ =404;\n\t}\n\terror_page 404 /404.html;\n\terror_page 500 502 503 504 /50x.html;\n\tlocation = /50x.html {\n\t\troot /usr/share/nginx/html;\n\t}\n\n\tlocation ~ \.php$ {\n\\t\ttry_files \$uri =404;\n\t\tfastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;\n\t\tfastcgi_index index.php;\n\t\tfastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;\n\t\t include fastcgi_params;\n\t}\n}" | sudo tee /etc/nginx/conf.d/default.conf
sudo systemctl restart nginx

LEMP stack is a group of open source software that is installed on a server so that we can host dynamic web applications and sites. LEMP stack is an another variation of the LAMP stack. LEMP is an acronym which consists of Linux as the operating system, NGINX as the web server, MySQL/MariaDB as the relational database management system and PHP as the server-side scripting language. 

Prerequisites

  • A CentOS 7 VPS
  • Root access to the server
  • An SSH client (PuTTY for example)

Step 1: Update Your System

Log in as a sudo user and then update your system using the following  command,

yum update -y

Step 2: Installing NGINX

Nginx is not available on the default CentOS 7 repositories, so add the Extra Packages for Enterprise Linux (EPEL) repository by executing the command below.

yum install epel-release -y

Now that the (EPEL) repository repository is installed on your server, let’s install the NGINX Web Server using the command below.

yum install nginx -y

After the installation, start and enable NGINX.

systemctl start nginx
systemctl enable nginx

Configure the system firewall for NGINX to allow 80 and 443 inbound to the server.

firewall-cmd – permanent – add-service=http

firewall-cmd – permanent – add-service=https

firewall-cmd – reload

To verify if NGINX is running, visit your IP address via the browser.

http://server_domain_name_or_IP/

You should see the default NGINX page as shown below.

Step 3: Installing MariaDB

MariaDB – a drop in replacement of MySQL, is the default database management system in CentOS 7. It is a community-developed fork of the MySQL relational database management system.

MariaDB comes with default CentOS repositories, thus run the following command to install it.

yum install mariadb-server mariadb -y

After the installation is complete, start and enable the service.

systemctl start maridb

systemctl enable mariadb

Run the following command to prevent all unauthorized accesses and remove some dangerous defaults in your database configuration.

mysql_secure_installation

You will be prompted with an option to change the MariaDB root password, remove anonymous user accounts, disable root logins outside of localhost, remove test databases and reload privileges. It is recommended that you answer ‘yes’ to these options to secure the database server.

Step 4: Installing PHP

PHP is a widely used scripting language suited for web development. By default PHP 5.4 is available in the CentOS 7 Yum repositories. It is now outdated and no longer supported. We recommend that you install the latest stable version of PHP 7.x as it has many improvements and new features.

To install the latest PHP version we need to add REMI repository. Lets start by installing the package “yum-utils” for configuring yum repositories and install the REMI repository.

yum install yum-utils http://rpms.remirepo.net/enterprise/remi-release-7.rpm -y

Configure the repository for installing PHP 7.3 (or the latest stable version) using the following command. If there is a later version then simply modify the command by changing the version numbers in the command to match the release you wish to install.

yum-config-manager – enable remi-php73

We can now install PHP 7.3 along with its common dependencies with the command below,

yum install php php-mysql php-fpm -y 

Check the PHP version with the command below,

php -v

The output should look similar to what show below,

PHP 7.3.2 (cli) (built: Feb 5 2019 13:10:03) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.2, Copyright (c) 1998-2018 Zend Technologies

By default PHP-FPM is configured to user “apache” on port 9000. W’ll need to change the user to nginx.

To do so edit the PHP-FPM configuration file at “/etc/php-fpm.d/www.conf” to what’s shown below.

...
user = nginx
...
group = nginx
...
listen = /run/php-fpm/www.sock
...
listen.owner = nginx
listen.group = nginx

Save the file and change the ownership of the directory “/var/lib/php” from “apache” to “nginx”.

chown -R root:nginx /var/lib/php

Start and enable PHP-FPM.

systemctl start php-fpm
systemctl enable php-fpm

Step 5: Configuring Nginx to Process PHP Pages

Create a new NGINX configuration file with the following command.

vim /etc/nginx/conf.d/default.conf

Paste the syntax below on to the newly created configuration file.

server {
    listen   80;
    server_name  your_server_ip;

    # note that these lines are originally from the "location /" block
    root   /usr/share/nginx/html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Note: Replace “your_server_ip” with your actual server IP.

Save the file and restart NGINX for the changes to take effect.

systemctl restart nginx

Conclusion

Now that you have successfully installed LEMP stack, you are now ready to host any type of dynamic website/software applications.