likes
comments
collection
share

lnmp - thinkphp6.0服务迁移实践笔记

作者站长头像
站长
· 阅读数 21

概述

最近入职一家初创公司从外包手机接下来一个项目,自己买了一个服务器迁移了一个项目,由外包的宝塔迁移到自己的服务器,主要说一下遇到的坑,进行一次总结,服务器是阿里云的。

配置Nginx

由于宝塔上的环境都是自己生成的配置,所以自己配置Nginx的时候要几个重要的技能点ssl证书、伪静态、防跨站攻击(open_basedir),迁移的时候配置了这几个重要的技术点

1. ssl证书

阿里云产品与服务->数据安全->数字证书管理服务,如果没有免费证书的话,可以先申请一个免费的证书,申请完免费证书如下图,点击下载以后有Nginx、Tomcat、Apache、IIS等等,我使用的是Nginx部署的ssl服务。

lnmp - thinkphp6.0服务迁移实践笔记

2.伪静态

伪静态Nginx 做了一个rewrite重写

location ~* (runtime|application)/{
    return 403;
}
location / {
    if (!-e $request_filename){
            rewrite  ^(.*)$  /index.php?s=$1  last;   break;
    }
}

3.添加防跨站攻击(open_basedir)

迁移遇到最严重的问题是防跨站攻击(open_basedir),错误消息提示Warning: require(): open_basedir restriction in effect.

造成这个问题的原因是因为fastcgi.conf里的最底下open_basedir的地方设置注释掉,推荐做法是在再根目录,thinkphp的话是public目录,添加.user.ini文件:

open_basedir=/home/wwwroot/tfb-web-api/:/tmp/

php.ini文件修改cgi.fix_pathinfo = 1 , 这样,所有问题都已经解决了。

server
{
    listen 80;
    listen 443 ssl http2;
    server_name www.localhost.cn;
    index index.php index.html index.htm default.php default.htm default.html;
    root /home/wwwroot/tfb-web-api/public;

    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #HTTP_TO_HTTPS_START
    if ($server_port !~ 443){
        rewrite ^(/.*)$ https://$host$1 permanent;
    }
    #HTTP_TO_HTTPS_END
    ssl_certificate      .pem 路径;
    ssl_certificate_key  .key 路径;
    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;
    #SSL-END
    
    location ~ [^/]\.php(/|$)
    {
        try_files $uri =404;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
    
    access_log  /www/wwwlogs/www.tufubao.cn.log;
    error_log  /www/wwwlogs/www.tufubao.cn.error.log;
}

Git版本库

刚开始的时候,我们使用补丁包的方式进行协作,后来使用了gitee.com,企业版5人之内的协作是免费的这个正好适合我们这种初创团队。

lnmp - thinkphp6.0服务迁移实践笔记

权限

因为安装的时候使用www的用户和用户组安装的,如果有权限的问题,执行命令chown -R www:www ./tfb-web-api