安装所需环境 Nginx 是 C语言 开发,建议在 Linux 上运行,当然,也可以安装 Windows 版本,本篇则使用 CentOS 7 作为安装环境。
一. gcc 安装 安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:
二. PCRE pcre-devel 安装 PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:
1 yum install -y pcre pcre-devel
三. zlib 安装 zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。
1 yum install -y zlib zlib-devel
四. OpenSSL 安装 OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。 nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。
1 yum install -y openssl openssl-devel
所有工具和依赖一起安装
1 yum install -y openssl openssl-devel zlib zlib-devel pcre pcre-devel gcc-c++
官网下载
直接下载.tar.gz安装包,地址:https://nginx.org/en/download.html
使用wget命令下载(推荐)。确保系统已经安装了wget,如果没有安装,执行 yum install wget 安装。
1 wget -c https://nginx.org/download/nginx-1.15.12.tar.gz
我下载的是1.12.0版本,这个是目前的稳定版。
解压 依然是直接命令:
1 2 tar -zxvf nginx-1.15.12.tar.gz cd nginx-1.15.12
配置 其实在 nginx-1.15.12 版本中你就不需要去配置相关东西,默认就可以了。当然,如果你要自己配置目录也是可以的。
使用默认配置
自定义配置(不推荐)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ./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 \ --with-http_ssl_module \ --with-stream \ --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目录
编译安装
查找安装路径:
启动、停止nginx 1 2 3 4 5 6 7 cd /usr/local /nginx/sbin/./nginx ./nginx -s stop ./nginx -s quit ./nginx -s reload 启动时报80端口被占用: nginx: [emerg] bind () to 0.0.0.0:80 failed (98: Address already in use)
解决办法,查看占用端口的进程
1 2 3 4 5 6 7 8 9 10 ➜ ~ lsof -i:80 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nginx 4214 root 6u IPv4 46826 0t0 TCP *:http (LISTEN) nginx 4216 root 6u IPv4 46826 0t0 TCP *:http (LISTEN) ➜ ~ ps -ef|grep nginx root 4214 1 0 4月27 ? 00:00:00 nginx: master process /usr/bin/nginx root 4216 4214 0 4月27 ? 00:00:00 nginx: worker process root 122099 121530 0 22:06 pts/0 00:00:00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn nginx ➜ ~
./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。
查询nginx进程:
重启 nginx 1.先停止再启动(推荐): 对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:
2.重新加载配置文件: 当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:
启动成功后,在浏览器可以打开页面了
开机自启动 即在rc.local增加启动代码就可以了。
增加一行 /usr/local/nginx/sbin/nginx 设置执行权限:
到这里,nginx就安装完毕了,启动、停止、重启操作也都完成了,当然,你也可以添加为系统服务。
添加系统服务 Centos 6 添加系统服务 nginx源码安装完成后默认不会注册为系统服务,所以需要手工添加系统服务脚本。在/etc/init.d目录下新建nginx文件,并更改权限其即可。
新建nginx文件
填写以下内容(根据自己的实际目录修改):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 # !/bin/bash # nginx Startup script for the Nginx HTTP Server # this script create it by caffreyxin at 2007.10.15. # it is v.0.0.1 version. # if you find any errors on this scripts, please contact caffreyxin. # and send mail to xinyflove at sina dot com. # # 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 /var/run/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
或https://github.com/xinyflove/MyDocument/blob/master/Nginx/nginx tip: 根据自己实际安装目录,修改这两行:
1 2 nginxd=/usr/local/nginx/sbin/nginx nginx_config=/usr/local/nginx/conf/nginx.conf
修改文件权限 1 chmod 755 /etc/init.d/nginx
设置开机启动
查看开机启动的服务
命令 启动服务:service nginx start
停止服务:service nginx stop
重启服务:service nginx reload
CentOS 7 添加系统服务
新建 nginx.server文件
1 vi /usr/lib/systemd/system/nginx.service
填写以下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID PrivateTmp=true [Install] WantedBy=multi-user.target
启动nginx服务
1 systemctl start nginx.service
设置开机自启动
1 systemctl enable nginx.service
停止开机自启动
1 systemctl disable nginx.service
查看服务当前状态
1 systemctl status nginx.service
重新启动服务
1 systemctl restart nginx.service
查看所有已启动的服务
1 systemctl list-units --type=service
如下出现Failed to execute operation: Access denied无权限错误,则需关闭selinux,操作如下:
1 2 3 4 5 6 7 8 # vi /etc/sysconfig/selinux … SELINUX=disabled … # setenforce 0 # getenforce Permissive
新旧系统服务命令对比
任务
旧指令
新指令
使某服务自动启动
chkconfig –level 3 nignx on
systemctl enable nignx.service
使某服务不自动启动
chkconfig –level 3 nignx off
systemctl disable nignx.service
检查服务状态
service nignx status
systemctl status nignx.service (服务详细信息) systemctl is-active nignx.service (仅显示是否 Active)
显示所有已启动的服务
chkconfig –list
systemctl list-units –type=service
启动某服务
service nignx start
systemctl start nignx.service
停止某服务
service nignx stop
systemctl stop nignx.service
重启某服务
service nignx restart
systemctl restart nignx.service