Flask配置域名

安装工具

  • 安装

      sudo apt-get update
      sudo apt-get upgrade
    
      sudo apt-get install nginx uwsgi uwsgi-plugin-python
    

创建 sock

    cd /tmp/
    touch mysite.sock
    sudo chown www-data mysite.sock

配置 Nginx

  • /etc/nginx/sites-available/文件夹下新建mysite.conf

      server{
          listen 80;
          server_tokens off;
          server_name XXXXX.com;
    
          location / {
              include uwsgi_params;
              uwsgi_pass unix:/tmp/mysite.sock;
          }
    
      }
    
  • 创建软连接

      sudo ln -s /etc/nginx/site-available/mysite.conf /etc/nginx/site-enable/mysite.conf
    

配置 uWSGI

  • /etc/uwsgi/apps-available/中创建mysite.ini

      [uwsgi]
      vhost = true
      socket = /tmp/mysite.sock
      venv = /path/to/your/.env
      chdir /path/to/your/flask/dic
      module = run
      callable = app
    
    • module:是包含if __name__ == '__main__'.py文件
    • callable:是 Flask 实例
  • 创建软连接

      sudo ln -s /etc/uwsgi/apps-available/mysite.ini /etc/uwsgi/apps-enabled/mysite.ini
    

重启服务

    sudo service nginx restart
    sudo service uwsgi restart

其他配置

输出 log

    [uwsgi]
    uid = root
    gid = www-data

    socket = /tmp/mysite.sock
    venv = /path/to/your/.env
    chdir /path/to/your/flask/dic
    module = run
    callable = app
    logto = 127.0.0.1:1717
  • logto 可以写/path/to/haswing.log,也可以是某个端口
  • 如果需要查看某个端口的 log 输出

      uwsgi --udp 127.0.0.1:1717
    

权限问题

  • 如果启动 uwsgi 后, 出现权限的错误, 修改/tmp/mysite.sock的 owner

      sudo chown root:www-data /tmp/mysite.sock
    
  • 我这里也碰到了权限问题, 没有深入研究过原因.

Mar 26th, 2015

Comments