Nginx是一种最重要的技能,在公司里可以不会其他的,nginx精通了你也可以所向披靡。 这篇文章也许是有史以来最长的文章了,没有之一。
LNMP=Linux Nginx MysqlPHP nginx在工作中是非常重要的web服务器,它是一个高性能的 HTTP 和 反向代理 服务器,也是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,百度BWS、新浪、网易、腾讯等都是使用的是nginx
Nginx的工作原理 Nginx 本身只支持静态页面的处理,当客户端访问php页面的时候,nginx会将php转到php-fpm也处理, php-fpm服务会把php页面解析成html文件给nginx处理,nginx返回给客户端处理 本次编译所需要的软件版本: libmcrypt-2.5.8 mysql-1.60 nginx-1.8.0 pcre-8.37 php-5.6.13 jpegsrc.v7 编译前安装环境: 1
2
| [iyunv@Xinsz08 ~]# yum install gcc gcc-c++autoconf automake zlib zlib-devel openssl openssl-devel pcre* -y
root@Xinsz08 ~]# tar xf pcre-8.37.tar.bz2 -C /usr/local/src/ //解压此安装包即可,不需要安装
|
解压编译安装nginx: 1
2
| [iyunv@Xinsz08 ~]# tar xvf nginx-1.9.4.tar.gz-C /usr/local/src/ ;
cd /usr/local/src/nginx-1.9.4
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [root@Xinsz08nginx-1.8.0]# ./configure --prefix=/usr/local/nginx
--with-http_dav_module //
#启用支持(增加PUT,DELETE,MKCOL:创建集合,COPY和MOVE方法)
--with-http_stub_status_module //
#启用支持(获取Nginx上次启动以来的工作状态)
--with-http_addition_module
#启用支持(作为一个输出过滤器,支持不完全缓冲,分部分相应请求)
--with-http_sub_module
#启用支持(允许一些其他文本替换Nginx相应中的一些文本)
--with-http_flv_module
#启用支持(提供支持flv视频文件支持)
--with-http_mp4_module
#启用支持(提供支持mp4视频文件支持,提供伪流媒体服务端支持)
--with-pcre=/usr/local/src/pcre-8.37
|
1
| root@Xinsz08 nginx-1.9.4]# make - j 3 ;make install ; cd
|
1
2
| [iyunv@Xinsz08 ~]# useradd -M -u 8001-s /sbin/nologinnginx //
用于运行Nginx的用户
|
配置Nginx支持php文件 1
2
| [iyunv@Xinsz08 ~]# vim/usr/local/nginx/conf/nginx.conf
//Nginx主配置文件
|
#user nobody; user nginx nginx; #添加此行 …… 并在所支持的主页面格式中添加php格式的主页,类似如下: location / { root html; index index.php index.html index.htm; }
#location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} #找到上面这段内容,将这段内容复制,去掉#且修改为如下 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; #将scripts修改为nginx的html,即Nginx页面目录,因为要处理的php文件也在这个目录下 include fastcgi_params; }
启动Nginx 1
| [iyunv@Xinsz08 ~]#/usr/local/nginx/sbin/nginx
|
1
2
| [iyunv@localhost conf]# cd /etc/init.d/
[iyunv@localhost init.d]# vi nginxd (书写启动的shell)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
| #!/bin/bash
#Author ethnicity(Just a check of others)
#Time 2011-9-24
nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf
nginx_pid=/usr/local/nginx/logs/nginx.pid
RETVAL=0
prog="nginx"
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0
[ -x $nginxd ] || exit 0
# Start nginx daemons functions.
start() {
if [ -e $nginx_pid ];then
echo "nginx already running...."
exit 1
fi
echo -n $"Starting $prog: "
daemon $nginxd -c ${nginx_config}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
return $RETVAL
}
# Stop nginx daemons functions.
stop() {
echo -n $"Stopping $prog: "
killproc $nginxd
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid
}
# reload nginx service functions.
reload() {
echo -n $"Reloading $prog: "
#kill -HUP `cat ${nginx_pid}`
killproc $nginxd -HUP
RETVAL=$?
echo
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
start
;;
status)
status $prog
RETVAL=$?
;;
*)
echo $"Usage: $prog {start|stop|restart|reload|status|help}"
exit 1
esac
exit $RETVAL
|
1
| [iyunv@localhost init.d]# chmod a+x nginxd
|
1
| [iyunv@localhost ~]# service nginxd restart
|
使用浏览器测试 http://IP
编译安装MySql
1
2
3
4
5
6
| tar zvxf mysql-5.1.60.tar.gz -C /usr/src
cd /usr/src/mysql-5.1.60/
./configure --prefix=/usr/local/mysql
make && make install
|
配置mysql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| PATH=$PATH:/usr/local/mysql/bin
echo $PATH
mkdir -p /var/run/mysqld
useradd -M -s /sbin/nologin mysql
chown -R mysql /usr/local/mysql/
/usr/local/mysql/bin/mysql_install_db --user=mysql
/usr/local/mysql/bin/mysqld_safe --user=mysql
cd /usr/src/mysql-5.1.60
cp -p support-files/mysql.server /etc/rc.d/init.d/mysqld
chmod a+x /etc/rc.d/init.d/mysqld
chkconfig --add mysqld
chkconfig --list mysqld
ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
netstat -nultp | grep ":3306"
ln -s /usr/local/mysql/bin/mysql /usr/bin/
service mysqld start
mysqladmin -u root password '123456'
|
编译安装PHP 解决依赖 1
2
| [iyunv@Xinsz08~]# yum install -y php-pear* xmlrpc* mhash* gd* libxml* zlib* freetype* libpng-devel* libcurl-devel*
//pear按照一定的分类来管理pear应用代码库
|
1
2
| [iyunv@Xinsz08 ~]# tar xf libmcrypt-2.5.8.tar.bz2 -C /usr/local/src/ ;
cd /usr/local/src/libmcrypt-2.5.8/
|
由于系统默认规定只在/lib、/lib64、/lib/lib64下面找库文件,所以我们需要手动添加进去。 1
| [iyunv@Xinsz08 ~]# vim /etc/ld.so.conf
|
include ld.so.conf.d/*.conf #此行原有 /usr/local/libmcrypt/lib #此行添加 /usr/local/mysql/lib #此行添加 1
2
| [iyunv@Xinsz08 ~]# ldconfig
[iyunv@Xinsz08 ~]# echo 'ldconfig' >>/etc/rc.local
|
编译安装php 1
| [iyunv@Xinsz08~]# tar xf php-5.6.13.tar.bz2 -C /usr/local/src/ ; cd/usr/local/src/php-5.6.13
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
| [iyunv@Xinsz08 php-5.6.13]# ./configure --prefix=/usr/local/php
#设置 php.ini 的搜索路径。默认为 PREFIX/lib
--with-config-file-path=/usr/local/php
--with-mysql=/usr/local/mysql
#mysql安装目录,对mysql的支持
--with-mysqli=/usr/local/mysql/bin/mysql_config
#mysqli扩展技术不仅可以调用MySQL的存储过程、处理MySQL事务,而且还可以使访问数据库工作变得更加稳定。是一个数据库驱动
--with-iconv-dir
#种字符集间的转换
--with-freetype-dir
#打开对freetype字体库的支持
--with-jpeg-dir
#打开对jpeg图片的支持
--with-png-dir
#打开对png图片的支持
--with-zlib
#打开zlib库的支持,实现GZIP压缩输出
--with-libxml-dir=/usr
#打开libxml2库的支持,libxml是一个用来解析XML文档的函数库
--enable-xml
#支持xml文档
--disable-rpath
--enable-bcmath
--enable-shmop
--enable-sysvsem
--enable-inline-optimization
--with-curl
--enable-mbregex
--enable-fpm
--enable-mbstring
--with-gd
--enable-gd-native-ttf
--with-openssl
--with-mhash
--enable-pcntl
--enable-sockets
--with-xmlrpc
--enable-zip
--enable-soap
--with-mcrypt=/usr/local/libmcrypt
[iyunv@Xinsz08 php-5.6.13]#make -j 3 && make install ; cd
|
配置php和php-fpm PHP配置文件: 1
| [iyunv@Xinsz08~]# cp /usr/local/src/php-5.6.13/php.ini-production /usr/local/php/php.ini
|
PHP-FPM配置文件:
1
| [iyunv@Xinsz08~]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
|
修改 /usr/local/php/etc/php-fpm.conf 运行用户和组改为nginx
PHP-FPM启动脚本
1
2
3
4
| [iyunv@Xinsz08~]# cp /usr/local/src/php-5.6.13/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[iyunv@Xinsz08~]# chmod +x /etc/init.d/php-fpm
[iyunv@Xinsz08 ~]# chkconfig php-fpm on
[iyunv@Xinsz08 ~]# /etc/init.d/php-fpmstart
|
测试LNMP的PHP支持
1
2
| [iyunv@Xinsz08~]#
echo "<?php phpinfo(); ?>" > /usr/local/nginx/html/index.php
|
浏览器访问:http://IP
|