2331 发表于 2016-1-7 09:56:36

LNMP环境搭建

LNMP环境搭建(基于Centos6.7 32位Linux操作系统)

一、linux环境安装


二、MySQL安装

三、PHP编译安装
注意:针对Nginx的php安装和针对apache的php安装是有区别的,因为Nginx中的php是以fastcgi的方式结合nginx的,可以理解为nginx代理了php的fastcgi,而apache是把php作为自己的模块来调用的

1、下载PHP源码包,解压
# cd /usr/local/src
# wget http://cn2.php.net/distributions/php-5.4.37.tar.bz2
# tar jxvf php-5.4.37.tar.bz2
# ls
php-5.4.37

2、创建相关账户
# useradd -s /sbin/nologin php-fpm

3、配置编译选项
注意:由于之前在该虚拟机上安装了LAMP,安装过php,这里需要先删除
# rm -rf /usr/local/php/
# cd 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
# echo $?   //结果为0说明没有报错
0
这里如果有报错,可能是某些库没有安装,例如:安装libcurl库:yum install -y libcurl-delevel

4、编译PHP
# make
# echo $?0注:如果这里遇到这样的错误,解决方法是:yum install -y libtool-ltdl-devel
/usr/bin/ld: cannot find -lltdl
collect2: ld returned 1 exit status
make: ***

5、安装PHP
# make install

6、修改配置文件
# cp php.ini-production /usr/local/php/etc/php.ini
# cd /usr/local/php/etc/   # mv php-fpm.conf.default php-fpm.conf# lspear.confphp-fpm.confphp.ini保存配置文件后,检验配置是否正确的方法为:# /usr/local/php/sbin/php-fpm -t NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
test is successful字样,说明配置没有问题

7、拷贝启动脚本
# cd /usr/local/src/php-5.4.37
首先是拷贝一个php的启动脚本到/etc/init.d/下:
# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
# chmod 755 /etc/init.d/php-fpm
添加php-fpm到服务列表中,让它开机启动
# chkconfig --add php-fpm
# chkconfig php-fpm on
# service php-fpm start
Starting php-fpmdone
查看php-fpm的进程:
# netstat -lnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local AddressForeign Address   State       PID/Program name
tcp   0    0   127.0.0.1:9000   0.0.0.0:*      LISTEN      19901/php-fpm
# 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-fpm199020.00.1260842644 ?S20:17   0:00 php-fpm: pool www   php-fpm199030.00.1260842644 ?   S    20:17   0:00 php-fpm: pool www   
root    199260.00.0   6048   780 pts/0   S+   20:20   0:00 grep php-fpm

二、Nginx编译安装

1、下载解压Nginx
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.8.0.tar.gz
# tar zxvf nginx-1.8.0.tar.gz
# ls
nginx-1.8.0
nginx-1.8.0.tar.gz

2、配置编译选项
# cd 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.
# echo $?
1
发现有错误,解决方法,安装pcre库,重新配置编译选项
# yum install -y pcre-devel
# echo $?
0

3、编译Nginx
#make

4、安装
#make install

5、启动Nginx
# cd /usr/local/nginx/
# ls sbin/nginx
sbin/nginx
启动nginx,报错:
# /usr/local/nginx/sbin/nginx
nginx: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: still could not bind()
# netstat -lnp
tcp00 ::ffff:127.0.0.1:8005:::*    LISTEN      2183/java
tcp00 :::8009            :::*   LISTEN      2183/java
tcp00 :::80             :::*    LISTEN      2183/java
这里报错是因为80端口已经被tomcat占用,需要先关闭tomcat
# service tomcat stop
重新启动nginx
# /usr/local/nginx/sbin/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   306320.10.0   5232   980 ?S    21:16   0:00 nginx: worker process
root   306360.00.0   6048   780 pts/0    S+   21:16   0:00 grep nginx

三、测试PHP解析
1、修改nginx配置文件,使其支持php

# vim /usr/local/nginx/conf/nginx.conf
把这一段注释去掉,让其生效,/usr/local/nginx/html是PHP的文件目录
location ~ \.php$ {
      root         html;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_indexindex.php;
      fastcgi_paramSCRIPT_FILENAME/usr/local/nginx/html$fastcgi_script_name;
      include      fastcgi_params;
      }
# /usr/local/nginx/sbin/nginx -s reload //重新加载nginx配置文件
# cd /usr/local/nginx/html
# vim info.php
<?php
phpinfo();
?>
浏览器中可以访问:
http://192.168.101.230/info.php

页: [1]
查看完整版本: LNMP环境搭建