编译运行nginx

引言:

使用linux编译运行nginx!!

编译运行nginx

安装环境

CentOS7安装编译nginx1.19.0

步骤:

1. Nginx官网

Nginx官网下载安装包 nginx-1.19.0

官网有详细的安装部署说明


2. CentOS7中解压

1
tar -zxvf nginx-1.19.0.tar.gz

进入解压后的文件,会看到

有多个文件,我们主要会用到的有conf configure


3. configure

切换到nginx目录内,执行

1
./configure --prefix=/usr/local/demo

其他参数官网十分详细,这里列举几个重要的配置

1
2
3
4
5
--prefix=path
# 定义保存服务器文件的目录,默认为 /usr/local/nginx

--with-http_ssl_module
# 将支持https协议

4. make

依然在原目录下执行

1
make

编译安装

5. make install

执行安装,安装后--prefix指定的安装目录下会出现

image-20200702135045948

如此的目录。

conf下就是配置文件,我们待会儿需要对其目录下的nginx.conf进行配置

sbin下是执行命令的目录

6. 找一个博客作为检验

我把博客放在了/usr/nginx/blog

img

博客目录下有html文件以及静态图片文件夹还有css文件


7. 配置conf

--prefix后的路径下,进入conf目录,配置nginx.conf文件,将

1
2
3
4
5
6
7
8
9
10
11
12
13
http{
server{
listen 80;
# 监听的端口地址
server_name localhost;

location /{
root html;
index index.html index.htm
}
# 把这里的root 配置到blog的目录
}
}

8. 启动配置

这里先学习一下nginx的命令

1
nginx -s signal

Where signal may be one of the following:

1
2
3
4
5
6
7
stop — fast shutdown

quit — graceful shutdown

reload — reloading the configuration file

reopen — reopening the log files

优雅的杀死nginx

1
kill -s QUIT [nginx的pid]

学会了这些,进入sbin目录执行

1
./nginx

就启动了,打开浏览器,输入localhost,发现已经能愉快的访问了

image


遇到的小bug

可能会出现这种情况

img

有可能是你执行了两遍./nginx,80端口被占用,就会报错

解决方法:

1
2
3
4
netstat -ntlp
# 查看端口为80的哪一个进程
kill -s QUIT [nginx的pid]
# 使用官方手法,优雅的杀死nginx

ENDING