nginx安装
大约 3 分钟
1. nginx安装
1.1 yum安装
#添加yum源
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#安装nginx
yum -y install nginx
#启动
systemctl start nginx
service nginx start
#停止
systemctl stop nginx
service nginx stop
#查看状态
systemctl status nginx
service nginx status
#开机自启
systemctl enable nginx
chkconfig nginx off
#配置文件路径
/etc/nginx/nginx.conf
#html路径
/usr/share/nginx/html
#卸载
#删除相关文件
rm -rf /usr/sbin/nginx
rm -rf /etc/nginx
rm -rf /etc/init.d/nginx
#卸载
yum remove nginx
1.2 源码安装
1.1 源码包下载
传送门
nginx官网
wget https://nginx.org/download/nginx-1.24.0.tar.gz
提示
错误: 无法验证 nginx.org 的由 “/C=US/O=Let's Encrypt/CN=R3” 颁发的证书:
颁发的证书还未生效。
使用以下命令
wget https://nginx.org/download/nginx-1.24.0.tar.gz --no-check-certificate
1.2 依赖包安装
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
#解压
tar -xvf nginx-1.24.0.tar.gz
#编译
cd nginx-1.24.0
./configure --with-http_ssl_module
#编译可选参数
默认安装位置 /usr/local/nginx/
可以使用命令指定安装位置,其他参数也一样
./nginx-1.24.2/configure --prefix=/home --with-http_ssl_module
其他可选参数
--prefix=PATH:指定 nginx 的安装目录
--conf-path=PATH:指定 nginx.conf 配置文件路径
--user=NAME:nginx 工作进程的用户
--with-pcre:开启 PCRE 正则表达式的支持
--with-http_ssl_module:启动 SSL 的支持
--with-http_stub_status_module:用于监控 Nginx 的状态
--with-http-realip_module:允许改变客户端请求头中客户端 IP 地址
--with-file-aio:启用 File AIO
--add-module=PATH:添加第三方外部模块
#编译
make
#安装
make install
#启动
cd /usr/local/nginx/sbin
./nginx
#停止
./nginx -s stop
./nginx -s quit
#重启
./nginx -s reload
常用命令
nginx -c /../nginx.conf # 指定配置文件启动
nginx -t # 验证配置是否正确
nginx -V # 查看Nginx的详细的版本号
nginx -v # 查看Nginx的简洁版本号
nginx -s reload # 重启Nginx(重新读取配置)
nginx -s reopen # 重新打开日志文件
nginx -t -c /path/to/nginx.conf # 测试Nginx配置文件是否正确
nginx -s stop # 快速停止Nginx
nginx -s quit # 完整有序地停止Nginx
卸载
直接删除 /usr/local/nginx/
2. nginx配置
2.1 开机自启
vim /etc/init.d/nginx
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
#nginx位置
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case
$1 in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|reload|configtest|status|restart|condrestart|try-restart|force-reload}"
exit 2
esac
#授予脚本执行权限:
sudo chmod +x /etc/init.d/nginx
#将 Nginx 添加到启动项中:
sudo chkconfig --add nginx
#启用 Nginx 服务以便在系统启动时自动启动:
sudo chkconfig nginx on