1、安装nginx
下载安装包 http://nginx.org 官网下载。
2、安装依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
# openssl :开启SSL的时候需要此模块的支持 。
#Zlib:Nginx提供gzip模块,需要zlib的支持
3、解压nginx包
tar -xvf 包名 -C /usr/local/src(解压路径)
4、预编译
./configure --prefix=/usr/local/nginx --with-http_ssl_module
#./configure --prefix=/usr/local/nginx --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-pcre=/usr/local/src/pcre-8.38 --user=nginx --group=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 #需要注意,这里指的是源码,用#./configure --help |grep pcre查看帮助
--with-http_ssl_module #启用支持,安装ssl模块,对https的支持
5、编译并安装
###
make -j 4(4核安装) && make install &&echo ********************************************************* && echo $?
# 输出0:安装成功;输出1或其他:安装失败。
6.Nginx文件目录
ll /usr/local/nginx/
drwxr-xr-x. 2 root root 4096 Apr 14 20:39 conf #Nginx相关配置文件
drwxr-xr-x. 2 root root 4096 Apr 14 20:39 html #网站根目录
drwxr-xr-x. 2 root root 4096 Apr 14 20:39 logs #日志文件
drwxr-xr-x. 2 root root 4096 Apr 14 20:39 sbin #Nginx启动脚本
7. 配置Nginx支持php文件
备份:
# cp /usr/local/nginx/conf/ nginx.conf{,.bak}
修改:
vim /usr/local/nginx/conf/nginx.conf
修改用户为nginx:
2 #user nobody;
3 user nginx nginx;
启用PHP支持
第66行始 修改为:
66 location ~ \.php$ {
67 root html;
68 fastcgi_pass 127.0.0.1:9000;
69 fastcgi_index index.php;
70 fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
71 include fastcgi_params;
72 }
7.1 启动Nginx服务
# /usr/local/nginx/sbin/nginx
7.2 编辑配置环境变量,让系统读到nginx路径
vim /etc/profile.d/nginx.sh
export PATH=/usr/local/nginx/sbin:$PATH
读取变量:
./etc/profile.d/nginx.sh
或者使用软连接也可以:
# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
8.配置启动文件
# centos7,使用systemctl
$ vim /lib/systemd/system/nginx.service # 输入以下代码,并且保存
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
#启动:nginx:systemctl start nginx.service
# centos 6 ,使用service
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 99 2
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -3 $(cat $PIDF)
;;
restart)
$0 stop &> /dev/null
if [ $? -ne 0 ] ; then continue ; fi
$0 start
;;
reload)
kill -1 $(cat $PIDF)
;;
*)
echo "Userage: $0 { start | stop | restart | reload }"
exit 1
esac
exit 0
#配置服务开机自动启动
# chmod +x /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig nginx on