搭建LAMP环境示例

编译apache httpd                    

安装依赖包。

[root@localhost ~]# yum -y install pcre pcre-devel expat-devel

编译apr和apr-util。

[root@localhost ~]# ls
anaconda-ks.cfg  apr-1.6.3.tar.gz  apr-util-1.6.1.tar.gz  httpd-2.4.29.tar.gz
[root@localhost ~]# tar xf apr-1.6.3.tar.gz 
[root@localhost ~]# tar xf apr-util-1.6.1.tar.gz 
[root@localhost ~]# cd apr-1.6.3
[root@localhost apr-1.6.3]# ./configure --prefix=/usr/local/apr 
[root@localhost apr-1.6.3]# make && make install
[root@localhost apr-1.6.3]# cd ../apr-util-1.6.1
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr && make && make install

编译httpd。

[root@localhost apr-util-1.6.1]# cd ..
[root@localhost ~]# tar xf httpd-2.4.29.tar.gz 
[root@localhost ~]# cd httpd-2.4.29
[root@localhost httpd-2.4.29]# ./configure --prefix=/usr/local/apache2 --sysconfdir=/etc/apache --with-z --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --with-mpm=event --enable-mpms-shared=all
[root@localhost httpd-2.4.29]# make && make install

编译后的规范化操作:                                      

[root@localhost httpd-2.4.29]# echo "MANPATH /usr/local/apache/man" >>/etc/man.config  # 设置man路径。
[root@localhost httpd-2.4.29]# echo 'PATH=/usr/local/apache/bin:$PATH' >/etc/profile.d/apache.sh
[root@localhost httpd-2.4.29]# source /etc/profile.d/apache.sh # 设置PATH环境变量。 [root@localhost httpd
-2.4.29]# ln -s /usr/include /usr/local/apache2/include # 输出头文件。

编译php                      

三种工作模式:CGI、作为模块加入到apache、fastcgi。最简单的是以模块方式加入到apache,此处演示的是php-fpm管理php-cgi方式。

fastcgi模式的php-cgi,由php-fpm提供服务管理,它会根据配置文件启动一定数量的cgi进程,其默认监听的端口为9000,该服务正常工作需要配置文件。也就是说fastcgi模式的php有两个配置文件,一个是php的配置文件,一个是php-fpm的配置文件。

虽然此处演示的是php-fpm管理方式,但有必要说明下,在Linux中如果模块化安装php,不推荐在使用Apache 2.x中使用线程化MPM(worker和event),而是使用prefork模式的mpm,因为Linux系统的线程设计并不那么完美。所以,如果php和apache在同一台主机上(cgi或者模块化方式安装php的时候),建议httpd使用prefork模型,而不在同一台主机中,建议将php设计为fastcgi的工作模式。而在windows平台中则无需考虑这些问题,因为windows系统是真正意义上的多线程系统。

下载相关文件:
php下载地址:http://php.net/downloads
php手册地址:http://php.net/manual/zh/
手册下载地址:http://php.net/download-docs.php

php编译选项说明                        

编译安装php有非常非常多的选项,比httpd还多。可以在解压php后的目录下使用./configure --help查看。以下是部分选项,其中给出"--enable-XXXX"的选项表示默认是disable的,若要开启,需要在此处手动enable,如果给出的是"--disable-XXXX"表示默认是enable的。

--prefix=PREFIX
【SAPI modules:】
--with-apxs2=FILE       Build shared Apache 2.0 Handler module. FILE is the optional
                          pathname to the Apache apxs tool apxs
--enable-fpm            Enable building of the fpm SAPI executable

【General settings:】
--with-config-file-path=PATH      Set the path in which to look for php.ini [PREFIX/lib]
--with-config-file-scan-dir=PATH  Set the path where to scan for configuration files

【Extensions:】
      #######################################################
      # --with-EXTENSION=shared[,PATH]                      #
      # NOTE: Not all extensions can be build as 'shared'.  #
      # Example: --with-foobar=shared,/usr/local/foobar/    #
      #######################################################
