uwsgi+Django
View uwsgi version and information: cmd - >> uwsgi
First, to test whether the normal use uwsgi
1. Create a test file index.py
vim index.py
write the code below:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"]
run:
uwsgi --http :9090 --wsgi-file index.py
Then you can access through a browser ip + port to see the Hello Worid
2. Add the concurrent (to increase processing speed)
uwsgi --http: 9090 --wsgi-File index.py --processes 4 --threads 2
Then it is to deploy a Django.
1. Create an ini file in the project root directory
vim uwsgi.ini
Write the code:
[Uwsgi] http = 0.0.0.0:8000 chdir = /usr/www/DjangoSpider/ wsgi-file = DjangoSpider/wsgi.py processes = 4 threads = 2
NOTE: Django in ALLOWED_HOSTS = [] to ALLOWED_HOSTS = [ '*']
2. Start
uwsgi uwsgi.ini
Then you can add ip port access through the deployment of a good project, but this time a static file is not showing up, and we need to show we can configure nginx perfect.
uwsgi+nginx+Django
1.cd default.conf modify the file into the '/etc/nginx/cond.d' directory (it is recommended to keep a copy of the source file)
server { listen 80; localhost server_name; # Uniform Domain Name provided by external users # Forwards the request to the dynamic server uwsgi LOCATION / { # comprises request parameters uwsgi the include uwsgi_params; # care uwsgi request to the server (server uwsgi ip and port) uwsgi_pass 127.0.0.1:8000; # single server uwsgi (no load equilibrium) # uwsgi_pass upstream1; } # If it is a static resource request is a direct response resources. LOCATION / static { # specify the directory to store static files (. need to modify / usr / ... directory operating authority chmod 777 $ / usr) Alias / usr / the WWW / project name / static / ; } }
2. End modify the configuration file, to re reload it.
nginx -s reload
3. Request 80 interfaces, may appear permission problem, then you need to turn off selinux
setenforce 0
4. Collect static resources.
When the admin backend access without style, you need to collect Django static resources.
# Project name /settings.py (project configuration, STATIC_ROOT set the path to collect static resources): STATIC_URL = ' / static / ' STATICFILES_DIRS = [os.path.join (base_dir, ' static ' )] # Specifies the path to collect static files (need to modify / var / ... operating authority catalog) STATIC_ROOT = ' ./static '
Then run the command:
python manage.py collectstatic
ini file in the root directory 5. Modify project
vim uwsgi.ini
[uwsgi] # http = 0.0.0.0:8000 Socket=0.0.0.0:8000 chdir = /usr/www/DjangoSpider/ wsgi-file = DjangoSpider/wsgi.py processes = 4 threads = 2
Start:
Start nginx: systemctl start / restart nginx
project root directory: uwsgi uwsgi.ini
see whether nginx start: systemctl status nginx
When web access ip packet 502, should be the authority, so we need to run the corresponding command.
setenforce 0