Deploy our Flask App (2) on the cloud Ali

Book connected to the back. . .

On a shared some basic configuration on Ali cloud, such as firewalls, which although seemingly has nothing to do with the deployment, but all throughout the course of practice in order to ensure the safety of inexpensive and indispensable matters.

Deploy our application

It must first be clear that our code is pushed to have a lot of ways on the linux server, I have used git management and scp command. git method is very common here is not to say, scp is commonly based linux system command ssh remote copy using scp, then you can bypass the git management, is a more simple way, of course, use git habit is very good, here we use "scp" command to operate.

scp -r USER/FILE/LOCATION [email protected]:~/
17708740-2304e1c59db12b09.gif
image.gif

Mv command with commonly used under Linux is almost here much introduction.

At the same time if you use this method, you need to manually add a "requirements.txt" folder to the format write "=" is required for each expansion depends, of course, for convenience, it is best to create a virtual environment, the above dependence installed into a virtual environment.

After the above steps do if your sensitive information is not exposed, then you can use the "flask run" to view your application, and note that in order so that we can see the status of applications running from a native environment, we use :

flask run --host=0.0.0.0
17708740-a738d7c144283f20.gif
image.gif

We note that the port is open above, it is possible even if you open port 5000, enter the local browser:

***.***.***.***:5000      //***代表你的共有IP
17708740-9a4cda6d7772c059.gif
image.gif

It may still be open, because Ali cloud security groups and not open port 5000, you need to manually set it

17708740-de004ae1883f6923
image
17708740-43fbc168820e1c5f.gif
image.gif

Nginx Gunicorn and Supervisor

Of course, the actual application deployment certainly can not be so sure, we need a more robust server to run our example. Simply put, Nginx is a Web server, it is convenient, and has good performance, but he can only handle CSS, JS and other documents, can not be identified Python code, therefore, Gunicorn comes into play. gunicorn is a python Wsgi http server, supports only run on Unix systems, the unicorn from Ruby project. Gunicorn using prefork master-worker model, capable of cooperating with various wsgi web frame.

Of course, not only these, if you do not think the site has been open window can also be run, then the Supervisor software like on the essential, Supervisor Python is a general-purpose development process management procedures, can an ordinary command line process becomes the background daemon, but also can monitor the daemon. Through these three dependent combined with each other, we can create a small blog has a good performance.

First, delete the default configuration and writes the new configuration:

sudo rm /etc/nginx/sites-enabled/default
sudo vim /etc/nginx/sites-enabled/flaskblog

17708740-23c85102366ebb34.gif
image.gif
//Nginx:

server{
        listen 80;
        server_name **.**.**.**;

        location /static {
                alias /home/**/**/app/static;

        }

        location / {
                proxy_pass http://localhost:8000;
                include /etc/nginx/proxy_params;
                proxy_redirect off;
        }
}

17708740-ad69a0f443d81e28.gif
image.gif

At the same time you can open port 80, the specific operation will not say.

Use the following operations to restart Nginx, of course, this time you can see the CSS file, but do not look like a web page, the reason is what we said before Nginx does not recognize Python Code.

sudo systemctl restart nginx
17708740-5a82e8e77ece5bd0.gif
image.gif

Then we use:

gunicorn -w 3 test:app
17708740-88d82f47b9af581a.gif
image.gif

About gunicorn we deploy in Heroku has been mentioned in an article in the Flask, not described in detail here.

Finally, we configure the look Supervisor:

[program:flaskblog]
directory=/home/**/tutorial
command=/home/**/tutorial/venv/bin/gunicorn -w 3 test:app
user=**
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stderr_logfile=/var/log/flaskblog/flaskblog.err.log
stdout_logfile=/var/log/flaskblog/flaskblog.out.log

17708740-8cc119973ea309a4.gif
image.gif

And we need to create a file folder, restart Supervisor can get it finished website.

The next section, which is the last one, we'll see how domain names and IP binding.

Reproduced in: https: //www.jianshu.com/p/bedda5a16ec3

Guess you like

Origin blog.csdn.net/weixin_34192732/article/details/91187149
Recommended