CentOS7 安装Nginx

CentOS7 安装Nginx

1. 安装编译安装所需要的依赖

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

2. 下载Nginx

## 没有安装wget的使用  yum install -y wget
wget https://nginx.org/download/nginx-1.16.1.tar.gz

3. 解压编译安装

tar zxf nginx-1.16.1.tar.gz
cd nginx-1.16.1 
## 编译的时候 需要配置 ssl支持
./configure --with-http_ssl_module
make && make install
## 查看安装目录  whereis nginx

## 启动:
/usr/local/nginx/sbin/nginx
## 检查配置文件是否有错
/usr/local/nginx/sbin/nginx  -t
## 选择配置文件
/usr/local/nginx/sbin/nginx  -c /***
## 停止:
/usr/local/nginx/sbin/nginx  -s  stop
## 重启:
/usr/local/nginx/sbin/nginx  -s  reload

到这里,nginx已经安装完毕可以正常启动,但是为了之后更好的使用,建议添加进PATH中。

4. 添加进PATH(非必要步骤)

vim ~/.bashrc
## 文件中添加
export PATH=$PATH:/usr/local/nginx/sbin/
## 保存后退出
source ~/.bashrc

> 配置nginx开机启动

切换到/lib/systemd/system 目录,创建nginx.service文件。

touch nginx.service

编辑nginx.service文件

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx reload
ExecStop=/usr/local/nginx/sbin/nginx quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

退出并保存文件

## 使nginx开机自启动
systemctl enable nginx.service

##启动
systemctl start nginx.service
##停止
systemctl stop nginx.service
##重启
systemctl restart nginx.service

> 可能存在的问题

nginx: [emerg] https protocol requires SSL support in /usr/local/nginx/conf/

编译的时候没有启用SSL支持,只使用了./configure,解决的方法要重新进行编译,首先查看旧的编译参数:

/usr/local/nginx/sbin/nginx -V
输出信息后的configure arguments:就是编译参数,在之前的参数后面加上 --with-http_ssl_module重新编译

## --XX 为之前的编译参数,没有的话就不用设置
./configure --XX --with-http_ssl_module
make

不直接进行make install是因为原来的nginx还有一堆的配置文件,不能被覆盖;我们应该只覆盖编译出来的nginx。之后重启nginx就没有问题啦

cp /usr/local/nginx/sbin/nginx ~/
## objs/nginx 是新编译出来的nginx可执行程序
cp objs/nginx /usr/local/nginx/sbin/
原文地址:https://www.cnblogs.com/huizhipeng/p/12133634.html