Detailed tutorials for web apps under the framework of deploying Python

Source: Internet
Author: User
Tags gz file nginx server
As a qualified developer, the development in the local environment is not enough, we need to deploy the Web app to the remote server, so that the majority of users can access the site.

Many of the development of the students to deploy this thing as the work of the students, this view is completely wrong. First, the recent trend in DevOps is that development and operations become a whole. Secondly, the difficulty of operation and maintenance, in fact, with the development of quality has a great relationship. Code to write rubbish, operation and maintenance of good also jiabuzhu every day hang off. Finally, DevOps concepts need to incorporate functions such as operations and monitoring into development. Do you want the server to be upgraded without interrupting user Service? That's something to consider when developing.

Next, we'll deploy the Awesome-python-webapp to a Linux server.
Build a Linux Server

To deploy to Linux, you first have to have a Linux server. For those of you who want to experience on the public Web, you can request a EC2 virtual machine (free for 1 years) on Amazon AWS, or use some of the cloud servers in the country to provide an image of Ubuntu server in general. If you want to deploy on-premises students, please install the virtual machine, recommended to use VirtualBox.

The Linux server version we chose is Ubuntu Server 12.04 LTS, because apt is too simple. If you're ready to use another Linux version, there's no problem.

After the Linux installation is complete, make sure that the SSH service is running, otherwise you will need to install via apt:

$ sudo apt-get install Openssh-server

With the SSH service, you can connect locally to the server. It is recommended that the public key be copied to the server-side user's. Ssh/authorized_keys, so that the certificate can be implemented without a password connection.
Deployment method

When we develop locally, we can use Python's own WSGI server, but on the server, it's obviously not possible to use the server that comes with this development version. Can choose the WSGI server many, we choose Gunicorn: It uses the similar nginx master-worker mode, simultaneously can provide gevent support, can obtain the extremely high performance without the code modification.

In addition, we also need a high-performance Web server, here to choose Nginx, it can handle static resources, and as a reverse proxy to the dynamic request to Gunicorn processing. Gunicorn is responsible for invoking our Python code, which is as follows:

Nginx is responsible for distributing the request:

On the server side, we need to define the directory structure of the deployment:

The code is as follows:

/
+-srv/
+-awesome/<--Web app root directory
+-www/<--store python source code
| +-static/<--storing static resource files
+-log/<--store Log

To deploy on the server, consider what to do if the new version is not functioning properly and needs to be rolled back to the old version. It is not possible to overwrite old files with new code each time, requiring a mechanism similar to version control. Because the Linux system provides the soft link function, we use www as a soft link, which directory it points to, which is the currently running version:

The Nginx and Gunicorn configuration files only need to point to the WWW directory.

Nginx can be started as a service process directly, but gunicorn not yet, so, supervisor debut! Supervisor is a management process tool that can start the service with the system boot, it also always monitors the service process, if the service process quits unexpectedly, supervisor can automatically restart the service.

To summarize the services we need to use are:

    • Nginx: High-Performance Web server + responsible for reverse proxy;
    • Gunicorn: High performance WSGI server;
    • Gevent: The python synchronization code into the asynchronous process of the library;
    • Supervisor: Tools for monitoring service processes;
    • MySQL: Database service.

The above services can be installed directly on a Linux server using apt:

$ sudo apt-get install nginx gunicorn python-gevent Supervisor Mysql-server

Then, we installed the Python library we used for our own web app:

$ sudo apt-get install python-jinja2 python-mysql.connector

Create the directory/srv/awesome/and the appropriate subdirectories on the server.

Initialize the MySQL database on the server and copy the database initialization script Schema.sql to the server for execution:

$ mysql-u Root-p < Schema.sql

The server side is ready.
Deployment

Copy files with FTP or SCP or rsync? If you need to manually copy, use twice a day, if you deploy 50 times, it is slow, inefficient, and error prone.

The right way to deploy is to use tools to complete automated deployment with scripts. Fabric is an automated deployment tool. Since fabric is developed in Python, the deployment script is also written in Python, which is very handy!

To deploy with fabric, you need to install fabric on this machine (the development machine, not the Linux server):

$ Easy_install Fabric

You do not need to install Fabric,fabric on a Linux server to log on to the server directly with SSH and execute deployment commands.

The next step is to write the deployment script. The deployment script for fabric called fabfile.py, we put it in the Awesome-python-webapp directory, with the WWW directory peer:

The code is as follows:

awesome-python-webapp/
+-fabfile.py
+-www/
+- ...

The scripting of fabric is simple, first importing the Fabric API, setting the variables at the time of deployment:

# fabfile.pyimport OS, refrom datetime import datetime# Import fabric api:from fabric.api import *# Server login username: env.user = ' Michae L ' # sudo user for root:env.sudo_user = ' root ' # server address, can have multiple, deploy in sequence: Env.hosts = [' 192.168.0.3 ']# server MySQL username and password: db_user = ' Www-data ' Db_password = ' www-data '

Then, each Python function is a task. Let's start by writing a packaged task:

