Have a Question?

If you have any question you can ask below or enter what you are looking for!

Installing Linux, Nginx, MySQL, PHP (LEMP) stack on CentOS

Nginx

LEMP stack is similar to that of LAMP, but it is considered as more fast than the LAMP stack.

Installing Nginx

1. To get the latest Nginx version, you should first install the EPEL repository, which contains additional software for the CentOS:

sudo yum install epel-release

You’ll be prompted to confirm that you want to continue the installation [Y/N]

2. Once the Nginx archive is installed on your server, you’ll need to install Nginx using the following command:

sudo yum install nginx

3. After installing Nginx, you need to enable it to start during the boot-up. You can use the following commands to start, stop, and enable Nginx:

sudo systemctl start nginx
sudo systemctl stop nginx
sudo systemctl enable nginx

4. Make sure that Nginx has been installed by opening your preferred browser and typing in the IP address of your server in the address bar. (e.g., http://98.76.54.321). You should see a page like this:

In case you aren’t sure what your server’s IP address is, run this command and it should then be displayed:

ifconfig eth0 | grep inet | awk ‘{ print $2 }’

or just run:

ip a

and you will find the whole detailed network list.

MySQL

The LEMP CentOS installation frequently runs MariaDB, the commercially supported fork of MySQL, and it is a backward-compatible replacement for MySQL.

Installing MariaDB

1. To install MariaDB on your CentOS server, you need to run the following command:

sudo yum install mariadb-server mariadb

2. When the installation is complete, we need to start MariaDB with the following command:

sudo systemctl start mariadb

3. Run the installation script to setup MySQL:

sudo mysql_secure_installation

You’ll be asked if you want to change your root password. Type ‘N’ since you have already set a root password previously. There will be another series of prompts; the easiest way to answer is [Y] to all of them.

4. Now you need to enable MariaDB to start on boot. Use the following command to do so:

sudo systemctl enable mariadb

PHP

PHP is a widely-used server-side scripting language designed for web development and is also used as a general-purpose programming language.

Installing PHP

1. To install PHP, type this command:

sudo yum install php php-mysql php-fpm php-gd php-xml php-mbstring -y

2. Now edit the php-fpm config file:

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

Find the following line:

listen = 127.0.0.1:9000

change it to this:

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

then find the following two lines:

;listen.owner = nobody
;listen.group = nobody

remove the preceding semicolons. Lastly, change the user and group value from “apache” to “nginx”:

user = nginx
group = nginx

3. To initially start the PHP service to run, enter in the following command:

sudo systemctl start php-fpm

For enabling the PHP service to start automatically, so you don’t have to type the command above every time the server restarts, use this command here:

sudo systemctl enable php-fpm


Configure Nginx Virtual Host

1. Create a new virtual host file in /etc/nginx/conf.d directory:

sudo nano /etc/nginx/conf.d/example.com.conf

Replace www.example.com and example.com with your own domain. Don’t forget to set A record for your domain name.

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

2. Save and close the file. then reload nginx:

sudo systemctl reload nginx


Testing PHP Processing

Create a file to test:

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

Type this in the document:

<?php
phpinfo();
?>

Save the document by pressing Control+X, then Y and Enter.
To check that PHP is working, type in the IP of your server plus ‘/info.php’.
For example, if your IP was 12.34.56.789, you must type in

12.34.56.789/info.php

You’ll see a page similar to this:

Congratulations – your LEMP system should now be fully functional!
You are now able to upload your sites files to the /usr/share/nginx/html directory or another directory if you update the Nginx config (located at /etc/nginx/nginx.conf).

Leave a Reply

You must be logged in to post a comment.