ramlife

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

创建用户,并有 sudo 权限

useradd xyz
passwd xyz
usermod -aG wheel xyz
sudo -l

检查端口

sudo netstat -tulpn | grep :80
sudo netstat -tulpn | grep :443

安装 nginx

sudo yum install -y nginx
sudo systemctl enable nginx
sudo systemctl start nginx
sudo systemctl status nginx

防火墙

sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

或者

sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
systemctl restart firewalld.service

检查默认网页

在浏览器中输入服务器地址,正常会出现 welcome to nginx。 但是 centos 用户可能会出现 welcome to centos。 这个也是正常的。

上传 hugo public

把 public 文件夹 上传到 xyz 用户目录中

配置 conf

修改 /etc/nginx/nginx.conf, 下面是修改和原版的差别:

[root@kvm-Buffalo nginx]# diff nginx.conf.orig  nginx.conf
5c5,6
< user nginx;
---
> #user nginx;
> user root;
39,40c40,41
<         listen       80;
<         listen       [::]:80;
---
>         listen       80 default_server;
>         listen       [::]:80 default_server;
42c43,44
<         root         /usr/share/nginx/html;
---
>         #root         /usr/share/nginx/html;
>         root         /home/xyz/www/public;
46a49,53
>       location / {
>               root /home/xyz/www/public;
>               index index.html index.htm index.php;
>       }
>
48a56
>               root /home/xyz/www/public;

具体配置如下, 有域名的别忘了把域名加上去:

[root@kvm-Buffalo nginx]# cat nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

#user nginx;
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        #root         /usr/share/nginx/html;
        root         /home/xyz/www/public;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                root /home/xyz/www/public;
                index index.html index.htm index.php;
        }

        error_page 404 /404.html;
        location = /404.html {
                root /home/xyz/www/public;
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

重启 nginx

sudo nginx -s reload
sudo systemctl status nginx
sudo systemctl restart nginx

再次检查网页

浏览器中重新输入 ip 地址。 如果碰到 403 forbiden. 可以检查 /var/log/nginx 目录下面的两个 log 文件。 正常就是权限不够。

  • 配置中,最开始的 user 改为 root
  • 文件权限不够,把 public 向上的所有父目录,至少有 755 的权限。
  • selinux,就算文件权限够了,selinux 如果开启也会 403。genenforce 检查 selinux,如果开启,vim /etc/selinux/config 改为 SELINUX=disabled, 然后 reboot

同步 public

有多种方式: rsync, ftp, rclone

参考:

hugo博客部署到腾讯云轻量级服务器
https://www.sulvblog.cn/posts/blog/hugo_deploy

Deployment with Rclone
https://gohugo.io/hosting-and-deployment/deployment-with-rclone/

使用Hugo搭建网站并部署到服务器
https://blog.csdn.net/weixin_35436966/article/details/103950907

nginx 访问静态资源文件出现403-forbidden
https://blog.csdn.net/taokeng/article/details/103705342

Centos中的nginx启动成功,但是浏览器却无法访问默认欢迎页面
https://blog.csdn.net/weixin_36332085/article/details/122510422

【解决方案】Nginx安装后,通过IP访问出现CentOS的欢迎界面
https://blog.csdn.net/qq_60923912/article/details/124090550

posted on 2023-01-18 15:18  ramlife  阅读(320)  评论(0编辑  收藏  举报