从0到1部署一个云服务站点需要哪些步骤

宫崎骏风-马尔科-红发

前言

这是手动敲命令配置的方式,需要一定的 linux 命令基础,也可以使用云服务器平台支持的面板操作方式,对于新手更友好,例如 宝塔面板 等等

买域名

去域名注册网站注册自己想要的域名,如火山引擎,腾讯云,阿里云 等等

基本价格都差不多,我五一之前买的,火山引擎的优惠力度更大一点,选了一个 xyz 结尾的域名,主要还是 com 结尾的域名太贵了

买云服务器

买了最小配置的,一年60

总费用

域名,云服务器加其他一共261,产品服务明细如下

开始配置

镜像选择

企业级服务系统大部分都是 CentOS,因此这里我选的是 CentOS Stream 9,后面的操作都是在 CentOS Stream 9 中进行的

实例绑定公网IP

按需创建实例后,查看实例是否绑定了公网IP,如果没绑定,点最右边的 ... 绑定即可

远程连接

可以在管理后台点击 远程连接 进入 CentOS 系统,也可以使用 ssh 的方式,因为远程连接默认是 root 用户,下面所有的命令操作不用加 sudo

安装 nginx

先安装 yum-utils

yum install yum-utils

设置 yum 仓库,在对应目录下创建配置文件 /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

执行安装命令

yum install nginx

安装结束后,使用 nginx -v 命令查看是否安装成功

# nginx -v
nginx version: nginx/1.26.0

准备站点文件

我使用了一个 index.html 文件,里面只有一句话 I am a test 8091,把 index.html 文件放到 /root/code/8091 目录下

配置 Nginx 站点

例如配置一个 8091 端口的站点,在 /etc/nginx/nginx.conf 文件中,添加 server 配置,主要是设置 root 路径,就是上面 index.html 文件所在的目录地址,配置 listen 端口为 8091, 剩下的配置基本都是默认的,直接粘就行,nginx.conf 完整配置如下

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    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;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       8091;
        server_name  localhost;
        location / {
            root   /root/code/8091;
            index  index.html;
            try_files $uri $uri/ /index.html;
        }
        location = /index.html {
            root   /root/code/8091;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # gzip
        gzip on;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_proxied any;
        gzip_vary on;
    }

}

重启 Nginx

每次 nginx.conf 文件变更后,都需要重启服务

systemctl restart nginx

nginx -s reload

配置防火墙

开启 CentOS 系统的防火墙

systemctl start firewalld

开放8091端口

firewall-cmd --permanent --add-port=8091/tcp

重启防火墙服务

firewall-cmd --reload

配置云服务端口

网络与安全 -> 安全组 -> 访问规则 -> 入方向规则 -> 添加规则 添加 8091 端口,如火山引擎操作后的页面效果

测试效果

到这里就全部配好了,打开浏览器,地址栏输入 公网IP:端口 查看效果

遇到的问题

500 Internal Server Error

配置好站点IP和对应目录文件后,访问后500错误

查看 Nginx 日志,进行分析

tail -f /var/log/nginx/error.log

默认一般是这个路径,这个路径是在 nginx.conf 文件中 error_log 参数配置的

tail -f /usr/local/nginx/logs/error.log

日志提示内容

2024/05/07 11:06:34 [error] 279863#279863: *73 rewrite or internal redirection cycle while internally redirecting to "/index.html", client: 61.148.18.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "101.126.84.155:8091", referrer: "http://101.126.84.155:8091/"

意思是 内部重定向到“/index.html”时重写或内部重定向循环,这个情况是配置有问题导致

nginx.conf 中我的配置是这样的

    server {
        listen       8091;
        server_name  localhost;
        location / {
            root   /root/code/8091;
            index  index.html;
            try_files $uri $uri/ /index.html;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # gzip
        gzip on;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_proxied any;
        gzip_vary on;
    }

因为 8091 端口配置中,location / 块使用了 try_files $uri $uri/ /index.html; 会导致导致在尝试访问根目录时发生重定向循环,添加一个新的location=/index块,直接提供 /index.html 而不进行重定向,如下即可

    server {
        listen       8091;
        server_name  localhost;
        location / {
            root   /root/code/8091;
            index  index.html;
            try_files $uri $uri/ /index.html;
        }
        location = /index.html {
            root   /root/code/8091;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }

        # gzip
        gzip on;
        gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
        gzip_proxied any;
        gzip_vary on;
    }

403 Forbidden

再次使用 tail -f /var/log/nginx/error.log 命令查看 nginx 日志

[error] 290911#290911: *104 open() "/root/code/8091/index.html" failed (13: Permission denied), client: 61.148.18.2, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "101.126.84.155:8091", referrer: "http://101.126.84.155:8091/"

Permission denied 权限拒绝

查看当前 code/ 目录权限如下

  File: /
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: fd02h/64770d    Inode: 2           Links: 20
Access: (0555/dr-xr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2024-05-07 14:54:16.911564120 +0800
Modify: 2024-05-07 14:54:15.197545360 +0800
Change: 2024-05-07 14:54:15.197545360 +0800
 Birth: 2022-08-29 16:45:42.000000000 +0800

555 改成 777

chmod 777 code/
chmod 777 code/*

重新刷新浏览器,页面正常能访问到了

欢迎大家讨论交流,如果喜欢本文章或感觉文章有用,动动你那发财的小手点赞、收藏、关注再走呗 ^_^

微信公众号:草帽Lufei

  • 27
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

草帽lufei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值