In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
In this article, the editor introduces in detail "how to deploy Django through Nginx based on ubuntu", the content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to deploy Django through Nginx based on ubuntu" can help you solve your doubts.
First, install nginx
Nginx is a lightweight web server / reverse proxy server and email (imap/pop3) proxy server, and is distributed under a bsd-like protocol. Its characteristic is that it occupies less memory and has strong concurrency ability. in fact, the concurrency ability of nginx does perform well in the same type of web server.
Nginx is also a very popular web server. Using it to deploy django, we will also make a brief introduction here.
Nginx official website:
Open the ubuntu console (ctrl+alt+t) to take advantage of ubuntu's warehouse installation.
Fnngj@ubuntu:~$ sudo apt-get install nginx # installation
Start nginx:
Fnngj@ubuntu:~$ / etc/init.d/nginx start # start fnngj@ubuntu:~$ / etc/init.d/nginx stop # shut down fnngj@ubuntu:~$ / etc/init.d/nginx restart # restart
Modify the nginx default port number, open the / etc/nginx/nginx.conf file, and modify the port number.
Server {listen 8088; # modify the port number server_name localhost; # charset koi8-r; # access_log logs/host.access.log main; location / {root html; index index.html index.htm;}
Approximately in line 36 of the file, change the default port number of 80 to another port number, such as 8088. Because the default port number of 80 is easily occupied by other applications.
Then, restart nginx with the above command. Visit: http://127.0.0.1:8088/
If it appears as shown in the figure above, nginx starts successfully.
Second, install uwsgi
Install uwsgi through pip.
Root@ubuntu:/etc# python3-m pip install uwsgi
Test the uwsgi and create the test.py file:
Def application (env, start_response): start_response ('200 ok', [(' content-type','text/html')]) return [b "hello world"]
Run the file through uwsgi.
Fnngj@ubuntu:~/pydj$ uwsgi-http: 8001-wsgi-file test.py
Next, configure django to connect to uwsgi. Here, my django project location is assumed to be: / home/fnngj/pydj/myweb
The copy code is as follows:
Uwsgi-- http: 8001-- chdir / home/fnngj/pydj/myweb/-- wsgi-file myweb/wsgi.py-- master-- processes 4-- threads 2-- stats 127.0.0.1
Common options:
Http: protocol type and port number
Processes: number of processes opened
Workers: the number of processes started is the same as processes (spawn the specified number ofworkers / processes, according to the official website)
Chdir: specify the running directory (chdir to specified directory before apps loading)
Wsgi-file: loading wsgi-file (load .wsgi file)
Stats: enable the status service (enable the stats server on the specified address) at the specified address
Threads: running thread. Because of the existence of gil, I think this is really useless. (run each worker in prethreaded mode with the specified number of threads)
Master: allow the main process to exist (enable master process)
Daemonize: causes the process to run in the background and logs to the specified log file or udp server (daemonize uwsgi). In fact, the most commonly used is to output the run record to a local file.
Pidfile: specify the location of the pid file and record the pid number of the main process.
Vacuum: automatically cleans up the environment and deletes unix socket and pid files (try to remove all of the generated file/sockets) when the server exits
III. Nginx+uwsgi+django
Next, we are going to combine the three. First, list the files required for the project:
Myweb/ ├── manage.py ├── myweb/ │ ├── _ _ init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── myweb_uwsgi.ini
The wsgi.py file that has been generated for us in the subdirectory myweb when we created the myweb project through django. So, we just need to create the myweb_uwsgi.ini configuration file again, and of course, uwsgi supports many types of configuration files, such as xml,ini, and so on. Here, a configuration of type ini is used.
# myweb_uwsgi.ini file [uwsgi] # django-related settingssocket =: 800 million the base directory (full path) chdir = / home/fnngj/pydj/myweb# django s wsgi filemodule = myweb.wsgi# process-related settings# mastermaster = true# maximum number of worker processesprocesses =... With appropriate permissions-may be needed# chmod-socket = 66 clear environment on exitvacuum = true
This configuration is actually equivalent to documenting the wsgi command in the previous section, followed by a bunch of parameters.
Socket specifies the port number on which the project executes.
Chdir specifies the directory of the project.
Module myweb.wsgi, to put it this way, for myweb_uwsgi.ini files, there is a myweb directory comparable to it, and there is a wsgi.py file in this directory.
For other parameters, you can refer to the introduction of the parameters in the previous section.
Next, change to the myweb project directory and start the project by reading the myweb_uwsgi.ini file with the uwsgi command.
Fnngj@ubuntu:~$ cd / home/fnngj/pydj/myweb/fnngj@ubuntu:~/pydj/myweb$ uwsgi-- ini myweb_uwsgi.ini [uwsgi] getting ini configuration from myweb_uwsgi.ini*** starting uwsgi 2.0.12 (32bit) on [sat mar 12 13:05:06 2016] * * compiled with version: 4.8.4 on 26 january 2016 06:14:41os: linux-3.19.0-25-generic # 26~14.04.1-ubuntu smp fri jul 24 21:18: 00 utc 2015nodename: ubuntumachine: i686clock source: unixdetected number of cpu cores: 2current working directory: / home/fnngj/pydj/mywebdetected binary path: / usr/local/bin/uwsgi!!! No internal routing support, rebuild with pcre support! chdir () to / home/fnngj/pydj/mywebyour processes number limit is 15962your memory page size is 4096 bytesdetected max file descriptor number: 1024lock engine: pthread robust mutexesthunder lock: disabled (you can enable it with-- thunder-lock) uwsgi socket 0 bound to tcp address: 8000 fd 3python version: 3.4.3 (default, oct 14 2015, 20:37:06) [gcc 4.8.4] * python threads support is disabled. You can enable it with-enable-threads * python main interpreter initialized at 0x8b52dc0your server socket listen backlog is limited to 100 connectionsyour mercy for graceful operations on workers is 60 secondsmapped 319920 bytes (312 kb) for 4 cores*** operational mode: preforking * wsgi app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app) * * uwsgi is running in multiple interpreter mode * * spawned uwsgi master process (pid: 7158) spawned uwsgi worker 1 (pid: 7160, cores: 1) spawned uwsgi worker 2 (pid: 7161) Cores: 1) spawned uwsgi worker 3 (pid: 7162, cores: 1) spawned uwsgi worker 4 (pid: 7163, cores: 1)
Pay attention to check the startup information of uwsgi, and if there is anything wrong, check whether the parameters of the configuration file are set incorrectly.
The next thing to do is to modify the nginx.conf configuration file. Open the / etc/nginx/nginx.conf file and add the following.
…… Server {listen 8099; server_name 127.0.0.1 charset utf-8; access_log / var/log/nginx/myweb_access.log; error_log / var/log/nginx/myweb_error.log; client_max_body_size 75m; location / {include uwsgi_params; uwsgi_pass 127.0.0.1 charset utf-8; access_log 8000; uwsgi_read_timeout 2;} location / static {expires 30d; autoindex on Add_header cache-control private; alias / home/fnngj/pydj/myweb/static/;}}.
Listen specifies the port number external to the nginx proxy uwsgi.
Most of the materials on the server_name are set to a URL (for example, www.example.com). If I set it up here, the URL cannot be accessed, so the specified one goes to the local default ip.
In the configuration, I have a problem that I can't figure out all the time. Exactly how nginx is related to uwsgi. Now it seems that probably the most important thing is the configuration of these two lines.
Include uwsgi_params
Uwsgi_pass 127.0.0.1:8000
Include must be specified as uwsgi_params; and the port of the native ip referred to by uwsgi_pass must always be in the myweb_uwsgi.ini configuration file.
Now restart nginx and look at the command above to restart nginx. Then, visit: http://127.0.0.1:8099/
By pointing to this ip and port number, the request should be nginx first. If you execute some requests on the page, you will see that these requests will eventually be transferred to uwsgi for processing.
After reading this, the article "how to deploy Django through Nginx based on ubuntu" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.