pip install uwsgi
cd /usr/local wget http://nginx.org/download/nginx-1.7.4.tar.gz tar -zxvf nginx-1.7.4.tar.gz cd nginx-1.7.4 ./configure $默認安裝在/usr/local/nginx make make install
啓動nginx: /usr/local/nginx/sbin/nginx 其餘參數: [user@host dir]$ /usr/local/nginx/sbin/nginx -h nginx version: nginx/1.8.0 Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help 查看幫助信息 -v : show version and exit 顯示 nginx 的版本 -V : show version and configure options then exit 顯示 nginx 的版本,編譯器版本和配置參數 -t : test configuration and exit 不運行,而僅僅測試配置文件。nginx 將檢查配置文件的語法的正確性,並嘗試打開配置文件中所引用到的文件。 -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload 傳遞一個信號,stop快速關閉,quit從容關閉,reopen從新打開日誌文件、用於切換日誌文件,reload重載配置文件 -p prefix : set prefix path (default: /usr/local/nginx/) -c filename : set configuration file (default: conf/nginx.conf) 設置配置文件 -g directives : set global directives out of configuration file
├── db.sqlite3 ├── manage.py ├── okr │ ├── admin.py │ ├── apps.py │ ├── exc.py │ ├── __init__.py │ ├── migrations │ ├── models.py │ ├── okr_middleware.py │ ├── serializers.py │ ├── tests.py │ ├── urls.py │ ├── utils.py │ └── views.py ├── okr_manage │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── README.md ├── requirements.txt ├── static ├── templates │ ├── 404.html │ ├── back.html │ ├── Home.html │ ├── index_back.html │ ├── login.html │ ├── personal_goals.html │ ├── sector_strategy.html │ ├── team_goals.html │ └── test.html └── uwsgi.ini
# 建立test.py文件 def application(env, start_response): start_response("200 OK", [("Content-Type","text/html")]) return [b"Hello World"]
uwsgi --http :8001 --wsgi-file test.py # 訪問127.0.0.1:8001 返回hello world表明uwsgi正常可用
在咱們經過Django建立okr項目時,在子目錄okr_manage下已經幫咱們生成的 wsgi.py文件。因此,咱們只須要再建立uwsgi.ini配置文件便可,固然,uwsgi支持多種類型的配置文件,如xml,ini等。此處,使用ini類型的配置.html
# uwsgi.ini配置 [uwsgi] http = :9020 socket = /var/run/uwsgi.sock chdir = /opt/code/wp_api wsgi-file = WorkPlatFormApi/wsgi.py processes = 4 threads = 2 stats = 127.0.0.1:9195 vacuum = true chmod-socket = 666 enable-threads = true listen = 1024
uwsgi --ini uwsgi.ini
# 成功信息: # 注意查看uwsgi的啓動信息,若是有錯,就要檢查配置文件的參數是否設置有誤 [uWSGI] getting INI configuration from uwsgi.ini *** Starting uWSGI 2.0.17.1 (64bit) on [Mon Aug 27 06:16:03 2018] *** compiled with version: 4.8.5 20150623 (Red Hat 4.8.5-28) on 27 August 2018 05:39:18 os: Linux-3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 nodename: workstation-qa.yiguo.com machine: x86_64 clock source: unix pcre jit disabled detected number of CPU cores: 4 current working directory: /opt/okr detected binary path: /usr/local/python3/bin/uwsgi uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** chdir() to /opt/okr your processes number limit is 63082 your memory page size is 4096 bytes detected max file descriptor number: 1024 lock engine: pthread robust mutexes thunder lock: disabled (you can enable it with --thunder-lock) uWSGI http bound on :9000 fd 3 uwsgi socket 0 bound to TCP address 127.0.0.1:8000 fd 6 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** Python version: 3.7.0 (default, Aug 9 2018, 22:10:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] Python main interpreter initialized at 0x1f7b360 uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** python threads support enabled your server socket listen backlog is limited to 100 connections your mercy for graceful operations on workers is 60 seconds mapped 416880 bytes (407 KB) for 8 cores *** Operational MODE: preforking+threaded *** WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1f7b360 pid: 4511 (default app) uWSGI running as root, you can use --uid/--gid/--chroot options *** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** *** uWSGI is running in multiple interpreter mode *** spawned uWSGI master process (pid: 4511) spawned uWSGI worker 1 (pid: 4512, cores: 2) spawned uWSGI worker 2 (pid: 4513, cores: 2) spawned uWSGI worker 3 (pid: 4515, cores: 2) spawned uWSGI worker 4 (pid: 4517, cores: 2) *** Stats server enabled on 127.0.0.1:9191 fd: 22 *** spawned uWSGI http 1 (pid: 4520)
# vim /usr/local/nginx/conf/nginx.conf # 若是出現403問題,請設置項目目錄的權限,而且在nginx配置中去掉註釋# user=nobady 改爲user=root便可 server { listen 8099; # 指定的是nginx代理uwsgi對外的端口 server_name 127.0.0.1 # 網上大多資料都是設置的一個網址(例,www.example.com),我這裏若是設置成網址沒法訪問,因此,指定的到了本機默認ip 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 unix:/home/uwsgi.sock; uwsgi_read_timeout 600; } location /static { expires 30d; autoindex on; add_header Cache-Control private; alias /opt/okr/static/; # 這裏的目錄必須是當前項目的static目錄 } }
在進行配置的時候,我有個問題一直想不通。nginx究竟是如何uwsgi產生關聯。如今看來大概最主要的就是這兩行配置 include uwsgi_params; uwsgi_pass 127.0.0.1:8000; include 必須指定爲uwsgi_params;而uwsgi_pass指的本機IP的端口號與myweb_uwsgi.ini配置中的文件中的必須一致.
node
收集Django靜態文件,把Django自帶的靜態文件收集到同一個static中,否則訪問Django的admin頁面會找不到靜態文件。在django的setting文件中,添加下面一行內容.python
settings.py STATIC_ROOT = os.path.join(BASE_DIR, "static/") DEBUG = False ALLOWED_HOSTS = ["*"]
python manage.py collectstatic
uwsgi --ini uwsgi.ini /usr/local/nginx/sbin/nginx
http://127.0.0.1:8099
app_config.import_models() File "/usr/local/lib/python3.7/site-packages/django/apps/config.py", line 211, in import_models self.models_module = import_module(models_module_name) File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "<frozen importlib._bootstrap>", line 1006, in _gcd_import File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 677, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 728, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/models.py", line 2, in <module> from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager File "/usr/local/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 47, in <module> class AbstractBaseUser(models.Model): File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 117, in __new__ new_class.add_to_class('_meta', Options(meta, app_label)) File "/usr/local/lib/python3.7/site-packages/django/db/models/base.py", line 321, in add_to_class value.contribute_to_class(cls, name) File "/usr/local/lib/python3.7/site-packages/django/db/models/options.py", line 204, in contribute_to_class self.db_table = truncate_name(self.db_table, connection.ops.max_name_length()) File "/usr/local/lib/python3.7/site-packages/django/db/__init__.py", line 28, in __getattr__ return getattr(connections[DEFAULT_DB_ALIAS], item) File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 201, in __getitem__ backend = load_backend(db['ENGINE']) File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 110, in load_backend return import_module('%s.base' % backend_name) File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/usr/local/lib/python3.7/site-packages/django/db/backends/mysql/base.py", line 20, in <module> ) from err django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module. Did you install mysqlclient?
解決方法: 進入到settings目錄到,編輯__init__.py文件.增長如下代碼:mysql
import pymysql pymysql.install_as_MySQLdb()