YouTip LogoYouTip

Django Nginx Uwsgi

In previous chapters, we used **python manage.py runserver** to run the server. This is only suitable for use in a testing environment. For a production service, we need a stable and persistent server, such as Apache, Nginx, lighttpd, etc. This tutorial will use Nginx as an example. > You can also refer directly to: (#) * * * ## Installing Basic Development Packages The installation steps under CentOS are as follows: yum groupinstall "Development tools" yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel CentOS comes with Python 2.4.3, but we can install Python 2.7.5: cd ~ wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 tar xvf Python-2.7.5.tar.bz2 cd Python-2.7.5./configure --prefix=/usr/local make && make altinstall ### Installing Python Package Management easy_install package: [https://pypi.python.org/pypi/distribute](https://pypi.python.org/pypi/distribute) Installation steps: cd ~ wget https://pypi.python.org/packages/source/d/distribute/distribute-0.6.49.tar.gz tar xf distribute-0.6.49.tar.gz cd distribute-0.6.49 python2.7 setup.py install easy_install --version pip package: [https://pypi.python.org/pypi/pip](https://pypi.python.org/pypi/pip) The advantage of installing pip is that you can use `pip list` and `pip uninstall` to manage Python packages. `easy_install` does not have this functionality; it only has uninstall. * * * ## Installing uwsgi uwsgi: [https://pypi.python.org/pypi/uWSGI](https://pypi.python.org/pypi/uWSGI) Detailed uwsgi parameters: [http://uwsgi-docs.readthedocs.org/en/latest/Options.html](http://uwsgi-docs.readthedocs.org/en/latest/Options.html) pip install uwsgi uwsgi --version # Check uwsgi version Test if uwsgi is working correctly: Create a new file named `test.py` with the following content: def application(env, start_response):start_response('200 OK', [('Content-Type','text/html')])return "Hello World" Then run in the terminal: uwsgi --http :8001 --wsgi-file test.py Enter http://127.0.0.1:8001 in your browser to see if "Hello World" is output. If there is no output, please check your installation process. * * * ## Installing Django pip install django Test if Django is working correctly by running: django-admin.py startproject demosite cd demosite python2.7 manage.py runserver 0.0.0.0:8002 Enter http://127.0.0.1:8002 in your browser to check if Django is running normally. * * * ## Installing Nginx The installation command is as follows: cd ~ wget http://nginx.org/download/nginx-1.5.6.tar.gz tar xf nginx-1.5.6.tar.gz cd nginx-1.5.6./configure --prefix=/usr/local/nginx-1.5.6 --with-http_stub_status_module --with-http_gzip_static_module make && make install You can read (#) to learn more. * * * ## uwsgi Configuration uwsgi supports multiple configuration methods such as ini and xml. This tutorial uses ini as an example. Create a new file `/etc/uwsgi9090.ini` and add the following configuration: socket = 127.0.0.1:9090 master = true //main process vhost = true //multi-site mode no-site = true //In multi-site mode, do not set entry module and file workers = 2 //number of child processes reload-mercy = 10 vacuum = true //clean up files when exiting or restarting max-requests = 1000 limit-as = 512 buffer-size = 30000 pidfile = /var/run/uwsgi9090.pid //pidfile, used for the script below to start and stop this process daemonize = /website/uwsgi9090.log * * * ## Nginx Configuration Find the Nginx installation directory (e.g., `/usr/local/nginx/`), open the `conf/nginx.conf` file, and modify the server configuration: server { listen 80; server_name localhost; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9090; //Must be consistent with uwsgi settings uwsgi_param UWSGI_SCRIPT demosite.wsgi; //entry file, namely wsgi.pyposition relative to the project root directory,β€œ.”equivalent to one directory level uwsgi_param UWSGI_CHDIR /demosite; //project root directory index index.html index.htm; client_max_body_size 35m; } } You can read (#) to learn more. After completing the setup, run the following in the terminal: uwsgi --ini /etc/uwsgi9090.ini &/usr/local/nginx/sbin/nginx Enter http://127.0.0.1 in your browser, and you should see Django's "It work".
← Python Clear ListPython Reversing List β†’