nginx再linux下可以自己编译,我采用的编译选项为:
--prefix=/home/hzh/soft/softy/nginx-1.18.0
--with-threads
--with-file-aio
--with-http_ssl_module
--with-http_v2_module
--with-http_realip_module
--with-http_addition_module
--with-http_xslt_module
--with-http_image_filter_module
--with-http_geoip_module
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_auth_request_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_degradation_module
--with-http_slice_module
--with-http_stub_status_module
--with-http_perl_module
--with-mail
--with-mail_ssl_module
--with-stream
--with-stream_ssl_module
--with-stream_realip_module
--with-stream_geoip_module
--with-stream_ssl_preread_module
--with-cpp_test_module
--with-compat
--with-pcre
--with-pcre-jit
--with-libatomic
--with-debug
--with-perl_modules_path=/home/hzh/soft/softy/nginx-1.18.0/perl_modules
uwsgi 采用 pip 来安装:
$ vf activate env3.8.2
$ pip install uwsgi
nginx和uwsgi部署django项目的文档大致有如下参考:
https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
https://docs.djangoproject.com/en/3.1/howto/deployment/wsgi/uwsgi/
https://uwsgi-docs.readthedocs.io/en/latest/Management.html
我的部署示例:
我的django项目位于: /home/hzh/develop/u-chuang/webs/device_register
项目目录结构:
device_register
├── db.sqlite3
├── device_register
│ ├── asgi.py
│ ├── __init__.py
│ ├── __pycache__
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── manage.py
├── register
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ ├── 0001_initial.py
│ │ ├── 0002_auto_20201127_0317.py
│ │ ├── 0003_auto_20201127_0512.py
│ │ ├── 0004_auto_20201127_0515.py
│ │ ├── 0005_auto_20201127_0630.py
│ │ ├── 0006_auto_20201128_1122.py
│ │ ├── 0007_auto_20201128_1403.py
│ │ ├── __init__.py
│ │ └── __pycache__
│ ├── models.py
│ ├── __pycache__
│ ├── static
│ │ └── register
│ │ └── style.css
│ ├── templates
│ │ └── register
│ │ └── waiting_lists.html
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── static
│ ├── admin
│ ├── grappelli
│ └── register
│ └── style.css
└── templates
└── admin-hzh
├── actions.html
├── app_list.html
├── base_site.html
├── change_list_results.html
└── index.html
其中 device_register/static 目录是静态目录,里面包含了app register的目录及 grappelli(一个admin美化模块) 目录。
部署配置文件结构:
nginx-uwsgi-config
├── nginx-config
│ ├── mime.types
│ ├── my-nginx.conf
│ ├── nginx.conf
│ └── uwsgi_params
└── uwsgi-config
└── uwsgi.ini
其中的 mime.types 和 uwsgi_params 是nginx自带的,没有任何改变; nginx.conf 大部分是nginx自带的,有点改变,全文如下:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include /tmp/hzh/my-nginx.conf;
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
其中 my-nginx.conf 是与自己项目相关的配置:
# the upstream component nginx needs to connect to
upstream django {
server unix:///tmp/hzh/device_register-uwgsi-nginx.sock; # for a file socket
# server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8000;
# the domain name it will serve for
server_name example.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
# Django media
location /media {
alias /path/to/your/mysite/media; # your Django project's media files - amend as required
}
location /static {
alias /home/hzh/develop/u-chuang/webs/device_register/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /home/hzh/soft/nginx/conf/uwsgi_params; # the uwsgi_params file you installed
}
}
其中 uwsgi.ini 是uwsgi的配置文件:
[uwsgi]
# Django-related settings
# the base directory (full path)
chdir = /home/hzh/develop/u-chuang/webs/device_register
# Django's wsgi file
module = device_register.wsgi:application
# the virtualenv (full path)
home = /home/hzh/.virtualenvs/env3.8.2
env = DJANGO_SETTINGS_MODULE=device_register.settings
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 10
# the socket (use the full path to be safe
socket = /tmp/hzh/device_register-uwgsi-nginx.sock
# ... with appropriate permissions - may be needed
chmod-socket = 664
pidfile = /tmp/hzh/device_register-uwsgi.pid
# clear environment on exit
vacuum = true
重要,建议nginx与uwsgi通信使用 file socket,而不是使用 port socket,因为 port socket 相当于暴露了 uwsgi 给外部,安全级别不好。
这些配置文件准备好后,就可以运行uwsgi和nginx了:
$ uwsgi --ini uwsgi.ini
$ /home/hzh/soft/nginx/sbin/nginx -c /tmp/hzh/nginx-uwsgi-config/nginx-config/nginx.conf