Centos7安装nginx1.8.0教程
1. 去官网下载安装包
官网下载地址:
https://nginx.org/en/download.html
国内镜像地址:
https://mirrors.huaweicloud.com/nginx/
这里选的是:nginx-1.8.0.tar.gz
2. 上传到服务器并解压压缩包
#上传随意路径,解压
[root@localhost /]# cd /home/package
[root@localhost package]# tar -zxvf nginx-1.8.0.tar.gz
3. 安装依赖
#安装gcc
[root@localhost /]# yum install gcc-c++
#安装PCRE
[root@localhost /]# yum install -y pcre pcre-devel
#安装zlib
[root@localhost /]# yum install -y zlib zlib-devel
#安装openssl
[root@localhost /]# yum install -y openssl openssl-devel
4. 编译安装
[root@localhost /]# cd /home/package/nginx-1.18.0
#配置信息,需要改变自行修改,这里默认
[root@localhost nginx-1.18.0]# ./configure
#编译和安装
[root@localhost nginx-1.18.0]# make && make install
#查找安装位置
[root@localhost nginx-1.18.0]# whereis nginx
nginx: /usr/local/nginx
5. 测试链接
# 进入nginx目录
[root@localhost /]# cd /usr/local/nginx/sbin/
#启动nginx
[root@localhost sbin]# ./nginx
默认80端口 网页用ip访问就可以啦
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
6. 配置自启
配置脚本
vim /etc/init.d/nginx
:wq退出保存
#!/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="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
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
fi
}
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 $prog -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|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
nginx官方脚本地址:https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/
自定义编译安装的nginx,需要根据您的安装路径修改下面这两项配置:
nginx="/usr/sbin/nginx" => nginx="/usr/local/nginx/sbin/nginx"
NGINX_CONF_FILE="/etc/nginx/nginx.conf" => NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
使用脚本指令
./etc/init.d/nginx start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest
授执行权限
chmod a+x /etc/init.d/nginx
配置自启
vim /etc/rc.local
下面追加指令 :wq保存
/etc/init.d/nginx start
7. 相关原生指令
start:启动Nginx服务。使用命令./nginx即可启动Nginx,默认情况下会读取配置文件/etc/nginx/nginx.conf。
stop:停止Nginx服务。使用命令./nginx -s stop可以停止正在运行的Nginx进程。
restart:重启Nginx服务。使用命令./nginx -s restart可以停止当前运行的Nginx进程,并重新启动一个新的进程。
reload:重新加载Nginx配置。使用命令./nginx -s reload可以在不停止服务的情况下重新加载配置文件,使配置生效。
quit:优雅地停止Nginx服务。使用命令./nginx -s quit可以平滑地停止Nginx进程,处理完正在进行的请求后再退出。
test:检查Nginx配置文件是否正确。使用命令./nginx -t可以测试配置文件的语法和逻辑错误。
status:查看Nginx服务的运行状态。使用命令./nginx -s status可以输出Nginx当前的运行状态信息。
8. 卸载nginx
#删除安装目录
rm -rf /usr/local/nginx
总结
本文介绍了nginx1.8.0的安装,喜欢就点个赞收藏一下吧!