新版LAMP环境搭建
注意:先安装MySQL和Apache,后安装PHP,因为PHP配置编译的时候需要指定mysql路径和apache路径。
编译安装:
httpd-2.4.12.tar.gz + mysql-5.6.22-linux-glibc2.5-x86_64.tar.gz + php-5.4.39.tar.gz + xcache-2.0.0.tar.gz
实验环境:
CentOS 6.5 x86_64
编译安装Apache
(1)构建MPM为静态模块
在全部平台中,MPM都可以构建为静态模块。在构建时选择一种MPM,链接到服务器中。如果要改变MPM,必须重新构建。为了使用指定的MPM,请在执行configure脚本时,使用参数 --with-mpm=NAME。NAME是指定的MPM名称。编译完成后,可以使用 ./httpd -l 来确定选择的MPM。此命令会列出编译到服务器程序中的所有模块,包括 MPM。
(2)构建 MPM 为动态模块
在Unix或类似平台中,MPM可以构建为动态模块,与其它动态模块一样在运行时加载。 构建 MPM 为动态模块允许通过修改LoadModule指令内容来改变MPM,而不用重新构建服务器程序。在执行configure脚本时,使用--enable-mpms-shared选项即可启用此特性。当给出的参数为all时,所有此平台支持的MPM模块都会被安装。还可以在参数中给出模块列表。默认MPM,可以自动选择或者在执行configure脚本时通过--with-mpm选项来指定,然后出现在生成的服务器配置文件中。编辑LoadModule指令内容可以选择不同的MPM。
解决依赖关系
注意:httpd-2.4.12需要较新版本的apr和apr-util
编译安装apr
1 2 3 4 5 | cd /usr/local/src tar xf apr-1.4.6. tar .bz2 cd apr-1.4.6 . /configure --prefix= /usr/local/apr make && make install |
编译安装apr-util
1 2 3 4 5 | cd /usr/local/src tar xf 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 |
编译安装httpd-2.4.12
1 2 3 4 5 6 | yum -y install pcre-devel openssl-devel cd /usr/local/src tar xf httpd-2.4.12. tar .gz cd httpd-2.4.12 . /configure --prefix= /usr/local/apache -- enable -so-- enable -ssl -- enable -cgi -- enable -rewrite --with-zlib --with-pcre --with-apr= /usr/local/apr--with-apr-util = /usr/local/apr-util -- enable -mpms-shared=all --with-mpm=event make && make install |
修改httpd主配置文件
编辑/usr/local/apache/conf/httpd.conf 添加如下行:
1 2 | PidFile "/var/run/httpd.pid" httpdPid文件路径 ServerName localhost:80 有域名填写真实域名即可 |
apache工具快捷键
修改PATH环境变量,让系统可以直接使用apache的相关命令。
编辑 /etc/profile.d/httpd.sh 添加如下行:
1 | export PATH=$PATH: /usr/local/apache/bin |
执行/etc/profile.d/httpd.sh 然后退出重新登录
启动apache
1 2 3 | apachectl -t 测试httpd是否正常 apachectl graceful 重新加载配置 apachectl start 启动httpd |
编译安装MySQL
建立安全用户
1 2 3 4 | groupadd mysql useradd -g mysql -s /sbin/nologin -M mysql mkdir -p /mydata/data chown -R mysql:mysql /data/mysql |
安装并初始化-5.6.22
1 2 3 4 5 6 7 8 | cd /usr/local/src tar xfmysql-5.6.22-linux-glibc2.5-x86_64. tar .gz -C /usr/local cd /usr/local/ ln -svmysql-5.6.22-linux-glibc2.5-x86_64 mysql cd mysql chown -R mysql:mysql /usr/local/mysql/ scripts /mysql_install_db --user=mysql--datadir= /data/mysql chown -R root . #防止文件被修改,初始化完成后修改回root用户权限 |
为mysql提供主配置文件
1 2 3 4 5 6 | cp /usr/local/mysql/my .cnf /etc 复制my.cnf到 /etc 目录下 #vim /etc/my.cnf 编辑my.cnf文件,修改如下内容 basedir = /usr/local/mysql datadir = /data/mysql port = 3006 server_id = 1 |
为mysql提供sys服务脚本
1 2 3 4 5 6 7 8 9 | cd /usr/local/mysql cp support-files /mysql .server /etc/rc .d /init .d /mysqld chmod +x /etc/rc .d /init .d /mysqld 修改 /etc/rc .d /init .d /mysqld basedir= /usr/local/mysql datadir= /mydata/data chkconfig --add mysqld chkconfig --level 35 mysqld on service mysqld start |
mysql工具快捷键
修改PATH环境变量,让系统可以直接使用mysql的相关命令。
编辑 /etc/profile.d/mysqld.sh 添加如下行:
1 | export PATH=$PATH: /usr/local/mysql/bin |
执行/etc/profile.d/mysql.sh 然后退出重新登录
可以直接使用mysql 登录终端
常见问题分析
错误:error while loadingshared libraries: libaio.so.1
解决:yum -y install libaio
错误:Starting MySQL.Theserver quit without updating PID file (/[FAILED]ql/darker.pid).
InnoDB: mmap(137363456 bytes) failed; errno 12
[ERROR]InnoDB: Cannot allocate memory for the buffer pool
[ERROR] Plugin 'InnoDB' init function returnederror.
[ERROR] Plugin 'InnoDB' registration as aSTORAGE ENGINE failed.
[ERROR] Unknown/unsupported storage engine:InnoDB
[ERROR] Aborting
解决:vim /etc/my.cnf innodb_buffer_pool_size= 8M 修改innodb为8M,内存过小导致无法启动
编译安装PHP
解决依赖关系
1 2 | #rpm -ivh http://www.lishiming.net/data/attachment/forum/month_1211/epel-release-6-7.noarch.rpm #yum -y install libxml2-devel opensslopenssl-devel bzip2 bzip2-devel libjpeg-devel libpng libpng-devel freetypefreetype-devel libmcrypt-devel |
编译安装
1 2 3 4 5 | cd /usr/local/src tar -xfphp-5.4.39. tar .gz cd php-5.4.39 . /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 -fpm -- enable -xml -- enable -sockets --with-mcrypt --with-config- file -path= /etc --with-config- file -scan- dir = /etc/php .d--with-bz2 -- enable -maintainer-zts make && make install |
说明:这里为了支持apache的worker或event这两个MPM,编译时使用了--enable-maintainer-zts选项。
新版本使用第三方模块PHP-FPM或者编译成模块化(默认编译是fpm)
--enable-fpm
--with-apxs2=/usr/local/apache/bin/apxs
为php提供配置文件:
1 | cp /usr/local/src/php-5 .4.39 /php .ini-production /etc/php .ini |
配置php-fpm
为php-fpm提供Sys init脚本,并讲其添加至服务列表
1 2 3 4 | cp /usr/local/src/php-5 .4.39 /sapi/fpm/init .d.php-fpm /etc/rc .d /init .d /php-fpm chmod +x /etc/rc .d /init .d /php-fpm chkconfig --add php-fpm chkconfig php-fpm on |
为php-fpm提供配置文件:
1 | cp /usr/local/php/etc/php-fpm .conf.default /usr/local/php/etc/php-fpm .conf |
编辑php-fpm的配置文件:
vim /usr/local/php/etc/php-fpm.conf
配置php-fpm相关选项为你所需要的值,并启动pid文件
1 2 3 4 5 6 7 | #pm=dynamic #默认为动态模式 #pm.max_children = 50 #静态方式下开启的php-fpm进程数量。 #pm.start_servers = 10 #动态方式下的起始php-fpm进程数量。 #pm.min_spare_servers = 5 #动态方式下的最小php-fpm进程数量。 #pm.max_spare_servers = 20 #动态方式下的最大php-fpm进程数量。 #user=apache #修改为apache启动的用户或者使用默认 #group=apache #修改为apache启动的组或者使用默认 |
如果dm设置为static,那么其实只有pm.max_children这个参数生效。系统会开启设置数量的php-fpm进程。
如果dm设置为dynamic,那么pm.max_children参数失效,后面3个参数生效。系统会在php-fpm运行开始的时候启动pm.start_servers个php-fpm进程,然后根据系统的需求动态在pm.min_spare_servers和pm.max_spare_servers之间调整php-fpm进程数。
xcache加速PHP
1、安装
1 2 3 4 5 6 | cd /usr/local/src tar xf xcache-2.0.0. tar .gz cd xcache-2.0.0 /usr/local/php/bin/phpize . /configure -- enable -xcache--with-php-config= /usr/local/php/bin/php-config make && make install |
安装结束时,会出现类似如下行:
1 | Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-zts-20100525/ |
2、编辑php.ini,整合php和xcache:
首先将xcache提供的样例配置导入php.ini
1 2 | mkdir /etc/php .d cp /usr/local/src/ xcache-2.0.0 /xcache .ini /etc/php .d |
接下来编辑/etc/php.d/xcache.ini,找到zend_extension开头的行,修改为如下行:
1 | zend_extension = /usr/local/php/lib/php/extensions/no-debug-zts-20100525/xcache .so |
注意:如果php.ini文件中有多条zend_extension指令行,要确保此新增的行排在第一位。
常见问题分析
错误:configure: error: xml2-config not found. Please check your libxml2installation.
解决:yum -y install libxml2-devel
错误:configure: error: Cannot find OpenSSL's <evp.h>
解决:yum install -y openssl openssl-devel
错误:configure: error: Please reinstall the BZip2 distribution
解决:yum install -y bzip2 bzip2-devel
错误:configure: error: jpeglib.h not found.
解决:yum install -y libjpeg-devel
错误:configure: error: png.h not found.
解决:yum install -y libpng libpng-devel
错误:configure: error:freetype.h not found.
解决:yum install -y freetype freetype-devel
错误:configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决:rpm -ivh http://www.lishiming.net/data/attachment/forum/month_1211/epel-release-6-7.noarch.rpm
yum install -y libmcrypt-devel
错误:make: *** [ext/fileinfo/libmagic/apprentice.lo] Error 1
这是由于内存小于1G所导致. 在./configure加上选项:
解决:--disable-fileinfo
错误:Disable fileinfo support 禁用 fileinfo 需重新编译
错误:make: *** [libphp5.la] Error 1
解决:yum-y install libtool libtool-ltdl-devel
安装完后直接make
错误:Cannot find autoconf. Please check your autoconf installation andthe
$PHP_AUTOCONFenvironment variable. Then, rerun this script.
解决:yum install autoconf
Apache支持PHP
修改配置文件
vim /usr/local/apache/conf/httpd.conf
1、添加如下二行
1 2 | AddType application /x-httpd-php .php AddType application /x-httpd-php-source .phps |
2、定位至DirectoryIndexindex.html
1 | DirectoryIndex index.phpindex.html |
3、去掉mod_proxy.so和mod_proxy_fcgi.so之前的注解,确保他们被apache加载。
4、如果php-fpm使用的是TCP socket,那么在httpd.conf末尾加上:
1 2 3 | <FilesMatch \.php$> SetHandler "proxy:fcgi://127.0.0.1:9000" < /FilesMatch > |
5、重启apache以及php-fpm
1 2 | apachectl restart /etc/init .d /php-fpm start |
测试LAMP架构
1 2 3 4 5 6 7 8 | vim /usr/local/apache/htdocs/index.php #写入PHP页面测试php连接mysql是否成功 <? php $ conn = mysql_connect (‘localhost’,’root’,’Password’); if ($conn) echo“Success….”; else echo“Failure….”; Phpinfo(); ?> |
常见问题
如果访问服务器上的php文件出现"file not found."十之八九是php-fpm.conf中的user和group没有读写权限,修改成apache所使用的user和group即可