Verification: a143cc29221c9be0

Nginx for centos php mysql

Step 1. Install MariaDB

– Install MariaDB:

# sudo yum install mariadb mariadb-server

– Start / Enable MariaDB:

# sudo systemctl start mariadb
# sudo systemctl enable mariadb

– Secure MySQL:

– To make the MariaDB secure by using the mysql_secure_installation command u can take a look to this tutorial: Securing MySQL server / Mariadb with mysql_secure_installation

Step 2. Install Nginx

– Nginx will not be found in the official CentOS repository, so we need to install the EPEL Repository first.

# sudo yum install epel-release 

– Now, install Nginx with following command:

# sudo yum install nginx -y 

– Enable / Start Nginx:

# sudo systemctl enable nginx
# sudo systemctl start nginx

– If you are running a firewall, run the following commands to allow HTTP and HTTPS traffic:

# sudo firewall-cmd --permanent --add-service=http # Allow HTTP Traffic
# sudo firewall-cmd --permanent --add-service=https # Allow HTTPS Traffic
# sudo firewall-cmd --reload # Reload firewalld configuration

Step 3. Install PHP and PHP-FPM

– Below is the command to install PHP with php-fpm and common modules.

# yum install php-fpm php-mysql php-cli php 

– Start / Enable PHP-FPM:

# sudo systemctl enable php-fpm
# sudo systemctl start php-fpm

Step 4. Configuring Nginx to work with PHP-FPM

-1 Editing php.ini File:

– Open the php.ini file and find the cgi.fix_pathinfo directive, uncomment it by removing the ; and set it to 0:

# sudo vi /etc/php.ini

cgi.fix_pathinfo=0

-2 Editing www.conf file:

# sudo vi /etc/php-fpm.d/www.conf

– Find the listen directive (it should be the first), and verify that it is set to listen for PHP traffic using a Unix socket (instead of port 9000):

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

– Find the listen.owner and listen.group directives, uncomment them, and modify them as follows:

listen.owner = nginx
listen.group = nginx

– Find the Unix user/group of processes section, and change the user and group from apache to nginx:

user = nginx
group = nginx

– Save and close the file:

3- Restart PHP-FPM:

# sudo systemctl restart php-fpm

4- Create a new Nginx configuration file:

# sudo vi /etc/nginx/conf.d/default.conf

– Paste the following code into this file:

server {
    listen	 80;
    server_name  your_server_ip;

    # note that these lines are originally from the "location /" block
    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;
    }
}

– Save and close the file:

– Restart Nginx:

# sudo systemctl restart nginx