--with-openssl=DIR      Include OpenSSL support (requires OpenSSL >= 0.9.6)
--enable-mbstring       Enable multibyte string support
--with-zlib=DIR         Include ZLIB support
--with-bz2=DIR          Include BZip2 support
--with-mhash=DIR        Include mhash support
--with-mcrypt=DIR       Include mcrypt support
--with-freetype-dir=DIR  GD: Set the path to FreeType 2 install prefix
--with-jpeg-dir=DIR     GD: Set the path to libjpeg install prefix
--with-png-dir=DIR      GD: Set the path to libpng install prefix
--with-libxml-dir=DIR   SimpleXML: libxml2 install prefix
--enable-sockets        Enable sockets support
--disable-xml           Disable XML support (不写时默认--enable-xml)

【连接数据库:】
--with-mysql=DIR        Include MySQL support.  DIR is the MySQL base
                          directory, if no DIR is passed or the value is
                          mysqlnd the MySQL native driver will be used
--with-mysqli=FILE      Include MySQLi support.  FILE is the path
                          to mysql_config.  If no value or mysqlnd is passed
                          as FILE, the MySQL native driver will be used
--with-pdo-mysql=DIR    PDO: MySQL support. DIR is the MySQL base directory
                          If no value or mysqlnd is passed as DIR, the
                          MySQL native driver will be used
--enable-mysqlnd        Enable mysqlnd explicitly, will be done implicitly
                          when required by other extensions
【Zend:】
--enable-maintainer-zts    Enable thread safety - for code maintainers only!!

部分选项说明:

  1. 在【zend】扩展部分的选项"--enable-maintainer-zts"是为了让php支持多线程MPM的,即php以模块化方式或cgi模式安装时且httpd配置为worker或event时需要启用该项。而如果php以fastcgi模式安装时,由于php有独立的服务和进程,所以该项是多余的。
  2. "--with-apxs2"是让php以模块化的方式安装到其他程序中,"--enable-fpm"是让php以fastcgi模式工作的选项。所以此处采用后者,而以模块方式和httpd交互时采用前者。
  3. "--with-config-file-path"和"--with-config-file-scan-dir"分别是php配置文件php.ini的存放位置以及其他加载的配置文件路径,scan-dir类的目录就像/etc/profile.d、/etc/httpd/conf.d这样的目录路径。
  4. "--with-openssl"选项让php支持ssl;"--enable-mbstring"选项是让php支持多字节字符的,例如中文一个字符两个字节,也就是说让php支持国际化的;"--with-zlib"、"--with-bz2"、"--with-mhash"和"--with-mcrypt"选项让php支持这些压缩和加密机制。
  5. "--with-freetype-dir"、"--with-jpeg-dir"和"--with-png-dir"分别是让php支持多种文字样式、支持jpeg、支持png的选项。
  6. php连接mysql有两种方式,一种是以libmysql的驱动方式连接mysql,一种是以mysqlnd方式驱动连接mysql。以下列出了libmysql和mysqlnd这两种驱动方式的编译模式。

(1).以libmysql驱动方式连接mysql(Mariadb),需要提前安装mysql(Mariadb)和mysql-devel(mariadb-devel),并使用"--with-mysql"选项指定mysql安装路径,"--with-mysqli"选项指定mysql_config脚本的路径,"--with-pdo-mysql"选项也指定mysql安装路径。假如mysql安装在/usr/local/mysql下。

./configure --prefix=/usr/local/php 
--with-mysql=/usr/local/mysql 
--with-mysqli=/usr/local/mysql/bin/mysql_config

(2).以mysqlnd驱动方式连接mysql,不需要提前安装mysql和mysql-devel,--with-mysql、--with-mysqli和--with-pdo-mysql选项都不需要指定具体路径,只需使用mysqlnd作为这些选项的值即可。

