FCGI mode compilation installation LAMP + XCACHE (build BBS Forum)

PHP’s working mode:

php has three working modes in the lamp environment: CGI mode, Apache module , Fastcgi mode. The performance of running PHP in CGI mode is not very good, and it runs as a module of Apache. The difference between the FastCGI method and the Apache module is: FastCGI PHP is an independent process, all sub-processes are managed by a component of PHP called php-fpm, while Apache runs PHP in a modular way. Apache is responsible for calling php to complete the work. The performance of PHP’s FastCGI method is much stronger than that of apache’s modular method. In this chapter, we will compile and install LAMP in the FastCGI method.


FastCGI working mechanism:
First, the client initiates a request. There are two types of requests. One is a static request, which can be directly returned by Apache; the other is a dynamic request, such as It contains script interpretive languages ​​such as php or Perl. The Apache server calls the php server to execute through the fastcgi protocol and returns to Apache. Apache returns the results of the interpretation and execution. If the process involves data manipulation, at this time The php server will also call the mysql server through the mysql protocol


Compilation environment and various software versions:
< /span>


This test requires at least three servers, the operating system is Centos7.4, and the network segment is in 192.168.146.0/ 24 The gateway 192.168.146.2 is allocated as follows:

One httpd server (192.168.146.100)

a php server (192.168.146.110)

One Mysql server (192.168.146.120)

Turn off the fire protection of all hosts during the test Wall

Compile and install LAMP

When installing Apache, you can refer to this article of mine: http://blog.csdn. net/qq_39591494/article/details/78726580

At the same time, you can also try a one-click installation script to install, as follows:

#!/bin/bash
#this ia a yankerp

systemctl stop firewalld.service && setenforce 0

