简单部署nginx

记录一下简单地修改nginx配置搭建博客的过程

nginx的配置文件

查看

1
2
yuki@ubuntu:~$ whereis nginx
nginx: /usr/sbin/nginx /usr/lib/nginx /etc/nginx /usr/share/nginx /usr/share/man/man8/nginx.8.gz

查看/etc/nginx

1
2
3
4
5
yuki@ubuntu:/etc/nginx$ ls
conf.d          koi-win            nginx.conf       sites-enabled
fastcgi.conf    mime.types         proxy_params     snippets
fastcgi_params  modules-available  scgi_params      uwsgi_params
koi-utf         modules-enabled    sites-available  win-utf

查看config文件

1
cat nginx.conf

整个配置文件的结构大致如下

 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
31
32
33
34
35
36
#全局块
user  root;
worker_processes  1;

#event块
events {
    worker_connections  1024;
}

#http块
http {
    #http全局块
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    #server块
    server {
        #server全局块
        listen       8000;
        server_name  localhost;
        #location块
        location / {
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
    #这边可以有多个server块
    server {
      ...
    }
}

修改配置

找到 http 花括号下的 sever 花括号部分,修改 location /{} 里面的 root 你的网站目录。
如果 config.conf 文件里你找不到 server块,那么文件里应该会有 include,比如下面这种。

1
2
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

按照上面的 include 找到对应的文件。 conf.d/*.conf 这里一般是空的,那就找下一个。
这里以 sites-enabled/* 为例子。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
yuki@ubuntu:/etc/nginx$ cd sites-available/
yuki@ubuntu:/etc/nginx/sites-available$ ls
default
yuki@ubuntu:/etc/nginx/sites-available$ cat default

...略
http {
    ...略
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   home/blog/web;
            index  index.html index.htm;
        }
        ...略
    }
    ...略
}
...略

这个文件里找到 http 花括号下的 sever 花括号部分,修改 location /{} 里面的 root 你的网站目录。

启动nginx服务

正常启动

如果之前启动过了,那么重启。下面两个命令二选一,看情况加不加 sudo

1
2
nginx -s reload
service nginx restart

或者

1
2
sudo systemctl stop nginx
sudo systemctl start nginx  

之前没启动了,启动一下。

1
sudo systemctl start nginx 

Bug / 没启动

之前的 配置文件(config.conf 里应该有这两行。

1
2
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

这里以上述路径为例

1
cat /var/log/nginx/error.log

查看报错,然后上网搜解决办法。

failed (13: Permission denied)

常见这个报错,权限被拒绝
先查看 nginx 启动用户和使用用户是否一致

1
yuki@ubuntu:~$ ps aux | grep nginx

看启动用户和使用用户的名称是否一致。 第四个是运行 ps aux | grep nginx 这条命令的用户,不用管

1
2
3
4
root     1401086  0.0  0.0  55208  1636 ?        Ss   Jul09   0:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
root     1401087  0.0  0.2  56108  5440 ?        S    Jul09   0:00 nginx: worker process
root     1401088  0.0  0.2  55840  4756 ?        S    Jul09   0:00 nginx: worker process
yuki  1696155  0.0  0.1   6476  2160 pts/0    S+   16:36   0:00 grep --color=auto nginx

不一样的一个解决办法是修改配置文件(config.conf 中的 user
如果你只是放个blog ,图省事可以改成root

1
2
3
4
5
6
7
8
user  root;
...略
events {
    ...略
}
http {
    ...略
}

改完重启 nginx 服务

常用命令

1
2
3
4
sudo systemctl start nginx  
sudo systemctl stop nginx
sudo service nginx restart
ps aux | grep nginx

参考

https://zhuanlan.zhihu.com/p/359394085
https://www.cnblogs.com/54chensongxia/p/12938929.html
https://blog.csdn.net/nilmao/article/details/123467932

Licensed under CC BY-NC-SA 4.0
Sow nothing, reap nothing.
Built with Hugo 主题 StackJimmy 设计