美文网首页
nginx安装教程(CENTOS6)

nginx安装教程(CENTOS6)

作者: 人间草木为伴 | 来源:发表于2019-12-19 20:56 被阅读0次

一:安装nginx

(1)安装gcc

将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc环境,需要安装 gcc:

yum install gcc-c++

(2)安装pcre和pcre-devel

PCRE(Perl Compatible Regular Expressions)是一个 Perl 库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库。

yum install -y pcre pcre-devel

注:pcre-devel 是使用 pcre 开发的一个二次开发库。nginx 也需要此库。

(3)安装zlib

zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在 linux 上安装 zlib 库。

yum install -y zlib zlib-devel

(4)openssl

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在 ssl 协议上传输 http),所以需要在linux 安装 openssl 库。

yum install -y openssl openssl-devel

(5)编译安装

解压:tar -zxvf nginx-1.8.0.tar.gz,进入到 nginx 的根目录

未安装好的nginx目录名不能为nginx,可以为nginx***,否则会编译失败
cd nginx-1.8.0
#nginx源文件的目录名不能为nginx,nginx这是编译好的nginx存放的目录,否则会报错

(6)配置安装参数

./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 下创建/var/temp/nginx目录,并设定权限0755

(7)编译安装

编译:

make

安装:

make install

二:操作 Nginx

(1)启动 nginx

 /usr/local/nginx/sbin/nginx 

注意:执行./nginx 启动 nginx,这里可以-c 指定加载的 nginx 配置文件,如下:

 /usr/local/nginx/sbin/nginx  -c /usr/local/nginx/conf/nginx.conf

如果不指定-c,nginx 在启动时默认加载 conf/nginx.conf 文件,此文件的地址也可以在编译安装 nginx 时指定./configure 的参数(--conf-path= 指向配置文件(nginx.conf))

(2)停止 nginx

方式 1,快速停止:此方式相当于先查出 nginx 进程 id 再使用 kill 命令强制杀掉进程。

 /usr/local/nginx/sbin/nginx -s stop

方式 2,完整停止(建议使用):此方式停止步骤是待 nginx 进程处理任务完毕进行停止。

 /usr/local/nginx/sbin/nginx -s quit

(3)重启 nginx

方式 1,先停止再启动(建议使用):对 nginx 进行重启相当于先停止 nginx 再启动 nginx,即先执行停止命令再执行启动命令。
如下:

 /usr/local/nginx/sbin/nginx -s quit
 /usr/local/nginx/sbin/nginx

方式 2,重新加载配置文件:
当 nginx 的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 nginx 再启动 nginx 即可将配置信息在 nginx 中生效,如下:

 /usr/local/nginx/sbin/nginx -s reload

(4)测试

nginx 安装成功,启动 nginx,即可访问虚拟机上的 nginx,Nginx 默认的是侦听 80 端口,访问

虚拟机地址:80

相关文章

网友评论

      本文标题:nginx安装教程(CENTOS6)

      本文链接:https://www.haomeiwen.com/subject/xsuvnctx.html