Nginx 是一款轻量级的 Web 服务器 、反向代理服务器及电子邮件(IMAP/POP3)代理服务器。
安装所需依赖
yum -y install gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre*
下载PCRE库
cd /usr/local/
wget –no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
注:解压即可,不用安装,Nginx安装时指定pcre的解压路径即可
tar xf pcre-8.43.tar.gz
下载nginx源码包
tar xf nginx-1.16.0.tar.gz
cd nginx-1.16.0
创建nginx用户
useradd -M -s /sbin/nologin nginx
生成Makefile
./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/pcre-8.43 \
--user=nginx \
--group=nginx
make -j 2 && make install
查看目录文件
ll /usr/local/nginx/
drwxr-xr-x. 2 root root 4096 Jul 21 21:52 conf //Nginx相关配置文件
drwxr-xr-x. 2 root root 38 Jul 21 21:52 html //网站根目录
drwxr-xr-x. 2 root root 6 Jul 21 21:52 logs //日志文件
drwxr-xr-x. 2 root root 18 Jul 21 21:52 sbin //Nginx启动脚本
配置Nginx支持php文件
cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
修改用户为nginx
user nginx nginx;
启用PHP支持
将以下内容前的注释去掉
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;
}
检查配置文件语法是否有错误
nginx -t
查看nginx版本
nginx -V
启动nginx服务
/usr/local/nginx/sbin/nginx
优化Nginx启动命令执行路径
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
生成服务脚本
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
查看nginx的运行状态
systemctl status nginx
浏览器访问:
Nginx常用命令
nginx -s stop 快速关闭Nginx,可能不保存相关信息,并迅速终止web服务。
nginx -s quit 平稳关闭Nginx,保存相关信息,有安排的结束web服务。
nginx -s reload 因改变了Nginx相关配置,需要重新加载配置而重载。
nginx -s reopen 重新打开日志文件。
nginx -c filename 为 Nginx 指定一个配置文件,来代替缺省的。
nginx -t 不运行,仅仅测试配置文件。nginx 将检查配置文件的语法的正确性,并尝试打开配置文件中所引用到的文件。
nginx -v 显示 nginx 的版本。
nginx -V 显示 nginx 的版本,编译器版本和配置参数。
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论