Install Nginx, MySQL, PHP 7 – LEMP on CentOS 7

0
93

Install Nginx, MySQL, PHP 7 – LEMP on CentOS 7. Your project requires PHP 7, such as Laravel high versions. You are looking for ways to install PHP 7, MySQL with Nginx and CentOS 7. This article will guide you to install and configure LEMP on CentOS 7.

  • CentOS 7
  • MySQL 5.6
  • PHP 7.2
  • Nginx 1.15

These configurations are like posts Deploy NodeJS Application on CentOS 7

Let’s start.

1. Install Nginx

Open /etc/yum.repos.d/nginx.repo

vim /etc/yum.repos.d/nginx.repo

and add code

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/mainline/rhel/7/$basearch/
gpgcheck=0
enabled=1

Install nginx

sudo yum install -y nginx

Start nginx and enable start with server

sudo systemctl start nginx
sudo systemctl enable nginx

Open firewall allow http, https

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

2. Install PHP 7.2

First, turn off SELINUX. Open /etc/sysconfig/selinux and change SELINUX to:

SELINUX=disabled

Reboot server. Then install PHP, PHP-FPM and commons Modules

Install packages:

sudo yum install -y yum-utils
sudo yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum-config-manager --enable remi-php72

Install PHP and modules:

sudo yum install -y php72 php72-php-fpm php72-php-gd php72-php-json php72-php-mbstring php72-php-mysqlnd php72-php-xml php72-php-xmlrpc php72-php-opcache

Start and Enable PHP-FPM:

sudo systemctl start php72-php-fpm
sudo systemctl enable php72-php-fpm

Check php version:

php72 -v

You can link php72 command to php command by this:

ln -s /usr/bin/php72 /usr/bin/php

Now you can use command with php

php -v

3. Configure Nginx with PHP

Configure PHP-FPM to work with Nginx.

vim /etc/opt/remi/php72/php-fpm.d/www.conf

Change user=apache and group=apache to nginx like this:

; RPM: apache user chosen to provide access to the same directories as httpd
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx

Restart php-fpm service:

sudo systemctl restart php72-php-fpm

4. Configure VirtualHost

Same as Deploy NodeJS Application on CentOS 7 article.

First create files:

  • block.conf in the /etc/nginx/conf.d/ directory. block.conf: block files/folders not accessible
  • staticfiles.conf in the /etc/nginx/conf.d/ directory.
  • php.conf in the /etc/nginx/conf.d directory.
# /etc/nginx/conf.d/block.conf
location = /robots.txt  { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; expires 30d; }
location ~ /\.          { access_log off; log_not_found off; deny all; }
location ~ ~$           { access_log off; log_not_found off; deny all; }
location ~ /\.git { access_log off; log_not_found off; deny all; }
location = /nginx.conf { access_log off; log_not_found off; deny all; }
# /etc/nginx/conf.d/staticfiles.conf
location ~* \.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|eot|svg|ttf|woff)$ {
    gzip_static off;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;
    expires max;
    break;
}

location ~* \.(css|js)$ {
    #add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;
    expires 30d;
    break;
}
# /etc/nginx/conf.d/php.conf
# pass PHP handle to PHP-FPM
location ~* \.php$ {
    fastcgi_index   index.php;
    fastcgi_pass    127.0.0.1:9000;
    #fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
    include         fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;

    # Timeout for proxy
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 512k;
    fastcgi_buffers 512 16k;
    fastcgi_busy_buffers_size 512k;
    fastcgi_temp_file_write_size 512k;
    fastcgi_intercept_errors on;
}

Next, create new directory: domains at /etc/nginx and add new default.conf

# /etc/nginx/domains/default.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    root /usr/share/nginx/html;
    location / {
        rewrite ^/(.*)$ /index.php?url=$1 last;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

        include /etc/nginx/conf.d/php.conf;
        include /etc/nginx/conf.d/staticfiles.conf;
        include /etc/nginx/conf.d/block.conf;

}

Save and open /etc/nginx/nginx.conf and find this line:

include /etc/nginx/conf.d/*.conf;

Replace it to:

include /etc/nginx/domains/*.conf;

Next, find this line:

access_log  /var/log/nginx/access.log  main; // you can comment this line for save storage

Add below it:

index index.php  index.html index.htm;

Now, you can restart nginx:

sudo systemctl restart nginx

5. Add domain

This step will repeat every time you want to add a new domain.

Create nginx directory in /home/.

mkdir /home/nginx

In each directory of the domain, we need to have 2 more directories including:

  • log/ – To store log files for each domain. There will be a file named error.log.
  • public_html/ – Directory containing the code of your web.

Example with domain: abcxyz.com

mkdir -p /home/nginx/abcxyz.com/log
touch /home/nginx/abcxyz.com/log/error.log
mkdir -p /home/nginx/abcxyz.com/public_html
chown -R nginx:nginx /home/nginx

Copy the file default.conf to abcxyz.com.conf located in the same directory (/etc/nginx/domains/).

cp /etc/nginx/domains/default.conf /etc/nginx/domains/abcxyz.com.conf

Open /etc/nginx/domains/abcxyz.com.conf and find:

server_name  127.0.0.1;

Change it to:

server_name  www.abcxyz.com abcxyz.com;

Next, find:

#access_log  /var/log/nginx/log/host.access.log  main;

Add below:

error_log /home/nginx/abcxyz.com/log/error.log error;

Next, find:

root   /usr/share/nginx/html;

Change it to:

root   /home/nginx/abcxyz.com/public_html; # server will read index file in this root

Save it. Then CHMOD folders and files as follows: 644 for files, 755 for directories.

find /home/nginx/abcxyz.com/public_html '(' -type f -exec chmod 644 {} ';' ')' -o '(' -type d -exec chmod 755 {} ';' ')'

Now restart nginx.

sudo systemctl restart nginx

You should see more this article: Add Let’s Encrypt SSL certificate for Nginx website

Thank you for reading.

Leave a Reply

avatar