美文网首页
Python部署

Python部署

作者: 诺之林 | 来源:发表于2020-07-03 11:01 被阅读0次

    本文环境基于vagrant-gunicorn

    目录

    Python

    sudo apt install -y python3-pip python3-dev python3-venv
    
    export LC_ALL="en_US.UTF-8"
    
    python3 -m venv ~/.env-py3
    
    source ~/.env-py3/bin/activate
    
    export LC_ALL="en_US.UTF-8"
    
    mkdir site && cd site
    

    Flask

    pip install flask
    
    vim app.py
    
    from flask import Flask
    
    app = Flask(__name__)
    
    @app.route('/')
    def hello_world():
        return "Hello World!"
    
    if __name__ == '__main__':
        app.run()
    
    python app.py
    
    curl localhost:5000
    # Hello World!
    

    Gunicorn

    pip install gunicorn
    
    gunicorn app:app
    
    curl localhost:8000
    # Hello World!
    

    Systemd

    sudo mkdir /usr/lib/systemd/system
    
    sudo vim /usr/lib/systemd/system/app.service
    
    [Unit]
    Description=Gunicorn App
    After=network.target
    
    [Service]
    User=vagrant
    Group=www-data
    WorkingDirectory=/home/vagrant/site
    Environment="PATH=/home/vagrant/.env-py3/bin/"
    ExecStart=/home/vagrant/.env-py3/bin/gunicorn -w 3 -b unix:app.sock -m 007 app:app
    
    [Install]
    WantedBy=multi-user.target
    
    sudo systemctl enable app.service
    # Created symlink from /etc/systemd/system/multi-user.target.wants/app.service to /usr/lib/systemd/system/app.service.
    
    sudo systemctl start app
    
    sudo systemctl status app
    

    Nginx

    sudo apt install -y nginx
    
    sudo vim /etc/nginx/sites-available/app.conf
    
    server {
        listen 80;
        server_name app.com;
    
        location / {
            include proxy_params;
            proxy_pass http://unix:/home/vagrant/site/app.sock;
        }
    }
    
    sudo ln -s /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled
    
    sudo nginx -t
    
    sudo nginx -s reload
    
    sudo sh -c "echo '127.0.0.1 app.com' >> /etc/hosts"
    
    curl app.com
    # Hello World!
    
    sudo reboot
    
    curl app.com
    # Hello World!
    

    参考

    相关文章

      网友评论

          本文标题:Python部署

          本文链接:https://www.haomeiwen.com/subject/kvmbtktx.html