Nginx安装和启动

nginx 是一个轻量 web 服务器。

安装

查看服务器是什么系统

1
uname -a

我是 Linux 系统,可使用 yum 软件包管理器

第一步:先安装 PCRE pcre-devel 和 Zlib,配置 nginx 的时候会用到这两个东西

1
2
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel

第二步:安装 nginx1.18.0

  1. 下载 nginx 文件
1
wget -c https://nginx.org/download/nginx-1.18.0.tar.gz

Linux 系统中的 wget 是一个下载文件的工具

  1. 解压并进入 nginx 目录
1
2
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
  1. 使用 nginx 的默认配置
1
./configure
  1. 编译安装
1
2
make
make install

nginx 已经安装完成了

查找 nginx 安装路径:

whereis nginx
pic.1709045302233

启动 nginx 服务器。进入 nginx 安装目录下的 sbin 目录,并执行 nginx 文件,即执行./nginx
pic.1709045387708


没有任何提示就表示启动成功了。访问机器 ip,如果显示 welcome to nginx 则表示 nginx 运行成功了。
pic.1709045415961

如果还无法访问服务器 ip 地址,以我的华为云服务器为例,还需要添加安全组规则。http 协议默认 80 端口(https 默认端口为 443),我们添加一个 80 端口:

pic.1709045483441

添加后即可成功访问 nginx 欢迎页面。

查看配置文件目录。这个语句也可以验证 nginx.conf 文件是否正确。正确的格式会提示 test is successful

1
./nginx -t

pic.1709045506823

nginx 常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
查看Nginx安装位置
whereis nginx
常见位置:
/etc/nginx/conf
/usr/local/etc/nginx
/opt/homebrew/etc/nginx

启动
启动代码格式:nginx安装目录地址 -c nginx配置文件地址
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
./nginx
没有任何提示消息就表示成功了

查看Nginx进程
ps -ef|grep nginx
master 是主进程(唯一),worker是工作进程

查看端口情况
lsof -i:80
-i:80 表示只看80端口

杀死进程
kill -QUIT pid

Nginx停止或重启。进入nginx可执行目录sbin下,输入以下命令
./nginx -s signal
quit :优雅停止
stop :立即停止
reload :重载配置文件
reopen :重新打开日志文件

https://segmentfault.com/a/1190000012297511
https://bbs.huaweicloud.com/blogs/304663
https://www.cnblogs.com/ghzjm/p/10677599.html

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×