Configure lnmp environment using yum (CentOS7.6)

Keywords: Linux Nginx PHP MariaDB yum

I. details of installation version
Server: MariaDB
Server version: 5.5.60-MariaDB MariaDB Server
[root@ln-125 ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core)
[root@ln-125 ~]# nginx -v
nginx version: nginx/1.14.2
[root@ln-125 ~]# php-fpm -v
PHP 5.4.16 (fpm-fcgi) (built: Oct 30 2018 19:32:20)
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

II. Install Nginx service
1. Configure the yum source of Nginx

[root@ln-125 ~]# cat >> /etc/yum.repos.d/nginx.repo <<EOF
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/      
#The release is the linux version number centos 7
gpgcheck=0
enabled=1
EOF

2. Install and add power on auto start

yum clean all ;
yum makecache ;
yum list nginx ;
#Then you can see the nginx installation package;
yum install nginx ;
systemctl enable nginx ;
systemctl start nginx 

If there is any installation module that needs to be supplemented, you can download the source package from the official according to the current Nginx version, and then compile the additional module according to the increment of the current version

[root@ln-125 ~]# nginx -V
nginx version: nginx/1.14.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-28) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

III. install php services
Query the current php installation package
yum list php  php-fmp
Why install PHP FPM here?
Because php FPM is the bridge between nginx and php. php FPM (fast process management), the default process of php FPM is 127.0.0.1:9000. After the installation of php and php FPM, you need to configure the configuration file of nginx to meet the php request of the client, which is to forward it to php FPM (127.0.0.1:9000), php FPM to complete the php parsing, and finally to nginx
1. installation

yum install -y php php-fpm php-pear php-devel #httpd 
#httpd is optional. In parameter update, php pear is an extension tool of php. After installation, you can use the pecl install command to install php extension

2. Configure Nginx to support php files
The default Nginx is to process html and htm files. Configure Nginx to support php

vim /etc/nginx/conf.d/default.conf 
...
    location / {
        root   /usr/share/nginx/html;  #Set the absolute path to the root
        index  index.html index.htm index.php;  #Match php file
    }
    location ~ \.php$ {      #It was originally commented out and needs to be opened or copied
        root           /usr/share/nginx/html;  #Set absolute path
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;  #Set root match
        include        fastcgi_params;
    }
...

3. Set the unix sock mode communication of php (skip this step)
The default configuration file is to listen to the 9000 port for communication. For small and single servers without debt balance, you can use unix sock to increase php response speed

touch /dev/shm/php-fpm-default.sock
[root@ln-125 ~]# cat /etc/php-fpm.d/www.conf |grep -Ev '^;|^$'
[www]
listen = /dev/shm/php-fpm-default.sock
listen.backlog = -1
listen.allowed_clients = 127.0.0.1
listen.owner = nobody
listen.group = nobody
listen.mode = 0666
user = nginx
group = nginx
. . . 
systemctl restart php-fpm.service
systemctl enable php-fpm

4. Optimize the configuration (optional)

A) modify php.ini Configuration
vim /etc/php.ini 
cgi.fix_pathinfo=1 #Remove the comments and enable the pathinfo pseudo static function of PHP.
max_execution_time = 0  #Maximum time for script to run, 30 seconds by default
max_input_time = 300#The time that script can consume, 60 seconds by default
memory_limit = 256M#The maximum memory consumed by script running. Change the value according to your needs. The default value is 128M
post_max_size = 100M  #For the maximum data submitted in a single form, this item is not limited to the size of the uploaded single file, but to the submitted data of the whole form. Restrictions include all content submitted by the form. For example, when posting a post, the title, content, attachment, etc Default 8M
upload_max_filesize = 10M#Maximum license size of uploaded file, 2M by default

B) modify php-fpm Configuration
vim /etc/php-fpm.d/www.conf 
//Find the following two lines to uncomment
listen.owner = nobody 
listen.group = nobody 
//Find the following two lines and change their apache to nginx
user = apache -> user = nginx 
group = apache -> group = nginx

IV. install mariadb database
yum install -y mariadb mariadb-server

#Boot from boot
[root@ln-125 ~]# systemctl start mariadb.service
[root@ln-125 ~]# systemctl enable mariadb.service
#Initialize database configuration
mysql_secure_installation  #Configure default settings (root password login mode, etc.)
#Set default character set
//Edit vim /etc/my.cnf
[root@ln-125 ~]# grep -Ev '^#|^$' /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
character-set-server = utf8  ##Set default encoding
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d

systemctl restart mariadb.service

Five, test

cat >> /usr/share/nginx/html/index.php << EOF
<?php  
echo   phpinfo();    
?>
EOF

http: / {domain name}
http: / {domain name} / index.php
See the test page, then congratulations on your completion.

END

Posted by ozzythaman on Sun, 08 Dec 2019 01:58:32 -0800