1. 老规矩, 来点开场白: Nginx 简单介绍
Nginx 是一款自由的, 开源的, 高性能的 HTTP 服务器和反向代理服务器; 同时也是一个 IMAP,POP3,SMTP 代理服务器; Nginx 可以作为一个 HTTP 服务器进行网站的发布处理, 另外 Nginx 可以作为反向代理进行负载均衡的实现.
更多关于 Nginx 介绍以及作用请自行百度
2. 安装 Nginx 基础依赖包
[root@nginx ~]# yum install gcc gcc-c++ pcre-devel zlib-devel openssl-devel -y
3. 下载相关的软件包
3.1 下载 nginx 稳定版
注: 进入 Nginx 安装包下载地址复制您想要的版本链接, 我这里以 Nginx-1.14.0 为例.
- [root@nginx~]# cd /usr/local/src/
- [root@ nginx src]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
3.2 下载 nginx-sticky 模块 (可选安装, 应用于集群)
作用: 后端做负载均衡解决 session sticky 问题.
[root@nginx src]# wget https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/get/master.tar.gz
3.3 下载 ngx_cache_purge 模块 (可选安装)
作用: 将 ngx_cache_purge 编译到到 Nginx 中, 用来清除指定 URL 的缓存.
- (官网地址 http://labs.frickle.com/nginx_ngx_cache_purge/ :http://labs.frickle.com/nginx_ngx_cache_purge/)
- [root@nginx src]# wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
4. 添加 Nginx 用户和组
- [root@nginx ~]# groupadd www
- [root@nginx ~]# useradd -g www www -s /sbin/nologin
注: 创建 nginx 运行用户 www 并加入到 www 组, 不允许 www 用户直接登录系统.
5. 解压 Nginx 源码包并进入解压目录, 进行编译安装
- [root@nginx src]# tar zxf nginx-1.14.0.tar.gz
- # 注意 3.2 的可选项
- [root@nginx src]# tar zxf ngx_cache_purge-2.3.tar.gz
- # 注意 3.3 的可选项
- [root@nginx src]# tar zxf nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz
- [root@nginx src]# cd nginx-1.14.0/
注:'='号后面是自己安装包的绝对路径, 和配置文件路径.
- [root@nginx-master nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
- > --user=www --group=www
- > --with-http_stub_status_module
- > --with-http_realip_module --with-http_ssl_module
- > --with-http_gzip_static_module
- > --http-client-body-temp-path=/var/tmp/nginx/client
- > --http-proxy-temp-path=/var/tmp/nginx/proxy
- > --http-fastcgi-temp-path=/var/tmp/nginx/fcgi
- # 注意 3.2 的可选项
- > --with-pcre --add-module=/usr/local/src/ngx_cache_purge-2.3
- # 注意 3.3 的可选项
- > --add-module=/usr/local/src/nginx-goodies-nginx-sticky-module-ng-08a395c66e42
执行完编译后:
[root@nginx nginx-1.14.0]# make && make install
注: Nginx 的所有模块必须在编译的时候添加, 不能再运行的时候动态加载.
6. 优化 Nginx 程序的执行路径
- [root@nginx nginx-1.14.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
- [root@nginx nginx-1.14.0]# nginx -t
如果出现以下问题:
需要创建此目录:
[root@nginx nginx-1.14.0]# mkdir -p /var/tmp/nginx/client
7. 配置 Nginx 开机自启动等命令, 方便我们管理 Nginx
原始管理 nginx 的命令, 不是很方便!
- [root@nginx-master ~]# nginx 启动 nginx
- [root@nginx-master ~]# nginx -s reload 重启 nginx
- [root@nginx-master ~]# nginx -s stop 停止 nginx
接下来设置常用的管理命令:
1) 创建开机启动命令脚本文件:
[root@nginx ~]# VIM /etc/init.d/nginx
2) 在这个 Nginx 文件中插入一下启动脚本代码:
----------------------------------- 脚本如下 -----------------------------------
- #! /bin/bash
- # chkconfig: - 85 15
- PATH=/usr/local/nginx
- DESC="nginx daemon"
- NAME=nginx
- DAEMON=$PATH/sbin/$NAME
- CONFIGFILE=$PATH/conf/$NAME.conf
- PIDFILE=$PATH/logs/$NAME.pid
- scriptNAME=/etc/init.d/$NAME
- set -e
- [ -x "$DAEMON" ] || exit 0
- do_start() {
- $DAEMON -c $CONFIGFILE || echo -n "nginx already running"
- }
- do_stop() {
- $DAEMON -s stop || echo -n "nginx not running"
- }
- do_reload() {
- $DAEMON -s reload || echo -n "nginx can't reload"
- }
- case "$1" in
- start)
- echo -n "Starting $DESC: $NAME"
- do_start
- echo "."
- ;;
- stop)
- echo -n "Stopping $DESC: $NAME"
- do_stop
- echo "."
- ;;
- reload|graceful)
- echo -n "Reloading $DESC configuration..."
- do_reload
- echo "."
- ;;
- restart)
- echo -n "Restarting $DESC: $NAME"
- do_stop
- do_start
- echo "."
- ;;
- *)
- echo "Usage: $scriptNAME {start|stop|reload|restart}">&2
- exit 3
- ;;
- esac
- exit 0
- [root@nginx ~]# cd /etc/init.d/
3) 设置所有人都有对这个启动脚本 nginx 文件的执行权限:
[root@nginx init.d]# chmod a+x nginx
4) 把 nginx 加入系统服务中:
[root@nginx init.d]# chkconfig --add nginx
5) 把服务设置为开机启动:
[root@nginx init.d]# chkconfig nginx on
注: reboot 重启系统生效!
6) 重启后, 就可以执行如下命令进行管理了
- systemctl start nginx.service # 启动 nginx
- systemctl stop nginx.service # 结束 nginx
- systemctl restart nginx.service # 重启 nginx
- systemctl enable nginx.service # 开机启动
8. 如果出现 Nginx 启动不了, 请尝试以下方法
1) 如果之前安装过 httpd, 侧需要先卸载 httpd, 否则启动失败, 除非修改 Nginx 启动端口.
[root@nginx ~]# yum remove httpd -y
2) 如果还是启动不来, 再试试这个方法: 杀死 Nginx 相关的进程之后, 启动 Nginx
- [root@nginx ~]#ps -A | grep nginx
- [root@nginx ~]#kill -9 pid1
- [root@nginx ~]#kill -9 pid2
3) 如果还是不行, 查看日志信息解决.
9. 启动 Nginx 并访问测试
注: 后续继续更新 Nginx 优化及反向代理功能!
来源: https://www.cnblogs.com/l-hh/p/10594415.html