LNMP环境搭建(基于Centos6.7 32位Linux操作系统)
一、linux环境安装
二、MySQL安装,参考LAMP安装过程:http://daixuan.blog.51cto.com/5426657/1717409
三、PHP编译安装
注意:针对Nginx的php安装和针对apache的php安装是有区别的,因为Nginx中的php是以fastcgi的方式结合nginx的,可以理解为nginx代理了php的fastcgi,而apache是把php作为自己的模块来调用的
1、下载PHP源码包,解压
[root@daixuan ~]# cd /usr/local/src
[root@daixuan src]# wget http://cn2.php.net/distributions/php-5.4.37.tar.bz2
[root@daixuan src]# tar jxvf php-5.4.37.tar.bz2
[root@daixuan src]# ls
php-5.4.37
2、创建相关账户
[root@daixuan src]# useradd -s /sbin/nologin php-fpm
3、配置编译选项
注意:由于之前在该虚拟机上安装了LAMP,安装过php,这里需要先删除
[root@daixuan php-5.4.37]# rm -rf /usr/local/php/
[root@daixuan src]# cd php-5.4.37
[root@daixuan php-5.4.37]# ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm --with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-mysql=/usr/local/mysql \
--with-mysql-sock=/tmp/mysql.sock \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-ftp \
--enable-mbstring \
--enable-exif \
--enable-zend-multibyte \
--disable-ipv6 \
--with-pear \
--with-curl \
--with-openssl
[root@daixuan php-5.4.37]# echo $? //结果为0说明没有报错
0
这里如果有报错,可能是某些库没有安装,例如:安装libcurl库:yum install -y libcurl-delevel
4、编译PHP
[root@daixuan php-5.4.37]# make
[root@daixuan php-5.4.37]# echo $?
0
注:如果这里遇到这样的错误,解决方法是:yum install -y libtool-ltdl-devel
/usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
make: *** [sapi/fpm/php-fpm]
5、安装PHP
[root@daixuan php-5.4.37]# make install
6、修改配置文件
[root@daixuan php-5.4.37]# cp php.ini-production /usr/local/php/etc/php.ini
[root@daixuan php-5.4.37]# cd /usr/local/php/etc/
[root@daixuan etc]# mv php-fpm.conf.default php-fpm.conf
[root@daixuan etc]# ls
pear.conf php-fpm.conf php.ini
保存配置文件后,检验配置是否正确的方法为:
[root@daixuan etc]# /usr/local/php/sbin/php-fpm -t
[05-Jan-2016 20:24:32] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
test is successful字样,说明配置没有问题
7、拷贝启动脚本
[root@daixuan src]# cd /usr/local/src/php-5.4.37
首先是拷贝一个php的启动脚本到/etc/init.d/下:
[root@daixuan php-5.4.37]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@daixuan php-5.4.37]# chmod 755 /etc/init.d/php-fpm
添加php-fpm到服务列表中,让它开机启动
[root@daixuan php-5.4.37]# chkconfig --add php-fpm
[root@daixuan php-5.4.37]# chkconfig php-fpm on
[root@daixuan php-5.4.37]# service php-fpm start
Starting php-fpm done
查看php-fpm的进程:
[root@daixuan php-5.4.37]# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 19901/php-fpm
[root@daixuan php-5.4.37]# ps aux | grep php-fpm
root 19901 0.0 0.1 26084 2976 ? Ss 20:17 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
php-fpm 19902 0.0 0.1 26084 2644 ? S 20:17 0:00 php-fpm: pool www php-fpm 19903 0.0 0.1 26084 2644 ? S 20:17 0:00 php-fpm: pool www
root 19926 0.0 0.0 6048 780 pts/0 S+ 20:20 0:00 grep php-fpm
二、Nginx编译安装
1、下载解压Nginx
[root@daixuan src]# cd /usr/local/src/
[root@daixuan src]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@daixuan src]# tar zxvf nginx-1.8.0.tar.gz
[root@daixuan src]# ls
nginx-1.8.0
nginx-1.8.0.tar.gz
2、配置编译选项
[root@daixuan nginx-1.8.0]# cd nginx-1.8.0
[root@daixuan nginx-1.8.0]# ./configure \
--prefix=/usr/local/nginx \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-pcre
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
[root@daixuan nginx-1.8.0]# echo $?
1
发现有错误,解决方法,安装pcre库,重新配置编译选项
[root@daixuan nginx-1.8.0]# yum install -y pcre-devel
[root@daixuan nginx-1.8.0]# echo $?
0
3、编译Nginx
[root@daixuan nginx-1.8.0]#make
4、安装
[root@daixuan nginx-1.8.0]#make install
5、启动Nginx
[root@daixuan nginx-1.8.0]# cd /usr/local/nginx/
[root@daixuan nginx]# ls sbin/nginx
sbin/nginx
启动nginx,报错:
[root@daixuan nginx]# /usr/local/nginx/sbin/nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@daixuan nginx]# netstat -lnp
tcp 0 0 ::ffff:127.0.0.1:8005 :::* LISTEN 2183/java
tcp 0 0 :::8009 :::* LISTEN 2183/java
tcp 0 0 :::80 :::* LISTEN 2183/java
这里报错是因为80端口已经被tomcat占用,需要先关闭tomcat
[root@daixuan nginx]# service tomcat stop
重新启动nginx
[root@daixuan nginx]# /usr/local/nginx/sbin/nginx
[root@daixuan nginx]# ps aux | grep nginx
root30631 0.1 0.0 5028 644 ? Ss 21:160:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 30632 0.1 0.0 5232 980 ? S 21:16 0:00 nginx: worker process
root 30636 0.0 0.0 6048 780 pts/0 S+ 21:16 0:00 grep nginx
三、测试PHP解析
1、修改nginx配置文件,使其支持php
[root@daixuan nginx]# vim /usr/local/nginx/conf/nginx.conf
把这一段注释去掉,让其生效,/usr/local/nginx/html是PHP的文件目录
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
[root@daixuan nginx]# /usr/local/nginx/sbin/nginx -s reload //重新加载nginx配置文件
[root@daixuan ~]# cd /usr/local/nginx/html
[root@daixuan html]# vim info.php
<?php
phpinfo();
?>
浏览器中可以访问:
http://192.168.101.230/info.php