Lnmp 环境的搭建还是非常简单的,之前由于博客迁移等原因,导致丢失了好多博文,这次重新整理记录一下。
Lnmp 即:Linux 、Nginx 、MySQL 、PHP
Lnmp 是一套 Web 环境,Linux 作为底层操作系统,Nginx 提供 web 服务,Mysql 提供数据库服务,PHP 负责解析 PHP 代码。
强烈建议宿主机内存大于、等于 1G ,否则建议还是安装低版本的 Mysql 跟 PHP !!!
一、Nginx
下载地址: # 现在最新稳定版已经发展到了 1.8.0
shell > yum -y install gcc gcc-c++ make wget zlib-devel pcre-devel openssl-devel
shell > wget
shell > tar zxf nginx-1.8.0.tar.gz
shell > cd nginx-1.8.0
shell > ./configure --prefix=/usr/local/nginx ; make ; make install # --prefix 参数指定 Nginx 安装路径
shell > /usr/local/nginx/sbin/nginx # 启动 Nginx
shell > netstat -anpt | grep nginx # 确认是否启动
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7520/nginx
shell > curl -I # 访问成功
HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Fri, 07 Aug 2015 12:08:14 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Fri, 07 Aug 2015 12:04:38 GMT
Connection: keep-alive
ETag: "55c49ed6-264"
Accept-Ranges: bytes
shell > iptables -I INPUT -p tcp --dport 80 -j ACCEPT
shell > service iptables save
## 至此 Nginx 就可以外网通过浏览器访问了
三、PHP
shell > yum -y install epel-release
shell > yum -y install gd-devel libtool libjpeg-devel libpng-devel freetype-devel libxml2-devel zlib-devel bzip2-devel libcurl-devel libxslt-devel openssl-devel glibc-devel glib2-devel libmcrypt-devel
shell > wget
shell > tar zxf php-5.6.11.tar.gz
shell > cd php-5.6.11
shell > ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php \
--with-mysql=/usr/local/mysql/ --with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-gd --with-xsl --with-bz2 --with-zlib --with-curl --with-pear --without-iconv --with-mcrypt \
--with-gettext --with-openssl --with-libxml-dir --with-png-dir --with-jpeg-dir --with-freetype-dir \
--with-libdir=lib64 --enable-ftp --enable-fpm --enable-exif --enable-soap --enable-bcmath --enable-calendar \
--enable-sockets --enable-mbstring --enable-gd-native-ttf --disable-rpath --disable-debug
# 这堆参数怎么说呢: 你也不必太在意,能解决大多数需求,有几个需要解释一下
# --prefix= 指定安装路径
# --with-config-file-path 指定 php.ini 存放位置
# --with-mysql 指定 Mysql 安装路径
# --enable-fpm Nginx 连接 php 全靠这个东西,如果没有它,你的 Nginx 就不能解析 php
shell > make ; make install
shell > cp /usr/local/src/php-5.6.11/php.ini-production /usr/local/php/php.ini
shell > cp /usr/local/src/php-5.6.11/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
shell > chmod a+x /etc/init.d/php-fpm
shell > cat /usr/local/php/etc/php-fpm.conf.default > /usr/local/php/etc/php-fpm.conf
shell > chkconfig --add php-fpm # 加入开机启动
shell > chkconfig --level 35 php-fpm on
shell > service php-fpm start # 启动 php-fpm
shell > netstat -anpt | grep php-fpm
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 71992/php-fpm
四、让 Nginx 支持 PHP
shell > vim /usr/local/nginx/conf/nginx.conf # 修改配置文件,使其可以解析 PHP
server {
listen 80;
server_name localhost;
location / {
root html;
index index.php index.html index.htm; # 加入 index.php
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
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;
}
}
# 当然,这只是让 Nginx 支持 PHP 而已,实际工作中可以还有更多的配置
## 创建测试页面 ( echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/info.php ) , 重启 Nginx 、php-fpm 放问测试!( 好久前测试记得需要 iptables -I INPUT -i io -j ACCEPT )
五、报错汇总:( 记录一些常见的错误及解决方法 )
1、没有安装 gcc 导致报错 ( yum -y install gcc )
checking for OS
+ Linux 2.6.32-504.el6.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
2、没有安装 pcre-devel 导致报错 ( yum -y install pcre-devel )
./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.
3、没有安装 zlib-devel 导致报错 ( yum -y install zlib-devel )