rm -rf /var/run/yum.pid && systemctl stop firewalld ;setenforce 0 && yum install gcc gcc-c++ -y
wget http://mirrors.hust.edu.cn/apache//apr/apr-1.5.2.tar.gz
tar zxf apr-1.5.2.tar.gz ;cd apr-1.5.2/ ;. /configure –prefix=/usr/local/apr && make && make install ;cd
wget http://mirrors.hust.edu.cn/apache//apr/apr-util-1.5.4.tar. gz
tar zxf apr-util-1.5.4.tar.gz && cd apr-util-1.5.4/ ;./configure –prefix=/usr/local/apr-util –with-apr=/ usr/local/apr ;make && make install ;cd
wget http://www.zlib.net/fossils/zlib-1.2.8.tar.gz
tar zxf zlib-1.2.8.tar. gz && cd zlib-1.2.8/ ;./configure –prefix=/usr/local/zlib && make && make install ;cd
wget https://ftp.pcre.org/pub/pcre/pcre- 8.39.tar.gz
tar zxf pcre-8.39.tar.gz && cd pcre-8.39/ ;./configure –prefix=/usr/local/pcre && make;make install ;cd
wget https: //www.openssl.org/source/old/1.0.1/openssl-1.0.1u.tar.gz
tar zxf openssl-1.0.1u.tar.gz && cd openssl-1.0.1u/ ;./ config -fPIC –prefix=/usr/local/openssl enable-shared && make;make install ;cd
mv /usr/bin/openssl /usr/bin/openssl.1.0.1e ;ln -s /usr/ local/open ssl/bin/openssl /usr/bin/openssl
wget http://mirrors.shuosc.org/apache//httpd/httpd-2.4.23.tar.gz && tar zxf httpd-2.4.23.tar. gz
sed -i’s/#define AP_SERVER_BASEVENDOR “Apache Software Foundation”/#define AP_SERVER_BASEVENDOR “yyankep”/g’ /root/httpd-2.4.23/include/ap_release.h
sed -i’s /#define AP_SERVER_BASEPROJECT “Apache HTTP Server”/#define AP_SERVER_BASEPROJECT “yankerp”/g’ /root/httpd-2.4.23/include/ap_release.h
sed -i’s/#define AP_SERVER_BASEPRODUCT “Apache”/# define AP_SERVER_BASEPRODUCT “Yankai”/g’ /root/httpd-2.4.23/include/ap_release.h
cd httpd-2.4.23/ && ./configure –prefix=/usr/local/http-2.4.23 –enable-so –enable-cgi –enable-cgid –enable-ssl –with-ssl=/usr/local/openssl –enable-rewrite –with-pcre=/usr/local/pcre- -with-z=/usr/local/zlib –with-apr=/usr/local/apr –with-apr-util=/usr/local/apr-util –enable-modules=most –enable- mods-shared=most –enable-mpms-shared=all –with-mpm=event –enable-proxy –enable-proxy-fcgi –enable-expire s –enable-deflate && make;make install
sed -i’s/#ServerName www.example.com:80/ServerName www.yankerp.com/g’ /usr/local/http-2.4.23/ conf/httpd.conf
ln -s /usr/local/http-2.4.23/bin/* /usr/local/bin/ ;cp /usr/local/http-2.4.23/bin/apachectl /etc /init.d/httpd
sed -i ‘1a # chkconfig: 35 85 21’ /etc/init.d/httpd ;sed -i ‘2a # description: apache 2.4.23’ /etc/init.d/ httpd
chkconfig –add httpd ;chkconfig httpd on ;systemctl start httpd ;netstat -anpt | grep httpd

Copy the above script to notepad++ for editing In the device, copy it to run under Linux again!

< span style="font-size:14px">

2, you can refer to me for installing Mysql Of this article: http://blog.csdn.net/qq_39591494/article/details/78255286

I installed it in advance!

3、FastCGI方式安装PHP(PHP主机操作)

1. Resolve dependencies

< /p>

[root@php ~]# yum -y install libxml2-devel lzip2-devel libcurl-devel libmcrypt-devel openssl-devel bzip2-devel

install Libmcrypt

[root@php ~]# wget ftp://mcrypt.hellug.gr/pub/crypto/mcrypt/libmcrypt/libmcrypt-2.5.7.tar .gz

2.编译安装PHP

[root@php ~]# tar zxf php-5.6.25.tar.gz [root @php ~]# cd php-5.6.25/

./configure –prefix=/usr/local/ph p5.6 –with-mysql=mysqlnd

–with-pdo-mysql=mysqlnd –with-mysqli=mysqlnd

–with-openssl –enable- fpm –enable-sockets–enable-sysvshm –enable-mbstring

–with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir =/usr –enable-xml –with-mhash

— with-mcrypt=/usr/local/libmcrypt –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d

–with-bz2 –enable-maintainer-zts

< p>make && make install


Explanation of related options:
–prefix=/usr/local/php5.6//installation location
–with-mysql=mysqlnd //support mysql
–with -pdo-mysql=mysqlnd //support pdo module
–with-mysqli=mysqlnd //support mysqli module
Note: The role of the above three options: database Not on the same server as php, specify this method and install the database connection driver
–with-openssl//support openssl module
–enable -fpm//Support fpm mode
–enable-sockets//Enable socket support
–enable-sysvshm//Enable system shared memory support
–enable-mbstring//Multi-byte string , Like our Chinese is multi-byte strings
–with-freetype-dir//support freetype, freetype-devel must be installed, font-related, font analysis tools
–with-jpeg- dir
–with-png-dir
Note: The role of the above two options: processing jpeg, png pictures, php can dynamically generate jpeg pictures
–with-zlib// is a compression library, Used to compress transmission during Internet transmission
–with-libxml-dir=/usr//This libxml is used to parse xml, specify /usr under
–enable-xml //support xml
–with-mhash//support mhash
–with-mcrypt=/usr/local/libmcrypt //libmcrypt-devel this package specifies
–with-config-file-path =/etc //Specify the storage path of the configuration file
–with-config-file-scan-dir=/etc/php.d //Configuration file scan path
–with-bz2 //Support BZip2
In order to support the two MPMs of apache worker or event ,编译时使用了–enable-maintainer-zts 选项

3.提供PHP Main configuration file and scripts provided

[root@php php-5.6.25]# cp php.ini-production /etc/php.ini[root@php php-5.6.25]# cp sapi/fpm/init.d.php-fpm /etc/ init.d/php-fpm[root@php php-5.6.25]# chmod +x /etc/init.d/php-fpm [root@php php-5.6.25]# chkconfig --add php-fpm[ root@php php-5.6.25]# chkconfig php-fpm on


< p>

4. Provide php-fpm configuration file and edit :

[root@php php-5.6.25]# cp /usr/local/php5.6/etc/php-fpm.conf.defa ult /usr/local/php5.6/etc/php-fpm.conf[root@php php-5.6.25]# vim /usr/local/php5.6/etc/php-fpm.conf

< span style="font-size:14px">The revised content is as follows:

pid = run/ php-fpm.pid
listen = 192.168.146.110:9000
//IP address of the PHP host
pm.max_children = 50
pm.start_servers = 5
pm. min_spare_servers = 5
pm.max_spare_servers = 35

Start PHP-FPM service


< /span>

Create a new virtual host directory on the php host for storing webpage files

< /p>

[root@php ~]# mkdir -p /var/www/y ankerp

here PHP is officially installed

< br>

5. Configure Apache server (Apache host operation)

After Apache2.4, there has been a special module for FastCGI implementation, this module is mod_proxy_fcgi.so, it is actually used as a mod_proxy_fcgi.so span style=”color:#ff0000″> mod_proxy.so module expansion, therefore, both modules must be loaded

LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so

< span style="font-size:18px">Open the Apache main configuration file to open the module (#) and remove it



Create a directory as the home directory of the virtual host

< p>

[root@apache ~]# mkdir -p /var/www/yankerp

Edit the main configuration file httpd .conf, open virtual host

enableInclude conf/extra/httpd-vhosts.conf

locate AddType at the same time; add the following two lines: let apache recognize pages in php format
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps


and navigate to DirectoryIndex: Homepage that supports php format
DirectoryIndex index.php index.html #Add index.php (preferably at the top)

< span style="font-family:FangSong_GB2312">

Configure the virtual host to support the use of fcgi as follows:

[root@apache ~]#vim /usr/local/http-2.4.23/conf/extra /httpd-vhosts.conf


ServerAdmin webmaster @yankerp.com
D ocumentRoot “/var/www/yankerp”
ServerName www.yankerp.com
ServerAlias ​​yankerp.com
ErrorLog “logs/yankerp.com-error_log”
CustomLog “logs/yankerp.com-access_log “common
ProxyRequests Off
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://192.168.146.110:9000/var/ www/yankerp/$1

Options FollowSymLinks
AllowOverride None
Require all granted

< /VirtualHost>


< strong>The explanation is as follows:

Among them:
ProxyRequests off #Turn off forward proxy
ProxyPassMatch: Send file requests ending with .php to php-fp m process, php-fpm needs to know at least the running directory and URI, so these two parameters are specified directly after fcgi://192.168.146.110:9000, and the transfer of other parameters has been encapsulated by mod_proxy_fcgi.so. No need to specify manually.
Special attention is that the red font part needs to be consistent with the path after DocumentRoot in . ProxyPassMatch will only match and execute this rule if the content meets a specific regular pattern. The pattern here is, ^/(.*\.php(/.*)?)$
Starting from the root directory of the website (virtual host , matching any ending with .php, or in After .php follow a / followed by the path of other content. ^ (caret) and $ (dollar) mark the beginning and end of the path to be matched( )The content in parentheses can be represented by $1, with It is convenient to quote it later.
fcgi:// 192.168.146.110:9000 Proxy forwarded by mod_proxy_fcgi , Use the fastCGI protocol to switch to the port that PHP-FPM is listening.


6. Create a mysql account for php server connection on the mysql host (Mysql server operation)

mysql> grant all on *.* to yankerp@'%' identified by '123' ; 


< /span>

Create a .php test page in the /var/www/yankerp directory on the php server: (PHP operation)

< span style="font-size:18px">Test access to the php test page: http://ApacheIP address/ The following interface appears to prove that LAMP is officially built successfully!



See the above two test pages explaining apache, php, mysql can work together.

< br>

7. Install ab stress test (Mysql host operation)

ab is the stress testing tool that comes with apache. ab is very practical. It can not only perform website access stress test on apache server, but also perform stress test on other types of servers. Such as nginx, tomcat, IIS, etc.

[root@mysql ~]# yum install -y httpd-tools


-n: The number of requests executed in the test session (ie Total number of requests).
-c: The number of requests generated at one time (ie the number of concurrent users).

Requests per second: 2909.06 [#/sec] (mean) //throughput, the number of requests per secondtest a few more Second, determine the scope

< br>

8. Install php acceleration software Xcache under CentOS7.4 (complete the following operations on the php host)

1) Download xcache

[root@php ~]# wget http://xcache.lighttpd.net/pub/Releases/3.2.0/ xcache-3.2.0.tar.gz

[root@php ~]# tar zxf xcache-3.2.0.tar.gz //Unzip
[ root@php ~]# cd xcache-3.2.0/ //Enter the decompression path

< p>[root@php xcache-3.2.0]# /usr/local/php5.6/bin/phpize / /Use phpize to generate ./configure configuration file

[root @php xcache-3.2.0]#./configure –enable-xcache –enable-xcache-coverager –enable-xcache-optimizer –with-php-config= /usr/local/php5.6/bin/php-config && make && make install


After the installation is complete, the following interface appears. Remember the following path, which will be used later


2) Create xcache cache file


3) Copy the xcache background management program to the root directory of the website

[root@php xcache-3.2.0]# cp -r htdocs/ /var/www/yankerp/ xcache


4) Configure php to support xcache

vi / etc/php.ini #Edit the configuration file, in Add the following content in the last line
[xcache-common]
extension = /usr/local/php5.6/lib/ php/extensions/no-debug-zts-20131226/xcache.so
[xcache.admin]
xcache.admin.enable_auth = Off
[xcache]
xcache.shm_scheme =”mmap”< br> xcache.size=60M
xcache.count =1
xcache.slots =8K
xcache.ttl=0
xcache.gc_interval =0
xcache.var_size=64M
xcache.var_count =1
xcache.var_slots =8K
xcache.var_ttl=0
xcache.var_maxttl=0
xcache.var_gc_interval =300
xcache.test =Off
xcache. readonly_protection = Off
xcache.mmap_path =”/tmp/xcache”
xcache.coredump_directory =””
xcache.cacher =On
xcache.stat=On
xcache.optimizer =Off< br> [xcache.coverager]
xcache.coverager =On
xcache.coveragedump_directory =””



< p>copy the xcache directory to the apache host

[root@php xcache-3.2.0]# scp -r /var/www/yankerp /xcache/ 192.168.146.100:/var/www/yankerp/


9. Test
service php-fpm restart #Restart the php-fpm browser and open xcache under the root directory of the website

http://ApacheIP address/xcache as follows:


So far, the installation of php acceleration software Xcache under Linux is complete


< span style="font-family:FangSong_GB2312">Perform an ab stress test: Perform the first stress test:

[root@mysql ~]#ab -c 500 -n 10000 http://192.168 .146.100/index.php

< br>


< span style="font-size:18px">Execute the second Stress tests:

Check the hit rate of xcache:



xcache is also very good, next we will deploy the BBS forum! Use Discuz (PHP server operation)

1. Unzip the Discuz file and upload all Place the file in the website directory

[root@php src]# unzip Discuz_7.0.0_FULL_SC_UTF8.zip [root@php src]# mv upload/ /var/www/yankerp/bbs

Set the service user of php-fpm as the owner of the following file or set the write permission for it, otherwise an error will be reported during installation



Modify the php.ini file

vim /etc/php.ini


restart PHP after modification -FPM

[root@php bbs]# service php-fpm restart 

web 服务器也需要有静态文件(apache 服务器上操作)

[root@apache bbs]# unzip Discuz_7.0.0_FULL_SC_UTF8.zip [root@ apache ~]# mv upload/ /var/www/yankerp/bbs[root@apache ~]# cd /var/www/yankerp/bbs/[root@apache bbs]# chown -R daemon config.inc.php attachments/ forumdata/ uc_client/data/cache/ templates/


< /p> 在数据库服务器上创建bbs 数据库及授权帐户(Mysql服务器操作)

mysql> create database bbsdb;mysql> grant all on bbsdb.* to runbbs@'%' identified by '123';


验证:输入 httd://192.168.146.100/bbs/install 即可安装,如下:



这时候出现以下界面


是不是感觉有点…..Lou 

出现上面这种情况是由于 php 服务器安装了 discuz 之后导致程序发生变化从而导致动态服务器和静态服务器的程序不一致,只需要手动把 bbs 服务器的文件和 web 服务器进行一次同步即可 如下:(php操作)

[root@php bbs]# scp -r /var/www/yankerp/bbs/* [email protected]:/var/www/yankerp/bbs/

再次访问 bbs 的网址就正常了。



2017年倒计时! ! !

这个图片插的就是很累,希望对您有所帮助,再见~

Leave a Comment

Your email address will not be published.