Linux Enterprise Operation and maintenance - nginx+php

1.PHP source code compilation

(1)php Download

Can be in http://www.php.net/ ##Download from the official website


Here I go directly to the host to get the software package and unzip it
tar jxf php-7.4.12.tar.bz2
If the decompression fails, install bzip2

(2) Software compilation

. / configure configuration
make compilation
make install install
Compilation Trilogy

./configure --prefix=/usr/local/lnmp/php --with-config-file-path=/usr/local/lnmp/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-curl --with-iconv --with-mhash --with-zlib --with-openssl --enable-mysqlnd --with-mysqli --with-pdo-mysql --disable-debug --enable-sockets --enable-soap --enable-inline-optimization --enable-xml --enable-ftp --enable-gd --enable-exif --enable-mbstring  --enable-bcmath --with-fpm-systemd

When configuring, follow the prompts to install the missing installation package
If you are not sure whether the configuration is successful, you can enter
echo $?
If the output is 0, it is successful

yum install -y systemd-devel
yum install -y libxml2-devel
yum install -y sqlite-devel
yum install -y libcurl-devel
yum install libpng-devel -y
yum install oniguruma-devel -y
 These installation packages are generally missing, and different system installation packages are different

The configuration is successful. Start compiling and installing
make compiled successfully
make install installation complete

2. Configuration of PHP FPM file

Copy the configuration php-fpm.conf and www.conf files

cd /usr/local/lnmp/php/etc/  
cp php-fpm.conf.default php-fpm.conf  #Copy file
vim php.fpm.conf   #Edit profile
cd php-fpm.d/
cp www.conf.default www.conf

In the php.fpm.conf file, uncomment this line
Copy php.ini file

cd /root/php-7.4.12/
cp php.ini-production /usr/local/lnmp/php/etc/php.ini  #Copy to specified file
cd /usr/local/lnmp/php/etc
vim php.ini   #Edit the file and change the time zone to Asia Shanghai

Configure php-fpm.service

cd /root/php-7.4.12/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system  #Place the php service file in the specified directory
cd /usr/lib/systemd/system
ls
vim php-fpm.service  
\\\
#ProtectSystem=full    #Comment this line
\\\
systemctl daemon-reload  #Overload service
systemctl start php-fpm.service  #open

3.nginx+php-fpm

(1) Modify the configuration file of nginx

vim /usr/local/nginx/conf/nginx.conf
Uncomment these lines of php service and comment fastcgi_param line, the last fastcgi file is changed to fastcgi.conf

Write a php release file and restart nginx

cd /usr/local/nginx/html/
vim index.php
///
<?php
phpinfo()
?>
///
nginx -s reload

Visit server1 in the real browser to see the php page

(2) Add environment variable

cd /usr/local/lnmp/php/bin/
pwd			##/usr/local/lnmp/php/bin
cd
vim .bash_profile	##In. Bash_ Add file path to profile file
///
PATH=$PATH:$HOME/bin:/usr/local/lnmp/php/bin
///
source .bash_profile 		##Make it effective
which php					##/usr/local/lnmp/php/bin/php
which phpize				##/usr/local/lnmp/php/bin/phpize
echo $PATH

4. Add memcache module in php

(1) Software download

Download the memcache-4.0.5.2.tgz package
Unzip tar zxf memcache-4.0.5.2.tgz

(2) Software installation

Switch to the memcache-4.0.5.2 directory
Execute phpize
Download dependent software
Execute phpize again

yum install autoconf -y
yum install automake.noarch -y
phpize			##Extension succeeded


Compile the memcache source code, and follow the three steps of configure – make – make install

./configure --enable-debug
make
make install

Edit php.ini, restart the service, and execute php -m to see memcache

cd /usr/local/lnmp/php/etc/
ls
vim php.ini 
php -m | grep memcache	  ##The presence of memcache indicates that the addition is successful
systemctl reload php-fpm.service 
php -m	  ##Test whether the php service is normal. You can see memcache

