【Linux】nginx安装
1、官网下载nginx安装压缩包,网址: https://nginx.org/en/download.html
可以使用下面命令直接下载:wget -c https://nginx.org/download/nginx-1.10.1.tar.gz
2、tar -zvxf nginx-1.17.3.tar.gz
cd nginx-1.17.3/
3、其实在 nginx-1.17.3 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。
a.使用默认配置
./configure
b.自定义配置(不推荐)
./configure \
--prefix=/usr/local/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录
4、编译安装
make
make install
5、启动、停止nginx
cd /usr/local/nginx/sbin/
./nginx
./nginx -s stop
./nginx -s quit
./nginx -s reload
6、重启nginx
a、先停止再启动(推荐):
对nginx进行重启相当于先停止再启动,执行如下
./nginx -s quit
./nginx
b、重新加载配置文件:
当nginx的配置文件nginx.conf修改之后,要想让配置生效需要重启nginx,使用-s reload不用先停止nginx再启动
./nginx -s reload
7、开机自启动(不推荐,后面有设置成系统服务并设置自启动)
vi /etc/rc.local
增加一行 /usr/local/nginx/sbin/nginx
设置执行权限 chmod 755 rc.local
8、注册成服务
vi /etc/init.d/nginx #创建nginx脚本文件
添加如下内容:
#! /bin/bash
chkconfig: - 85 15
PATH=/usr/local/nginx
DESC="nginx daemon"
NAME=nginx
DAEMON=$PATH/sbin/$NAME
CONFIGFILE=$PATH/conf/$NAME.conf
PIDFILE=$PATH/logs/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
set -e
[ -x "$DAEMON" ] || exit 0
do_start() {
$DAEMON -c $CONFIGFILE || echo -n "nginx already running"
}
do_stop() {
$DAEMON -s stop || echo -n "nginx not running"
}
do_reload() {
$DAEMON -s reload || echo -n "nginx can't reload"
}
case "$1" in
start)
echo -n "Starting $DESC: $NAME"
do_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
do_stop
echo "."
;;
reload|graceful)
echo -n "Reloading $DESC configuration..."
do_reload
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
do_stop
do_start
echo "."
;;
*)
echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
exit 3
;;
esac
exit 0
chmod a+x /etc/init.d/nginx #设置执行权限
chkconfig --add nginx #注册成服务
chkconfig nginx on #设置开机自动启动
验证:shutdown -h 0 -r #关机重启
netstat -anp | grep nginx #查看服务是否起来
对nginx服务执行停止/启动/重新读取配置文件操作
#启动nginx服务
systemctl start nginx.service
#停止nginx服务
systemctl stop nginx.service
#重启nginx服务
systemctl restart nginx.service
#重新读取nginx配置(这个最常用, 不用停止nginx服务就能使修改的配置生效)
systemctl reload nginx.service