安装 Nginx
1 将 Nginx 安装包上传到 Linux
- 使用的 Nginx 版本为 nginx-1.8.0.tar.gz
2 nginx 安装环境
nginx 是 C 语言开发,建议在 linux 上运行
gcc
安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc环境,需要安装 gcc:
yum install gcc-c++
PCRE
PCRE(Perl Compatible Regular Expressions)是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。
yum install -y pcre pcre-devel
注:pcre-devel 是使用 pcre 开发的一个二次开发库。nginx 也需要此库。
zlib
zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在 linux 上安装 zlib 库。
yum install -y zlib zlib-devel
openssl
OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在 ssl 协议上传输 http),所以需要在linux 安装 openssl 库。
yum install -y openssl openssl-devel
3 编译安装
解压:
tar -zxvf nginx-1.8.0.tar.gz
进入到 nginx 的根目录
在根目录下,配置安装参数 参数设置如下:
./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目录
在根目录下
编译:make
编译安装 make install
成功!
操作 Nginx
1 启动 nginx
./nginx
注意:执行./nginx 启动 nginx ,这里可以-c 指定加载的 nginx 配置文件,如下:
./nginx -c /usr/local/nginx/conf/nginx.conf
如果不指定-c ,nginx 在启动时默认加载 conf/nginx.conf 文件,此文件的地址也可以在编译安装 nginx 时指定./configure 的参数(–conf-path= 指向配置文件(nginx.conf) ))
2 停止 nginx
方式 1
快速停止:
./nginx -s stop
此方式相当于先查出 nginx 进程 id 再使用 kill 命令强制杀掉进程。
方式 2 完整停止(建议使用):
./nginx -s quit
此方式停止步骤是待 nginx 进程处理任务完毕进行停止。
3 重启 nginx
方式 1
先停止再启动(建议使用):
对 nginx 进行重启相当于先停止 nginx 再启动 nginx,即先执行停止命令再执行启动命令。
./nginx -s quit
./nginx
方式 2
重新加载配置文件:
当 nginx 的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 nginx 再启动 nginx 即可将配置信息在 nginx 中生效,如下:
./nginx -s reload