vim php.ini

3. Build nginx cache and add memcache function module

MEMC nginx and srcache nginx modules are used to build an efficient and transparent cache mechanism.
If you need to cache, nginx can skip php and store directly to memcache, but it can only do static storage. If you need dynamic storage, you still need to call php. Therefore, the two caching strategies are carried out at the same time


Install memcached, start the service, and view the port

yum install -y memcached
systemctl start memcached.service
netstat -antlp					##11211 port
cat /etc/sysconfig/memcached	##11211 port


cat /etc/sysconfig/memcached

Switch the environment to the memcache directory, copy and compile the files, and finally restart the service

cd /root/memcache-4.0.5.2/
ls
cp example.php /usr/local/nginx/html/
cp memcache.php /usr/local/nginx/html/
cd /usr/local/nginx/html/
ls
vim memcache.php
///
define('ADMIN_PASSWORD','westos');      // Admin Password 				## Set password
$MEMCACHE_SERVERS[] = '172.25.7.1:11211'; // add more as an array				
#$MEMCACHE_SERVERS[] = 'mymemcache-server2:11211'; // add more as an array 		## Comment this line
///
nginx -s reload				##Restart nginx
systemctl start php-fpm.service	##Open service
systemctl start memcached.service  ##Open service

vim memcache.php is shown in the figure below:

Test in the authentic browser and visit 172.25.7.1/example.php

At this point, the service configuration is successful. Visit 172.25.7.1/memcache.php
Login user name: memcache
Login password: westos


At this point, we can see that the current information processing rate can not reach 100%, which needs to be optimized.

Execute the pressure test command in the real machine and optimize it

ab -c20 -n 1000 http://172.25.7.1/example.php


Refresh the page and you can see that the information processing rate has reached 100% after the stress test of the real machine

5. Configure the php loading module openresty

Based on openresty (build an efficient and transparent cache mechanism) access, the cache can be placed in nginx, which is faster

Note: nginx -s stop ## first stop nginx

Download module

Official download address: https://openresty.org/cn/

After downloading, unzip, compile and install

tar zxf openresty-1.19.3.1.tar.gz  
cd openresty-1.19.3.1/
./configure --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio
make
make install

Configuration module file information

Edit the configuration file, copy the configuration file, and restart the module

cd /usr/local/openresty/nginx.conf
vim nginx.conf
///
user  nginx;
worker_processes  auto;

events {
    worker_connections  65535;
}

#Uncomment the php lines
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }
///
cd ..
cd html/
cp /usr/local/nginx/html/example.php .		#Copy profile
cp /usr/local/nginx/html/index.php .   #Copy
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx -s reload


At this time, the pressure test is carried out in the real machine

Further configuration, feel the change of speed

cd /usr/local/openresty/nginx/conf
vim nginx.conf
///
http {
        upstream memcache {
        server 127.0.0.1:11211;
        keepalive 512;					##Keep 512 connections that don't close immediately to improve performance
        }

    include       mime.types;
    default_type  application/octet-stream;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        location /memc {
        internal;						##Indicates that only internal access is accepted
        memc_connect_timeout 100ms;
        memc_send_timeout 100ms;
        memc_read_timeout 100ms;
        set $memc_key $query_string;	##Use the built-in $query_string as key
        set $memc_exptime 300;			##Indicates the cache expiration time
        memc_pass memcache;
        }
///
/usr/local/openresty/nginx/sbin/nginx -t
/usr/local/openresty/nginx/sbin/nginx -s reload

vim /usr/local/openresty/nginx/conf/nginx.conf


Perform the pressure test again in the real machine
ab -c10 -n 5000 http://172.25.7.1/example.php
ab -c10 -n 5000 http://172.25.24.1/index.php

##It can be compared that the speed becomes faster and the transmission capacity is greatly improved

Tags: PHP Linux Nginx

Posted on Sun, 05 Sep 2021 13:46:45 -0400 by grissom