centos7安装nginx
centos7安装nginx
centos7安装nginx有两种方式,这里重点介绍源码包安装
yum安装
添加nginx到yum源中
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
安装nginx
yum install -y nginx
启动nginx
systemctl start nginx
设置开机自启
systemctl enable nginx
源码包安装
首先安装依赖包
安装gcc
yum install -y gcc-c++
安装pcre
yum install -y pcre pcre-devel
安装zlib
yum install -y zlib zlib-devel
安装openssl
yum install -y opsenssl openssl-devel
下载安装nginx
首先到官网:nginx下载链接
下载安装包上传至/usr/local/nginx.这个目录是软件默认安装路径,然后解压缩
tar -zxvf nginx-1.20.2.tar.gz
或者直接在线下载
mkdir /usr/local/nginx
cd /usr/local/nginx
yum install -y wget
wget http://nginx.org/download/nginx-1.20.2.tar.gz
解压缩
tar zxvf nginx-1.20.2.tar.gz
进入nginx目录下
cd nginx-1.20.2
检查平台安装环境
./configure --prefix=/usr/local/nginx
编译并安装
make
make install
启动nginx
/usr/local/nginx/sbin/nginx
防火墙开放80端口
firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
firewall-cmd --reload
看到下面的界面就说明成功了
设置开机启动
因为是用源码编译安装的,所以首先需要手动将nginx添加至服务
vi /etc/init.d/nginx
插入以下内容
#!/bin/sh
# nginx - this script starts and stops the nginx daemin
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# pidfile: /usr/local/nginx/logs/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"
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
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
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|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
进入此目录
cd /etc/init.d
chmod 755 /etc/init.d/nginx
注册成服务
chkconfig -add nginx
设置nginx开机自启
systemctl enable nginx
nginx常用命令
cd /usr/local/nginx/sbin/
./nginx 启动
./nginx -s stop 停止
./nginx -s quit 安全退出
./nginx -s reload 重新加载配置文件
ps aux|grep nginx 查看nginx进程
License:
CC BY 4.0