Configure Nginx + PHP-FPM + MySQL on CentOS 7

Requirements

You need to have CentOS 7 with vanilla kernel, you also need about 10 Gigs of free disk space.

Installing the repository

Most of the stuff we need are available in the EPEL repository, so now we are going to install that.

sudo yum install epel-release

You need to be a sudoer to do that.

Installing nginx

sudo yum install nginx

now enable it by doing

systemctl enable nginx.service

and start it by doing

systemctl start nginx.service

now visit your server's ip on a browser, if you see a test page for the Fedora Project and stuff, that means your installation is working.

Install MySQL

First you need to install MySQL yum repo for your platform.

Red Hat Enterprise Linux 7 / Oracle Linux 7 (Architecture Independent), RPM Package

Red Hat Enterprise Linux 6 / Oracle Linux 6 (Architecture Independent), RPM Package

Fedora 29 (Architecture Independent), RPM Package

Fedora 28 (Architecture Independent), RPM Package

Fedora 27 (Architecture Independent), RPM Package

download the package, and install them

sudo rpm -Uvh /path/to/file.rpm

now find all the available repositories

yum repolist all | grep mysql

and select your desired version

sudo yum-config-manager --disable mysql80-community
sudo yum-config-manager --enable mysql<your_version>-community

verify it by executing

yum repolist enabled | grep mysql

now install mysql

sudo yum install mysql-community-server

and start it

sudo systemctl enable mysqld.service
sudo systemctl start mysqld.service

now find your temporary password by running

sudo grep 'temporary password' /var/log/mysqld.log

and run the secure installation

MySQL 5.6:

mysql_secure_installation

Other versions:

mysql -uroot -p
ALTER USER 'root'@'localhost' IDENTIFIED BY '<NewPassword>';

follow the steps and change your password

now we need to create an admin user other than root

login to mysql

mysql -u root -p

enter the password you just set

now you need to create a new user and grant all perms

CREATE USER '<username>'@'%' IDENTIFIED BY '<password>';
GRANT ALL PRIVILEGES ON *.* TO '<username>'@'%';

reload privileges

FLUSH PRIVILEGES;

Install PHP

Installing php is as simple as installing a few packages via yum and editing the config files

sudo yum install php php-mysql php-fpm

next, we need to make php-fpm not assume filenames by doing

sudo echo 'cgi.fix_pathinfo=0' >> /etc/php.ini

next open up /etc/php-fpm.d/www.conf with an editor

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

search up listen = and replace the entire line with

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

next search up listen.owner = and make sure the two lines look like this

listen.owner = nginx
listen.group = nginx

next search up user = and replace apache with nginx

user = nginx
group = nginx

then save the file and exit the editor

now start the php-fpm service

sudo systemctl enable php-fpm.service
sudo systemctl start php-fpm.service

Configure nginx

open up /etc/nginx/conf.d/.conf with an editor

sudo vim /etc/nginx/conf.d/info.conf

paste in these

server {
    listen 80;
    server_name  <your-domain>;
    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;
    }
}

if you want an ssl connection and have a certificate, paste this

server {
    listen 80;
    server_name  <your-domain>;
    return 302 https://$server_name$request_uri;
}
server {
    listen 443 ssl;
    server_name  <your-domain>;
    ssl_certificate /path/to/cert.pem;
    ssl_certificate_key /path/to/privkey.pem;
    ssl on;
    ssl_session_cache builtin:1000 shared:SSL:10m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
    ssl_prefer_server_ciphers on;
    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;
    }
}

now test if your php works by creating a test file

echo '<?php phpinfo(); ?>' > /usr/share/nginx/html/info.php

and go to

http://<your-domain>/info.php

if you see a bunch of boxes and your system information in it, your php is working.

now delete that file for security purposes.

{{ message }}

{{ 'Comments are closed.' | trans }}