linux/centOS 下安装 ngnix
Nginx 是一款轻量级的 Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,它是由 C 语言开发,建议在 Linux 下运行。
Nginx 需要的安装环境
如果不知道 linux/centOS 上是否安装了下面的软件,那么先尝试安装 Nginx ,如果安装的过程中出现问题,那么根据错误提示,安装相应的必要环境,例如:
./configure: error: the HTTP rewrite module requires the PCRE library.
./configure: error: the HTTP gzip module requires the zlib library.
- 1
- 2
- 安装 gcc
安装Nginx的编译环境gcc
yum install gcc-c++
- 1
- 安装 pcre pcre-devel
nginx的http模块使用pcre解析正则表达式,所以安装perl兼容的正则表达式库
yum install -y pcre pcre-devel
- 1
- 安装 zlib
nginx使用zlib对http包的内容进行gzip
yum install -y zlib zlib-devel
- 1
- 安装 Open SSL
nginx不仅支持http协议,还支持https(即在ssl协议上传输http),如果使用了https,需要安装OpenSSL库
yum install -y openssl openssl-devel
- 1
Nginx 的安装
解压
tar -zxvf nginx-1.12.2.tar.gz
- 1
配置
- 使用默认配置:
cd nginx-1.12.2
./configure
- 1
- 2
注意:使用默认配置时,nginx 被安装到 /usr/local/nginx 下。
编译、安装
make && make install
- 1
配置环境变量
vim /etc/profile
- 1
在合适位置添加环境变量
export NGINX_HOME=/usr/local/nginx
export PATH=$PATH:$NGINX_HOME/sbin
- 1
- 2
重新编译 /etc/profile 文件
source /etc/profile
- 1
注意:重新编译文件时,如果会出现下面的问题
[root@pc-server1 nginx]# source /etc/profile
bash: id: command not found
bash: tty: command not found
- 1
- 2
- 3
此时说明在添加环境变量时,有单词写错了,或者是少写了 $PATH,此时需要重新修改 /etc/profile 文件,修改文件的命令改为
/bin/vi /etc/profile
- 1
然后断开 linux 连接,再重新连接即可。
Nginx 的运行
因为将 Nginx 配置到了环境变量中,因此,在任何路径下都可以直接使用 nginx 命令,而不需要进入 nginx 路径下执行。
- 启动
./nginx
- 1
- 查看 nginx 进程
ps -ef | grep nginx
- 1
- 关闭
nginx -s stop
- 1
- 重启
nginx -s reload
- 1
- 测试 nginx 配置脚本是否运行正常
通常可以通过这个命令查看 nginx 配置文件的位置
nginx -t
- 1
- nginx 开机自启
在 rc.local 文件中加入 /usr/local/nginx/sbin/nginx
vim /etc/rc.local
- 1
- nginx 指定配置文件启动
nginx -c /usr/local/nginx/conf/nginx.conf
--------------------- 本文来自 p7atYz 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/qq_30038111/article/details/79410354?utm_source=copy

浙公网安备 33010602011771号