In the previous article, we have installed httpd and MySQL services. Next, we will install PHP.
We know that,PHP works in three modes:: CGI, module, FastCGI. Apache and PHP are usually combined in the form of modules. In view of FastCGI's working mode, it will be explained later.
I. Compile PHP in modular mode. php is loaded as an Apache module.
If we didn't install libxml2 libxml2-devel bzip2-devel libmcrypt-devel before
Install these packages:
[[email protected] ~]# yum -y install libxml2 libxml2-devel bzip2-devel libmcrypt libmcrypt-devel
However, I do not have the libmcrypt * package:
No package libmcrypt available.
No package libmcrypt-devel available.
You can download the RPM package: http://rpm.pbone.net/
[[email protected] ~]# ls | grep lib*[[email protected] ~]# ls lib*libmcrypt-2.5.8-9.el6.i686.rpm libmcrypt-devel-2.5.8-9.el6.i686.rpm[[email protected] ~]# rpm -ivh lib*warning: libmcrypt-2.5.8-9.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEYPreparing... ########################################### [100%] 1:libmcrypt ########################################### [ 50%] 2:libmcrypt-devel ########################################### [100%]
1. decompress and compile
-- With-apxs2 =/usr/local/Apache/bin/apxs # compile PHP into Apache module based on apxs
OK. Go...
[[Email protected] ~] # Tar xf php-5.4.30.tar.gz-C/usr/local/src [[email protected] ~] # Cd/usr/local/src/php-5.4.30 [[email protected] php-5.4.30] #. /configure -- prefix =/usr/local/PHP -- With-mysql =/usr/local/MySQL -- With-OpenSSL -- With-mysqli =/usr/local/MySQL/bin/ mysql_config -- enable-mbstring -- With-FreeType-dir -- With-JPEG-dir -- With-PNG-dir -- With-zlib -- With-libxml-Dir =/usr -- enable-XML -- enable-sockets -- enable-FPM -- with-apxs2 =/usr/local/Apache/bin/apxs -- With-mcrypt -- With-config-file-Path =/etc -- With-config -File-scan-Dir =/etc/PHP. D -- with-bz2 -- enable-maintainer-ZTS # description: -- prefix =/usr/local/PHP # PHP installation path -- With-mysql =/usr/local/MySQL # MySQL installation path -- With-OpenSSL # support for OpenSSL functions -- With-mysqli =/usr/local/MySQL/bin/mysql_config # Another interface for accessing MySQL and Apache, install it in the binary directory -- enable-mbstring # multi-byte string -- With-FreeType-Dir # the header file of the installed library -- With-JPEG-Dir # JPEG library -- With-PNG- dir # PNG Library -- With-zlib # common compression library on the Internet, first compress and then transfer, reduce bandwidth -- With-libxml-Dir =/usr # path of the XML library file -- enable-XML # support for XML functions -- enable-sockets # PHP support for socket functions -- enable-FPM # PHP fastCGI -- with-apxs2 =/usr/local/Apache/bin/apxs # compile PHP into an Apache module based on apxs -- With-mcrypt # support for additional cryptographic libraries -- With-config-File -Path =/etc # PHP configuration file placement path -- With-config-file-scan-Dir =/etc/PHP. d # PHP configuration file multipart file placement path -- with-bz2 # compression library -- enable-maintainer-ZTS # This option is used when Apache uses worker or event mpm, the usage of this item depends on the type of Apache, which is not required when Apache uses prefork; this item must be added if event or worker is used; apache must compile this [[email protected] php-5.4.30] # Make & make install/usr/local/PHP # Is the PHP home directory/usr/local/PHP/ bin # php Command Line tool/usr/local/PHP/etc # configuration file
2. Provide configuration files for PHP
The path of the configuration file has been specified during compilation, so PHP will automatically go to the/etc/directory to find its configuration file.
[[email protected] php-5.4.30]# cp php.ini-production /etc/php.ini
3. Integrate Apache and PHP
We know that for simple web servers, rather than application servers, it only supports static webpage formats in HTML format. So how does Apache support dynamic web pages? For example, PHP.
PHP has three working modes: CGI, Moudle, and FastCGI. When apache finds that the page to be processed is a dynamic webpage (depending on the file name suffix), it will find it in the configuration file to see which application (handler) can compile and interpret this type, these pages are handed over to the corresponding application for processing, and then the application returns the Apache pure HTML static page, and Apache then returns the HTML page to the client browser.
1. Edit the/etc/httpd. conf configuration file and locate addtype.
Check/etc/httpd. conf. We can see that PHP is loaded as a module of Apache.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/41/35/wKioL1PRvRbSUAh_AAAriqcMVFA008.jpg "Title =" 9.png" alt = "wkiol1prvrbsuah_aaariqcmvfa008.jpg"/>
[[Email protected] ~] # Vi/etc/httpd. conf # Add the following two lines to enable Apache to explain ". PHP "file addtype application/X-httpd-PHP. phpaddtype application/X-httpd-PHP-source. PHPs
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/41/2E/wKiom1PRquHyIA4_AAEu9VTWjps944.jpg "Title =" 9.png" alt = "wkiom1prquhyia4_aaeu9vtwjps944.jpg"/>
2. Locate directoryindex
<Ifmodule dir_module> directoryindex. php index.html # modify the default Apache page type </ifmodule>
3. Test
[[Email protected] ~] # Httpd-T [[email protected] ~] # Service httpd restart [[email protected] ~] # Vi/usr/local/Apache/htdocs/index. php # Add the following content <? Phpphpinfo ();?>
Access through a browser. If the following page appears, it means OK.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/41/34/wKioL1PRtp-Cy6aMAAF_VXe-OuA003.jpg "Title =" 9.png" alt = "wKioL1PRtp-Cy6aMAAF_VXe-OuA003.jpg"/>
4. Integration of PHP and MySQL
Make sure that the -- with-mysql option is used to compile PHP so that PHP can call the library provided by MySQL.
[[email protected] ~]# vi /usr/local/apache/htdocs/index.php<?php $conn = mysql_connect(‘localhost‘, ‘root‘, ‘123456‘); if ($conn) echo "Connect MySQL Success"; else echo "Failure, can‘t connect mysql";?>
Access through a browser. If the following page appears, it means OK.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/41/34/wKioL1PRub2hOy8MAABU_tE6znM549.jpg "Title =" 9.png" alt = "wkiol1prub2hoy8maabu_te6znm549.jpg"/>
Configure PHP in Apache to support the module mode, CGI Mode, and FastCGI mode.
Install Apache, MySQL, and PHP in sequence.
Apache must support FastCGI and PHP must also support FastCGI. Both are supported.
1. The installation of Apache and MySQL is relatively simple, skipped
2. install PHPI installed php5.4.30 with the built-in PHP-FPM, so I don't need to install any additional patches.
If PHP is a lower version, you may need to install PHP-FPM separately.
[[email protected] ~]# yum -y install php-fpm
If PHP-FPM is built in, specify the -- enable-FPM option during compilation.
# ./configure --prefix=/usr/local/php--with-mysql=/usr/local/mysql--enable-fpm # fastCGI--with-apxs2=/usr/local/apache/bin/apxs # module
Note:
-- Enable-FastCGI
-- Enable-force-CGI-Redirect
These two new PHP versions have built-in support. Therefore, if these two parameters are added, the system prompts that these two parameters cannot be found after make is complete. Therefore, you do not need to add these two parameters.
With-apxs2
This parameter cannot be configured in some articles that require FastCGI mode. In fact, this is not exactly the case, the purpose of this parameter is to compile the PHP interpretation module into the so file and add it to the modules of Apache and automatically add it to the conf file. If you do not want to use the module mode, run the following command in httpd. conf:
Loadmodule php5_module modules/libphp5.so
This line can be commented out.
3. install Apache mod_fastcgi
### The following dependent packages may be required. Generally, mod_fastcgi is compiled first. If an error is reported during compilation, re-install ### Yum-y install httpd-devle APR Apr-devel libtool # wget http://www.fastcgi.com/dist/mod_fastcgi-2.4.6.tar.gz# tar-zxvf mod_fastcgi-2.4.6.tar.gz # cd mod_fastcgi-2.4.6 # cp makefile. AP2 makefile # Vim makefile # change the top_dir path in makefile to your Apache installation path top_dir =/usr/local/Apache # Make & make install ### after successful installation, mod_fastcgi.so will be automatically copied to the/usr/local/Apache/modules directory
The following describes how to configure the four modes:
1. Module Mode
This mode is the easiest to add in HTTP. conf
LoadModule php5_module modules/libphp5.so
You can. Add addtype
<IfModule mime_module>AddType application/x-httpd-php .phpAddType applicaiton/x-httpd-php-source .phps
2. CGI Mode
This mode needs to be commented out in httpd. conf.
LoadModule php5_module modules/libphp5.so
This line. If you do not comment this line, it will always go to the handler mode. That is, the module mode.
Then add action in httpd. conf:
Action application/x-httpd-php /cgi-bin/php-cgi
If you cannot find PHP-CGI in the/cgi-bin/directory, you can use CP in the PHP bin.
Restart Apache, open the test page, and find that the Server API becomes cgi/FastCGI. It indicates that the CGI Mode is successfully switched.
# service httpd restart
Advantages of FastCGI:
In terms of stability, FastCGI uses an independent process pool to run CGI. When a single process dies, the system can easily discard it and re-allocate the new process to run the logic.
In terms of security, FastCGI supports distributed computation. FastCGI is completely independent from the host server, and FastCGI does not bring down the server.
In terms of performance, FastCGI separates the processing of dynamic logic from the server, and leaves the High-load Io processing to the host server. In this way, the host server can concentrate on Io, for a common dynamic web page, logical processing may only be a small part. A large number of static Io processing, such as images, does not require the participation of Logic Programs.
In terms of scalability, FastCGI is a neutral technical standard that fully supports processing programs written in any language (PHP, Java, Perl, Ruby, C ++, python ...)
Applicable to any operating system, which can be used at http://www.fastcgi.com/dist/mod_fastcgi-current.tar.gz.
3. FastCGI mode, with Apche built-in Process Manager
Add the mod_fastcgi module to the httpd. conf configuration file:
LoadModule fastcgi_module modules/mod_fastcgi.so
This mode does not comment out loadmodule php5_module modules/libphp5.so. It seems that this line does not matter, but it is better to comment out it. You only need to configure the following modules
<IfModule fastcgi_module> FastCgiServer /usr/local/apache/cgi-bin/php-cgi -processes 20 AddType application/x-httpd-php .php AddHandler php-fastcgi .php Action php-fastcgi /cgi-bin/php-cgi</IfModule>
It will automatically go to FastCGI mode.
Restart Apache and use PS aux | grep PHP to check that many PHP-CGI processes are running. Indicates that the configuration takes effect.
4. FastCGI mode, using the PHP-FPM Process Manager
Add the mod_fastcgi module to the httpd. conf configuration file:
LoadModule fastcgi_module modules/mod_fastcgi.so
Restart httpd.
[[email protected] ~]# service httpd restart[[email protected] ~]# tail /usr/local/apache/logs/error_log [Mon Jul 21 15:44:21 2014] [notice] FastCGI: process manager initialized (pid 1274)
Edit the/etc/httpd/CONF. d/FastCGI. php. conf configuration file:
<Ifmodule fastcgi_module> fastcgiexternalserver/usr/local/Apache/cgi-bin/PHP-CGI-host 127.0.0.1: 9000 addtype application/X-httpd-PHP. PHP addhandler PHP-FastCGI. PHP action PHP-FastCGI/cgi-bin/PHP-CGI </ifmodule> note: # configure FastCGI server # You may need to specify the location of PHP-CGI ScriptAlias/cgi-bin/"/usr/local/PHP/bin/" # Add the MIME type addtype application/X-HTTPd -PHP. PHP #. php-FastCGI must be used for PHP end requests to process addhandler PHP-FastCGI. PHP # Set the PHP-FastCGI processor action PHP-FastCGI/cgi-bin/PHP-CGI
Note that 127.0.0.1: 9000 is the enabled port of PHP-FPM, so we also need to open PHP-FPM.
For PHP-FPM installed in yum,
/Usr/sbin/PHP-FPM # PHP-FPM Service Startup Script:/etc/init. d/PHP-FPM, configuration file/etc/php-fpm.conf, /etc/php-fpm.d/# chkconfig -- add PHP-FPM # chkconfig PHP-FPM on # service PHP-FPM {START | stop}
If the -- enable-FPM is specified during PHP compilation,
/Usr/local/PHP/sbin/PHP-FPM start config file:/usr/local/PHP/etc/php-fpm.conf
Check whether PHP-FPM is enabled. We can see that PHP-FPM listening is on port 9000.
[[email protected] ~]# netstat -tulpn | grep phptcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 1192/php-fpm [[email protected] ~]# ps -ef | grep phproot 1472 1 0 16:14 ? 00:00:00 php-fpm: master process (/etc/php-fpm.conf)apache 1473 1472 0 16:14 ? 00:00:00 php-fpm: pool www apache 1474 1472 0 16:14 ? 00:00:00 php-fpm: pool www apache 1475 1472 0 16:14 ? 00:00:00 php-fpm: pool www apache 1476 1472 0 16:14 ? 00:00:00 php-fpm: pool www apache 1477 1472 0 16:14 ? 00:00:00 php-fpm: pool www
Php-FPM has a master process, which is responsible for the creation and destruction of other sub-processes. You can configure in/etc/php-fpm.conf, and/etc/php-fpm.d/*. conf.
Then we can test whether the access is correct.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/41/42/wKiom1PR9h6ygli7AAOpptUgSiI877.jpg "Title =" 1.png" alt = "wkiom1pr9h6ygli7aaopptugsii877.jpg"/>
Refer:
Https://www.centos.bz/2011/12/configure-apache-run-php-as-fastcgi/
Http://ixdba.blog.51cto.com/2895551/806622
This article is from the share your knowledge blog, please be sure to keep this source http://skypegnu1.blog.51cto.com/8991766/1530080