1.Nginx 安装环境
nginx 是 C 语言开发,建议在 linux 上运行,本教程使用 Centos6.5 作为安装环境。
- 【进入Linux的root目录下操作】
- 【1.0】cd /root/
- 【1.1】yum install gcc-c++ -y
说明:安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境。
- 【1.2】yum install -y pcre pcre-devel
说明:PCRE(Perl Compatible Regular Expressions)是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。pcre-devel 是使用 pcre 开发的一个二次开发库。nginx 也需要此库。
- 【1.3】yum install -y zlib zlib-devel
说明:zlib 库提供了很多种压缩和解压缩的方式,nginx 使用 zlib 对 http 包的内容进行 gzip,所以需要在 linux 上安装 zlib 库。
- 【1.4】yum install -y openssl openssl-devel
说明:OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书 封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在 ssl 协议上传输 http),所以需要在 linux 安装 openssl 库。
2.Nginx 编译安装
- 【点此下载】https://gitlab.com/pjion/233/linux-nginx.git
- 【首先将 nginx-1.8.0.tar.gz 拷贝至 linux 服务器目录:/usr/local/tmp】
- 【2.0】cd /usr/local/tmp
- 【2.1】tar -zxvf nginx-1.8.0.tar.gz(解压nginx-1.8.0.tar.gz 至 /usr/local/tmp)
- 【2.21】mkdir /var/temp(在/var 下创建 temp 临时文件目录)
- 【2.22】mkdir /var/temp/nginx(在/var 下创建 nginx 临时文件目录)
- 【2.3】cd /usr/local/tmp/nginx-1.8.0(进入nginx-1.8.0目录下)
- 【2.4】./configure(查询详细参数——参考:nginx 编译参数)
- 【2.5】(输入下面的代码)
-
./configure \ --prefix=/usr/local/nginx \ --pid-path=/var/run/nginx/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 \ --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 目录
- 【2.6】make
- 【2.7】make install(编译安装)
3.Nginx 启动
- 【3.1】cd /usr/local/nginx/sbin/
- 【3.2】./nginx -c /usr/local/nginx/conf/nginx.conf
说明:启动 nginx,这里用 -c 指定加载的 nginx 配置文件;如果不指定 -c,nginx 在启动时默认加载 conf/nginx.conf 文件,此文件的地址也可以在编译安装 nginx 时指定./configure 的参数(--conf-path= 指向配置文件nginx.conf)
- 【3.3】ps aux|grep nginx
说明:安装成功查询 nginx 进程,15098 是 nginx 主进程的进程 id,15099 是 nginx 工作进程的进程 id
- 浏览器访问 http://IP地址 即可访问 Nginx 欢迎页!(需开放80端口)
- 【3.4】vim /etc/sysconfig/iptables(开启 80 端口)
- 在 -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT 下粘贴一行,修改 22 端口为 80
- :wq 保存并退出
- 【3.5】service iptables restart(重启服务:restart 重启 || start 启动 || stop 停止)
4.Nginx 停止
- 方式 1,快速停止:
- 【4.11】cd /usr/local/nginx/sbin
- 【4.12】./nginx -s stop
说明:此方式相当于先查出 nginx 进程 id 再使用 kill 命令强制杀掉进程。
- 方式 2,完整停止(建议使用):
- 【4.21】cd /usr/local/nginx/sbin
- 【4.22】./nginx -s quit
说明:此方式停止步骤是待 nginx 进程处理任务完毕进行停止。
5.Nginx 重启
- 方式 1,先停止再启动(建议使用):
- 【5.10】cd /usr/local/nginx/sbin
- 【5.11】./nginx -s quit
- 【5.12】./nginx
说明:对 nginx 进行重启相当于先停止 nginx 再启动 nginx,即先执行停止命令再执行启动命令。
- 方式 2,重新加载配置文件:
- 【5.20】cd /usr/local/nginx/sbin
- 【5.21】./nginx -s reload
说明:当 nginx 的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload 不用 先停止 nginx 再启动 nginx 即可将配置信息在 nginx 中生效。
6.Nginx 开机自启动
- 【6.1】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: /var/run/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=/var/run/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
- :wq 保存并退出
- 【6.2】chmod a+x /etc/init.d/nginx(a+x ==> all user can execute 所有用户可执行)
- 【6.3】vim /etc/rc.local
- 加入一行(/etc/init.d/nginx start)保存并退出,下次重启会生效。
7.Nginx 反向代理
- 【7.1】vim /usr/local/nginx/conf/nginx.conf(修改 nginx 的访问路径)
- 【7.2】删除首行注释“#”,修改user的“nobody”为 /home/ 路径下用户名命名的文件夹(例如:ftpuser)
-
# user nobody; # 改为: user ftpuser;
- 【7.3】修改:【root html;】中 html 为 /home/ftpuser
-
location / { root html; index index.html index.htm; } #改为如下: location / { root /home/ftpuser; index index.html index.htm; }
- :wq 保存并退出
- Nginx 重启
- 浏览器访问 http://IP地址+ /文件名 即可访问 /home/ftpuser/ 下的资源文件!(能显示即显示,不能显示即下载)
- eg: 访问 http://IP地址+ /pjion233.jpg
- 菜鸟教程:https://www.runoob.com/linux/nginx-install-setup.html
- Linux 操作手册:https://blog.csdn.net/pjion233/article/details/109576878
- JDK 安装配置:https://blog.csdn.net/pjion233/article/details/109805952
- Tomcat 安装:https://blog.csdn.net/pjion233/article/details/109993437
- MySQL 安装:https://blog.csdn.net/pjion233/article/details/109807973
- Redis 单机版:https://blog.csdn.net/pjion233/article/details/109890725
- Redis 集群版:https://blog.csdn.net/pjion233/article/details/109895354