How to install LEMP(Nginx PHP FastCGI MariaDB) in CentOS

How to install LEMP(Nginx PHP FastCGI MariaDB) in CentOS

Make sure your system is up to date:

yum update

Install Nginx from EPEL

The fastest and easiest way to install Nginx is to use a third-party source EPEL

sudo yum install epel-release
yum update
yum install nginx

These commands refer to installing the EPEL repository, pulling the latest metadata, and then installing Nginx
Configure Nginx
After the Nginx installation is complete, it needs to be activated and started on systemd. You can use the systemctl command to do this.

systemctl enable nginx.service
systemctl start nginx.service

You can then check its status to ensure that nginx is always started

systemctl status nginx.service

Configure Nginx virtual host
Once nginx is installed, you need to configure the server block in the configuration file. Each server block requires a server and location directive. You can create separate files for the server block, or put them directly into a file /etc/nginx/nginx.conf. In this example, we use multiple configuration files. By default, Nginx will reference the .conf file in the /etc/nginx/conf.d directory.

/etc/nginx/conf.d/example.com.conf:

server {
listen 80;
server_name www.example.com example.com;
access_log /var/www/example.com/logs/access.log;
error_log /var/www/example.com/logs/error.log;

location / {
root /var/www/example.com/public_html;
index index.html index.htm index.php;
}
}

When you want to host additional websites, you can put the configuration file in the /etc/nginx/conf.d directory. Once the configuration file is created, you need to create a directory for your public html file and log directory:

mkdir -p /var/www/example.com/{public_html,logs}

After configuring the file virtual host, you need to restart nginx for the configuration to take effect

systemctl restart nginx.service

Deploying PHP FastCGI
If your website is written in PHP, you need to implement PHP-FastCGI in order for Nginx to properly process and parse PHP code. You can install using YUM from EPEL repository

yum install php-cli php spawn-fcgi

After PHP-FastCGI is installed, you need to create a script to start and control the php-cgi process.

/usr/bin/php-fastcgi:

#!/bin/sh
if [ `grep -c “nginx” /etc/passwd` = “1” ]; then
FASTCGI_USER=nginx
elif [ `grep -c “www-data” /etc/passwd` = “1” ]; then
FASTCGI_USER=www-data
elif [ `grep -c “http” /etc/passwd` = “1” ]; then
FASTCGI_USER=http
else
# Set the FASTCGI_USER variable below to the user that
# you want to run the php-fastcgi processes as

FASTCGI_USER=
fi

/usr/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 6 -u $FASTCGI_USER -f /usr/bin/php-cgi

After the script is created, set it to have executed permissions

chmod +x /usr/bin/php-fastcgi

Configure PHP-FastCGI as a service
After PHP-FastCGI is installed, it does not automatically register as a service with systemd. If you want to manage PHP-FastCGI more conveniently with systemd, you can configure it as a systemd service. Need to create a service file pointing to / usr / bin / php-fastcgi:

/etc/systemd/system/php-fastcgi.service:

[Unit]
Description=php-fastcgi systemd service script

[Service]
Type=forking
ExecStart=/usr/bin/php-fastcgi start

[Install]
WantedBy=multi-user.target

After the service file is created, you need to reload the systemd daemon to activate the service and then start it.

systemctl daemon-reload
systemctl enable php-fastcgi.service
systemctl start php-fastcgi.service

Install MariaDB
The official source of CentOS 7 is no longer MySQL, and MariaDB has been replaced, but rest assured that this is fully compatible with MySQL and the usage is the same.
Install MariaDB from official sources

yum install mariadb-server

After installation is complete, use systemctl to activate and start mariadb

systemctl enable mariadb.service
systemctl start mariadb.service

Use mysql_secure_installation command to configure MariaDB
mysql_secure_installation