./configure --prefix=/usr/local/php 
--with-mysql=mysqlnd 
--with-mysqli=mysqlnd 
--with-pdo-mysql=mysqlnd

在php 5.3的时候已经支持mysqlnd驱动方式了,在php 5.4的时候mysqlnd已经是默认的配置选项了。建议使用mysqlnd的驱动方式。

php编译过程                              

由于是配置fastcgi的模式,所以在./configure的时候将apxs2功能换为"--enable-fpm",并且由于此模式下的php由自己独立的服务来控制进程的生成,所以对于为了支持httpd线程的选项"--enable-maintainer-zts"也去掉。以下是编译安装过程:

[root@localhost ~]# yum install -y bzip2-level libmcrypt-devel openssl-devel libxml2-devel
[root@localhost ~]# tar xf php-5.4.25.tar.gz 
[root@localhost ~]# cd php-5.4.25
[root@localhost php-5.4.25]# yum install -y epel-release
[root@localhost php-5.4.25]# yum install -y bzip2-level libmcrypt-devel openssl-devel libxml2-devel libmcrypt
[root@localhost php-5.4.25]# ./configure --prefix=/usr/local/php --with-openssl --enable-mbstring --enable-sockets --with-freetype-dir --with-jpeg-dir --with-png-dir --with-libxml-dir=/usr --enable-xml --with-zlib --with-mcrypt --with-bz2 --with-mhash --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-fpm
[root@localhost php-5.4.25]# make && make install
[root@localhost php-5.4.25]# cp php.ini-production /etc/php.ini
# 提供php配置文件
[root@localhost php-5.4.25]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpmd
[root@localhost php-5.4.25]# chmod +x /etc/init.d/php-fpmd
# 提供php-fpm服务管理脚本
[root@localhost php-5.4.25]# cd /usr/local/php/
[root@localhost php]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[root@localhost php]# vi etc/php-fpm.conf # 修改php-fpm配置文件(做实验的话改不改随意)
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 2
pm.max_spare_servers = 8
[root@localhost php]# service php-fpmd start    
Starting php-fpm  done

配置httpd使其转发动态请求给php-fpm                    

[root@localhost php]# vi /etc/apache/httpd.conf 
# 启用fcgi的支持模块。
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
# 添加php后缀格式文件的识别功能。
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
# 添加php后缀的主页
DirectoryIndex index.php index.html
# 启用虚拟主机模块,Include虚拟主机配置文件,并注释中心主机DocumentRoot。
#DocumentRoot "/usr/local/apache/htdocs"
Include /etc/apache/extra/httpd-mpm.conf
# 配置虚拟主机。注意主机中需要添加下面两行,第一行表示关闭正向代理功能,第二行表示反向代理时进行正则匹配。
# 对主机的.php(不区分大小写)文件的访问都通过fcgi协议交给php,由于此处测试,php正好和httpd在同一服务器上,# 且php-fpm默认监听的端口为9000,所以为fcgi://127.0.0.1:9000,在此之后还需要写上/DocumentRoot/$1,
# "$1"是正则的反向引用
ProxyRequests off
ProxyPassMatch "(?i)^/(.*.php)$" fcgi://127.0.0.1:9000/var/www/a.com/$1

配置虚拟主机

[root@localhost extra]# cd /etc/apache/extra/
[root@localhost extra]# vi httpd-vhosts.conf 
<VirtualHost 192.168.1.220:80>
    ServerAdmin webmaster@dummy-host.example.com
    DocumentRoot "/usr/local/apache2/htdocs/"
    #ServerName dummy-host.example.com
    ErrorLog "logs/192_error.log"
    CustomLog "logs/192_access.log" common
</VirtualHost>
[root@localhost extra]# apachectl restart

[root@localhost extra]# vi /usr/local/apache2/htdocs/index.html
<h1>a.com</h1>
<?php
phpinfo();
?>
~

 
原文地址:https://www.cnblogs.com/liujunjun/p/12051509.html