Centos7安装nginx
下载
官网下载地址:http://nginx.org/en/download.html
Mainline version 开发版
Stable version 稳定版
Legacy versions 历史版
鼠标移动到你要选择的版本超链接上点右键 复制链接地址
下载:wget https://nginx.org/download/nginx-1.16.0.tar.gz
安装
移走:mv ./nginx-1.16.0.tar.gz /usr/local/
解压:tar -zxvf nginx-1.16.0.tar.gz
进去:cd nginx-1.16.0
配置:./configure –with-http_stub_status_module –with-http_ssl_module
安装:make && make install
配置
打开配置文件:vim /usr/local/nginx/conf/nginx.conf
一个监听是一个server{….}段落,里面默认的server代码块可以删掉。
当你在你的域名控制台(阿里云或腾讯云或百度云等等)中设置域名解析成当前服务器IP时,你就可以在这里配置域名监听。
比如下面这段:
server { listen 80; server_name www.abc.com abc.com; root /www/abc; location / { index.html; } }
当浏览器访问 www.abc.com时,由于域名解析指向当前服务器,所以网络请求会发往当前服务器的80端口,nginx监听到请求后,会去找到root指定的地址 也就是/www/abc目录,将目录中的index.html返回给请求方,此时浏览器收到返回,解析标签,展示在页面上。
命令
编写服务脚本:vim /etc/init.d/nginx
粘贴以下代码:
#!/bin/bash # nginx Startup script for the Nginx HTTP Server # it is v.0.0.2 version. # chkconfig: - 85 15 # description: Nginx is a high-performance web and proxy server. # It has a lot of features, but it's not for everyone. # processname: nginx # pidfile: /usr/local/nginx/logs/nginx.pid # config: /usr/local/nginx/conf/nginx.conf nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf nginx_pid=/usr/local/nginx/logs/nginx.pid RETVAL=0 prog="nginx" # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "${NETWORKING}" = "no" ] && exit 0 [ -x $nginxd ] || exit 0 # Start nginx daemons functions. start() { if [ -e $nginx_pid ];then echo "nginx already running...." exit 1 fi echo -n $"Starting $prog: " daemon $nginxd -c ${nginx_config} RETVAL=$? echo [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx return $RETVAL } # Stop nginx daemons functions. stop() { echo -n $"Stopping $prog: " killproc $nginxd RETVAL=$? echo [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid } # reload nginx service functions. reload() { echo -n $"Reloading $prog: " #kill -HUP `cat ${nginx_pid}` killproc $nginxd -HUP RETVAL=$? echo } # See how we were called. case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; status) status $prog RETVAL=$? ;; *) echo $"Usage: $prog {start|stop|restart|reload|status|help}" exit 1 esac exit $RETVAL
PS:经常粘贴前两行贴不全,小心检查。
设置权限:chmod 755 /etc/init.d/nginx
自启配置:vim /etc/rc.local
在文件末尾新增一行:/usr/local/nginx/sbin/nginx
服务命令启动:/etc/init.d/nginx start
开机自启:chkconfig nginx on
可使用的命令:
systemctl status nginx
systemctl start nginx
systemctl stop nginx
systemctl reload nginx
systemctl restart nginx