- 安装 nginx。注意,这种方式最简单,但无法添加第三方库。
$ yum install -y nginx
- 编写静态网页。
$ mkdir /data/web
$ vim index.html
<html>
<head>
<title>Example</title>
</head>
<body>
Hello World
</body>
</html>
- 在 /etc/nginx/nginx.conf 中添加 server,以访问静态网页。
server {
listen 8080; # 监听端口
server_name localhost; # 指定 ip 地址或者域名,多个配置之间用空格分隔
root /data/web; # 站点目录
index index.html; # 用户访问 web 网站时的全局首页
location / { # 表示匹配访问根目录
root /data/web; # 用于指定访问根目录时,访问虚拟主机的web目录
index index.html; # 在不指定访问具体资源时,默认展示的资源文件列表
}
- 启动 nginx
$ systemctl start nginx
或者
$ nginx -c /etc/nginx/nginx.conf
$ ps -ef | grep nginx
root 9386 1 0 15:48 ? 00:00:00 nginx: master process nginx -c /etc/nginx/nginx.conf
nginx 9387 9386 0 15:48 ? 00:00:00 nginx: worker process
root 9402 7089 0 15:48 pts/0 00:00:00 grep --color=auto nginx
- 访问静态网页。
$ curl http://localhost:8080/
<html>
<head>
<title>Example</title>
</head>
<body>
Hello World
</body>
</html>
不过,为什么是这个样子?
在浏览器中访问就正常了很多
停止 nginx
$ systemctl stop nginx
或者
$ ps -ef | grep nginx
root 9386 1 0 15:48 ? 00:00:00 nginx: master process nginx -c /etc/nginx/nginx.conf
nginx 9387 9386 0 15:48 ? 00:00:00 nginx: worker process
root 10415 7089 0 15:58 pts/0 00:00:00 grep --color=auto nginx
$ kill 9386
[root@VM_133_121_centos nginx]# kill 9387
$ ps -ef | grep nginx
root 10701 7089 0 16:01 pts/0 00:00:00 grep --color=auto nginx
参考
Nginx安装及配置详解. https://www.cnblogs.com/fengff/p/8892590.html