Swift写服务端四:优化项目配置文件

1.存在的问题

前面三个步骤,我们已经打通了vue、nginx、supervisor、vapor之间的连接,接下来我们要优化如下内容:

  • 1.nginx和supervisor的配置文件中,user都是用的root,这是需要修改的,root用户的权限过大。
  • 2.vue相关内容配置在了nginx.conf这个文件中,nginx.conf这个文件是nginx基本的配置文件,不应该在这个文件中配置,应该把配置挪到相应站点的配置文件中,也就是三中我们创建的位于sites-enabled文件夹下的samplecode.conf文件中。
  • 3.接口配置在了8888端口上,vue打包好的资源在80端口,这是不方便后续写代码的,应该把接口也配置到80端口,通过请求的uri进行转发。

2.解决用户问题

2.1.使用root用户登录服务器

2.2.添加新的用户

root@sweet-story-2:~# adduser yuhua
Adding user `yuhua' ...
Adding new group `yuhua' (1000) ...
Adding new user `yuhua' (1000) with group `yuhua' ...
Creating home directory `/home/yuhua' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for yuhua
Enter the new value, or press ENTER for the default
    Full Name []: yh
    Room Number []: 
    Work Phone []: 
    Home Phone []: 
    Other []: 
Is the information correct? [Y/n] y

除了输入密码,其余的内容可以全部回车,为空即可。

2.3.允许新用户使用sudo命令

usermod -aG sudo yuhua

2.4.退出,使用新用户登录

exit
ssh yuhua@11.11.11.11

2.5.停掉nginx和supervisor

sudo systemctl stop nginx
sudo supervisorctl stop sample-code
// 或者如下命令停掉所有
sudo supervisorctl stop all

2.6.上传vue代码以及拉取vapor代码到yuhua用户目录下

先在目录下新建一个dist文件夹,然后使用scp命令上传vue打包好的内容。

mkdir dist
scp -r ./dist/* yuhua@11.11.11.11:/home/yuhua/dist/"

然后使用git命令拉取vapor的代码。

git clone https://github.com/flywo/SampleCode.git

此时,文件夹下的内容是这个样子的

yuhua@sweet-story-2:~$ ls
dist  SampleCode

2.7.修改nginx.conf和sample-code.conf配置文件

修改nginx.conf中user为yuhua,然后注释掉server的内容

user yuhua;
http {

    ##
    # Basic Settings
    ##

    #server {
    #   listen 80;
    #   server_name code.yuhua.pub;
    #   location / {
    #       root /root/dist;
    #       index index.html index.htm;
    #       try_files $uri $uri/ /index.html;
    #   }
    #}
}

修改supervisor的配置文件/etc/supervisor/conf.d/sample-code.conf

[program:sample-code]
command=/home/yuhua/SampleCode/.build/release/Run serve --env production
directory=/home/yuhua/SampleCode/
user=yuhua
stdout_logfile=/var/log/supervisor/%(program_name)-stdout.log
stderr_logfile=/var/log/supervisor/%(program_name)-stderr.log

3.配置nginx的samplecode.conf文件

/etc/nginx/sites-enabled下的samplecode.conf文件修改配置。

server {
//监听的域名
    server_name code.yuhua.pub;
//监听的端口
    listen 80;
//vue相关配置
    location / {
        root /home/yuhua/dist;     
        index index.html index.htm;
        try_files $uri $uri/ /index.html;
    }
//接口转发配置,后续接口都是以api开头
    location ^~ /api/ {
        proxy_pass http://127.0.0.1:8080;
        proxy_pass_header Server;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass_header Server;
        proxy_connect_timeout 3s;
        proxy_read_timeout 10s;
    }
}

4.构建vapor项目,然后运行

1.构建vapor项目

swift build -c release

这一步有可能会有这个错误

-bash: swift: command not found

原因很简单,当前用户没有配置swift的可执行文件路径。使用如下命令添加即可。

echo "export PATH=/usr/share/swift/usr/bin:$PATH" >> ~/.bashrc
source ~/.bashrc

2.启用supervisor

sudo supervisorctl reread
sudo supervisorctl add sample-code
sudo supervisorctl start sample-code

使用curl测试:

curl http://localhost:8080
访问接口成功

3.启用nginx

sudo systemctl start nginx

访问:http://code.yuhua.pub即可看到页面

image.png

测试接口:http://code.yuhua.pub/api/
image.png

服务器使用curl直接请求vapor测试:

curl http://localhost:8080/api/
{"error":true,"reason":"Not Found"}

接口和postman相同,说明这个json是vapor返回的。原因也很简单,因为我们没有处理/api/的请求。但是重点不在这里,重点是说明nginx将请求转发给了vapor。

5.结果

大功告成,现在就可以投入到服务器代码的编写和前端代码编写了。

  • 有代码洁癖的朋友,可以去/root/目录下,把之前的代码给删除掉,我就受不了,都删了的。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容