_tar_file = ' dist-awesome.tar.gz ' def build ():  includes = [' Static ', ' templates ', ' transwarp ', ' favicon.ico ', ' *.py ' ]  excludes = [' Test ', '. * ', ' *.pyc ', ' *.pyo ']  local (' rm-f dist/%s '% _tar_file) with  LCD (os.path.join ( Os.path.abspath ('. '), ' www '):    cmd = [' tar ', '--dereference ', '-czvf ', '. /dist/%s '% _tar_file]    cmd.extend (['--exclude=\ '%s\ '% ex for Ex ' excludes])    cmd.extend (includes)    Local (". Join (CMD)")

Fabric provides a local (' ... ') command to run locally, with an LCD (path) that sets the directory of the current command to the directory specified by the LCD (), noting that fabric can only run command-line commands, which may require a cgywin environment under Windows.

Run under the Awesome-python-webapp directory:

$ fab Build

See if you created the dist-awesome.tar.gz file in the Dist directory.

After packaging, we can continue to write the deploy task, upload the package file to the server, unzip, reset the WWW soft link, restart the related services:

_remote_tmp_tar = '/tmp/%s '% _tar_file_remote_base_dir = '/srv/awesome ' def deploy ():  newdir = ' www-%s '% datetime.no W (). Strftime ('%y-%m-%d_%h.%m.%s ')  # Delete the existing tar file:  run (' rm-f%s '% _remote_tmp_tar)  # Upload a new tar file:  put (' dist/%s '% _tar_file, _remote_tmp_tar)  # Create a new directory: with  CD (_remote_base_dir):    sudo (' mkdir%s '% newdir)  # Unzip to new directory: with  CD ('%s/%s '% (_remote_base_dir, newdir)):    sudo (' tar-xzvf%s '% _remote_tmp_tar)  # Reset Soft Link: C12/>with CD (_remote_base_dir): sudo ('    rm-f www ') sudo ('    ln-s%s www '% newdir)    sudo (' chown www-data: Www-data www ')    sudo (' chown-r www-data:www-data%s '% newdir)  # Restart Python service and Nginx server: With  settings (warn _only=true): sudo ('    supervisorctl stop awesome ') sudo ('    supervisorctl start awesome ')    sudo ('/etc/ Init.d/nginx Reload ')

Note that the command executed by the run () function is run on the server, similar to with CD (path) and with LCD (path), which sets the current directory on the server side to the directory specified by CD (). If a command requires sudo permission, it cannot be run (), but it is executed with sudo ().
Configure Supervisor

The command above let supervisor restart Gunicorn will fail because we have not configured supervisor yet.

Write a supervisor configuration file awesome.conf, which is stored in the/etc/supervisor/conf.d/directory:

The code is as follows:

[Program:awesome]
Command =/usr/bin/gunicorn--bind 127.0.0.1:9000--workers 1--worker-class gevent wsgiapp:application
Directory =/srv/awesome/www
user = Www-data
Startsecs = 3

Redirect_stderr = True
Stdout_logfile_maxbytes = 50MB
Stdout_logfile_backups = 10
Stdout_logfile =/srv/awesome/log/app.log

The configuration file specifies the command line to start Gunicorn by specifying the service name Awesome,command by [Program:awesome], and setting the Gunicorn's boot port to 9000,wsgi handler function entry is Wsgiapp: Application.

After restarting supervisor, you can start and stop Supervisor managed services at any time:

$ sudo supervisorctl reload$ sudo supervisorctl start awesome$ sudo supervisorctl statusawesome        RUNNING  pid 1401, Uptime 5:01:34

Configure Nginx

Supervisor is only responsible for running Gunicorn, we also need to configure Nginx. Put the configuration file awesome in the/etc/nginx/sites-available/directory:

server {  listen   80; # Listening 80 Port  root    /srv/awesome/www;  Access_log/srv/awesome/log/access_log;  Error_log/srv/awesome/log/error_log;  # server_name awesome.liaoxuefeng.com; # config domain  # handles static files/favicon.ico:  location/favicon.ico {    root/srv/awesome/www;  }  # Handling Static Resources: Location  ~ ^\/static\/.*$ {    root/srv/awesome/www;  }  # Dynamic Request forwarding to Port 9000 (Gunicorn): Location  /{    proxy_pass    http://127.0.0.1:9000;    Proxy_set_header x-real-ip $remote _addr;    Proxy_set_header Host $host;    Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;  }}

Then create a soft link in the/etc/nginx/sites-enabled/directory:

$ pwd/etc/nginx/sites-enabled$ sudo ln-s/etc/nginx/sites-available/awesome.

Let Nginx reload the configuration file, no accident, our Awesome-python-webapp should run normally:

$ Sudo/etc/init.d/nginx Reload

If there are any errors, you can find Nginx and the log of the app itself under/srv/awesome/log. If supervisor start the Times wrong, you can view the log of supervisor under/var/log/supervisor.

If all goes well, you can access the Awesome-python-webapp on the Linux server in your browser:

If you update your code in the development environment, you only need to do it at the command line:

$ fab build$ Fab Deploy

Automatic deployment Complete! Refresh your browser to see the effects of server code updates.
Friendship Link

Suspicion of foreign network slow children's shoes, please visit NetEase and Sohu Mirror site:

http://mirrors.163.com/

http://mirrors.sohu.com/

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.