Centos7/8 installation of nginx php7 X Mysql8. 0

Posted by prashant0812 on Mon, 03 Jan 2022 17:01:13 +0100

The tutorials in this regard can be said to be a large number of searches, but they all have some defects. In practice, all kinds of pits are in order to step on fewer pits. This article will present the most comprehensive building tutorial for novices.

Installation environment

  • Nginx1.2
  • PHP7.x
  • Mysql8.0

Installing Nginx

To add a CentOS 7 EPEL repository, run the following command:

yum install epel-release

Installing Nginx

yum install nginx

Start Nginx service

systemctl start nginx

Power on

sudo systemctl enable nginx

Install mysql8 0

Add mysql Library

yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

Install mysql

yum --enablerepo=mysql80-community install mysql-community-server

mysql start

systemctl start mysqld.service

Change root password Because in mysql8, the default password will be set when installing the database. We can use the following command to view the default password

grep 'A temporary password is generated for root@localhost' /var/log/mysqld.log |tail -1

Initialize database

sudo mysql_secure_installation

You will be prompted to enter the root password, which is the default password obtained in the previous step. Then enter the root password again and enter Y all the way. I don't know why the modified program can't connect and log in. Use the following command to modify the root password again.

mysql -u root -p
use mysql;
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'youpassword';
systemctl restart mysqld #Restart MySQL

Install PHP

Installing the Remi repository

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

Check the PHP 7 + version available in the Remi repository

yum --disablerepo="*" --enablerepo="remi-safe" list php[7-9][0-9].x86_64

You will see this output

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * remi-safe: mirrors.ukfast.co.uk
Available Packages
php70.x86_64                                              2.0-1.el7.remi                                       remi-safe
php71.x86_64                                              2.0-1.el7.remi                                       remi-safe
php72.x86_64                                              2.0-1.el7.remi                                       remi-safe
php73.x86_64                                              2.0-1.el7.remi                                       remi-safe
php74.x86_64                                              1.0-3.el7.remi                                       remi-safe
php80.x86_64                                              1.0-3.el7.remi  

Enable the corresponding PHP Version (take 7.4 as an example)

sudo yum-config-manager --enable remi-php74

Install php

yum -y install php php-mysqlnd php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc php-zip php-cli php-fpm php-gd php-mysqlnd php-mbstring php-opcache php-pdo php-json

configuration file

Configuration PHP FPM file

vim /etc/php-fpm.d/www.conf The user and group variables are apache by default. We need to change these to nginx Find listen PHP FPM, which listens to specific hosts and ports over TCP. We want to change this setting to listen to local socket files because it will improve the overall performance of the server.

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

Finally, we need to change the owner and group settings of the socket file we just defined in the listen directive. Find listen owner,listen. Group and listen Mode instruction. These lines are commented out by default. By deleting; Uncomment with the leading symbol at the beginning of the line. Then, change the owner and group to nginx:

listen.owner = nginx
listen.group = nginx
listen.mode = 0660

Finally, restart PHP FPM

systemctl start php-fpm

Configure Nginx to handle PHP

The current configuration is unable to access php files. We still need a simple configuration. Nginx has a dedicated directory where we can define each managed site as a separate configuration file using a server block. This is similar to Apache's virtual host. In the default installation, this directory is empty. We will create a new file as the default PHP website on this server, which will overwrite / etc / nginx / nginx The default server block defined in the conf file. Open a new file in the / etc/nginx/conf.d directory

vim /etc/nginx/conf.d/default.conf
#The contents are as follows:
server {
    listen       80; #The port can be modified by itself, such as 8080
    server_name  192.168.x.x; #If there is no domain name, fill in the public IP or intranet IP directly

    root   /var/www/html; #Website Lu Jin
    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 /var/www/html; #Website Lu Jin
#php configuration section
    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;

Restart Nginx to apply changes:

sudo systemctl restart nginx

Configure site directory user groups and permissions

At present, both php and nginx run as nginx, and sometimes the permissions of our directory are owned by root. So visit the website will appear 404. First, check the user groups of php and ngxin

ps -ef | grep php
ps -ef | grep nginx

Finally, modify the user group permissions of the corresponding directory

chown -R nginx:nginx /var/www/blog

Then restart php and nginx again

Configure PATHINFO

Like Typecho, we need to enable PATHINFO. PATHINFO is not supported by default, so we need to change the configuration file of the host that supports PATHINFO.

location ~ ^(.+\.php)(.*)$ {
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        include        fastcgi_params;

And configure etc / PHP ini


Finally, restart nginx and php There are so many problems at present. Let's talk about the problems later!

Copyright: big cousin xiaoyaozi

Link to this article: https://blog.bbskali.cn/2844.html

Licensed under the Creative Commons Attribution - non-commercial use 4.0 international agreement, reprinting of cited articles shall follow the same agreement.