Saturday, September 24, 2022

How to install LEMP stack on Centos 7

In this article, we want to teach you how to install the LEMP stack on centos 7.

LEMP is short for Linux, Nginx, MySQL, and PHP. and we use MariaDB instead of MySQL.

How to install LEMP stack on Centos 7

To install LEMP on Centos 7 you need to log in as a non-root user with root privileges that can run Sudo commands. follow our article about the Initial server setup with Centos 7.

Install Nginx on Centos 7

For installing Nginx you should get the latest version of Nginx. So for that install centos 7 EPEL repository with the following command:

sudo yum install epel-release

Now install Nginx on Centos 7 with the command below:

sudo yum install nginx

Then start the Nginx service with the following command:

sudo systemctl start nginx

Type your public IP address in your web browser now you can see the default centos 7 Nginx web page.

You can get your public IP address with the following command:

curl http://icanhazip.com

Enable Nginx to start on boot with the following command:

sudo systemctl enable nginx

Install MariaDB on Centos 7

To install LEMP on Centos 7 you need MariaDB. You can install MariaDB and then start it with the following commands:

sudo yum install mariadb-server mariadb
sudo systemctl start mariadb

Now you need to run a security script for your database.

sudo mysql_secure_installation

Here you will be asked for your current root password press enter to leave it blank then you will be asked to set a root password enter Y and set your password.

Enable MariaDB to start on boot with the following command:

sudo systemctl enable mariadb

Install PHP on Centos 7

Here to complete the installation of the LEMP stack on Centos 7 You need to install the REMI repository for centos 7 for the up-to-date PHP releases. Run the following command:

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

Then enable the repository and check the available packages:

yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64

In your output you will see the available packages choose the most updated version of PHP and run the following command:
Here we choose PHP74.

sudo yum-config-manager --enable remi-php74

Now install PHP for the LEMP stack with the following command:

sudo yum install php php-mysqlnd php-fpm

To see that PHP is available as your chosen version, run:

php –version

At this point, you need to make some changes to the default configuration. Open the file with the vi or your favorite editor:

sudo vi /etc/php-fpm.d/www.conf

Find the user and group in the file. Then change the apache to Nginx for them.

Next, locate the listen directive and change the line to the following:

listen = /var/run/php-fpm/php-fpm.sock;

Finally, you need to look for “listen.owner”, “listen.group” and “listen.mode” directives. Uncomment them by removing the preceding “;” sign at the beginning of the line. Then, change the owner and group to Nginx.

Save and close the file.

Enable and start the PHP-fpm with the following command:

sudo systemctl start php-fpm

Configure Nginx to Process PHP Pages

At this point when you have finished the installation of PHP for the LEMP stack on Centos 7, you need to tell Nginx to use your PHP processor for dynamic content.

Create a new file as a default PHP website in the /etc/nginx/nginx.conf file.

Open a new file in the /etc/nginx/conf.d directory with the following command:

sudo vi /etc/nginx/conf.d/default.conf

Copy the following PHP server definition block to your configuration file:

server {
listen 80;
server_name server_domain_or_IP;

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;
}
}

Save and close the file.

Next, restart Nginx to apply the changes:

sudo systemctl restart nginx

Test PHP Processing on your Web Server

Now that the LEMP stack is installed on Centos 7, we can create a test PHP script to make sure Nginx is correctly handling .php scripts with the help of PHP-fpm.

Before this, you need to change the default ownership settings on Nginx’s document root.
Run the following command:

sudo chown -R orca.orca /usr/share/nginx/html/

Then create a new PHP file called info.php at the /usr/share/nginx/html directory:

vi /usr/share/nginx/html/info.php

Copy the below commands into your file:

<?php
phpinfo();

Save and close the file.

Now you can test your web server by typing your IP address or server host in your web browser.

http://server_host_or_IP/info.php

After checking information about your PHP server through that page, it’s better to remove the file you created as it contains sensitive information about your PHP environment and your CentOS server. You can use rm to remove that file:

rm /usr/share/nginx/html/info.php

Hope you enjoy this article about how to install the LEMP stack on Centos 7.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular