Skip to content

Instantly share code, notes, and snippets.

@kurtisdunn
Created May 26, 2019 23:08
Show Gist options
  • Save kurtisdunn/230072767fc36bbfc89f51476da22cc7 to your computer and use it in GitHub Desktop.
Save kurtisdunn/230072767fc36bbfc89f51476da22cc7 to your computer and use it in GitHub Desktop.
Install LEMP stack (Nginx, MariaDB, PHP-FPM) on CentOS7

Install LEMP stack (Nginx, MariaDB, PHP-FPM) on CentOS7

Install Nginx

Nginx is a fast web server compared to Apache and becomes more popular these days. In order to install Nginx on CentOS 7, we need to add the EPEL repository using the following command. EPEL stands for Extra Packages for Enterprise Linux.

sudo yum install epel-release -y

# Type your password. Now that the repository is added, it’s time to install Nginx:

sudo yum install nginx -y

# After Nginx is installed, we need to start it.

sudo systemctl start nginx

# Enable Nginx to start at system boot time.

sudo systemctl enable nginx

# Check if it’s running:
nginx -v
#nginx version: nginx/1.12.2

Install MariaDB

MariaDB is a drop-in replacement for MySQL. Install it using following command:

sudo yum install mariadb-server mariadb -y

# After it’s installed, we need to start it.
sudo systemctl start mariadb

# Enable MariaDB to start at system boot time.
sudo systemctl enable mariadb

# Check status:
mysql --version
#mysql  Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1

# Now we need to run the security script.
sudo mysql_secure_installation

Install PHP-FPM

Install PHP and related packages using the following command:

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

# Now edit the php-fpm config file:
sudo vi /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

# Once you save and close the file, start the PHP Processor:
sudo systemctl start php-fpm

# Next, enable php-fpm to start on system boot.
sudo systemctl enable php-fpm

Configure Nginx Virtual Host

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

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

# Add the following lines to it. 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 www.example.com example.com;
  root /usr/share/nginx/html;
  index index.php index.html index.htm;
  location / {
    try_files $uri $uri/ /index.php$query_string;
  }
  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. then reload nginx:
sudo systemctl reload nginx

Test PHP Processing

# Create a  `info.php`  file in the web root directory:
sudo vi /usr/share/nginx/html/info.php

# Put the following text into the file
<?php phpinfo(); ?>

Save and close it. Then visit you newly created file by typing the following in your browser:

http://example.com/info.php

or

your ip address/info.php

If you will see something like in the following screenshot, then your PHP is working correctly.

LEMP On CentOS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment