centos7 安装 nginx-1.18.0 并设置开机自启动
一、到官网下载nginx
Mainline version: nginx主力版本,为开发版
Stable version: 稳定版,在生产环境中选择此版本进行安装
Legacy versions:历史版本
nginx 下载地址:http://nginx.org/download/nginx-1.18.0.tar.gz
2、 安装依赖库
1、安装gcc环境
命令:yum install gcc-c++
2、Nginx的http模块依赖pcre
命令:yum install -y pcre pcre-devel
3、安装依赖的解压包。
命令:yum install -y zlib zlib-devel
4、ssl 功能需要 openssl 库,安装 openssl。
命令:yum install -y openssl openssl-devel
3、解压安装nginx
1、解压下载的安装包,命令:tar -zxvf nginx-1.18.0.tar.gz
2、进入nginx-1.18.0 文件夹,并创建安装目录 /usr/local/nginx ,命令: mkdir /usr/local/nginx
3、执行命令编译,命令:./configure --prefix=/usr/local/nginx 【执行安装目录/usr/local/nginx】
注意:如果要使用nginx https ,则使用以下配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
4、执行命令安装,命令:make & make install
5、启动nginx,并指定对应的配置文件, 使用-c指定配置文件
命令:/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
6、安装完后输入主机IP,测试是否正常:
7、针对需要修改对应的配置文件,然后重新刷新下配置即可
配置文件目录:/usr/local/nginx/conf/nginx.conf
刷新配置命令:/usr/local/nginx/sbin/nginx -s reload
4、配置开机自启动
新建shell脚本,内容如下,文件保存路径 /etc/init.d/nginx
#!/bin/sh # # chkconfig: 2345 10 20 #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 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 killall -9 nginx } 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|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
需要注意的配置: