Centos7
使用 apache、mod_wsgi、python3 venv 在 centos 7 上部署 Django 2.1
我偶然發現了這個看似最相關的部署選項,因為指南似乎要麼使用 python2 引用 mod_wsgi,要麼在預期路徑不同的基於 deb 的系統上進行部署。
所以我遵循以下步驟:
#repos for python3.6, wsgi for python3.6 yum install epel-release centos-release-scl #base packages yum install python36 python36-devel httpd httpd-devel rh-python36-mod_wsgi #python3.6 venv cd /var/www; python36 -m venv django-venv source django-venv/bin/activate pip3 install django #apache config to support wsgi edit /etc/httpd/conf/httpd.conf to include LoadModule wsgi_module modules/mod_wsgi.so
用於提供位於 /var/www/mysite 的 django 內容的 apache 配置
<VirtualHost *:80> ServerAdmin foo@mysite.com ServerName mysite.com ServerAlias www.mysite.com WSGIDaemonProcess mysite python-home=/var/www/django-venv/ python-path=/var/www/django-venv/lib/python3.6/site-packages WSGIProcessGroup mysite WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py Alias /static /var/www/mysite/static <Directory /var/www/mysite/mysite/static> Require all granted </Directory> <Directory /var/www/mysite/mysite> <Files wsgi.py> Require all granted </Files> </Directory> </VirtualHost>
SEL 變化:
chown apache:apache -R /var/www/mysite chown apache:apache -R /var/www/django-venv
httpd 成功啟動,但我在錯誤日誌中不斷收到以下內容:
Current thread 0x00007f5b5a486880 (most recent call first): [Fri Nov 23 14:29:02.019635 2018] [core:notice] [pid 4837] AH00052: child pid 5159 exit signal Aborted (6) Fatal Python error: Py_Initialize: Unable to get the locale encoding ModuleNotFoundError: No module named 'encodings'
你們能否確認到目前為止我在此設置的步驟中缺少什麼?
其他一些資訊:
ll /etc/httpd/modules/*wsgi* -rwxr-xr-x. 1 root root 966K Nov 23 09:13 /etc/httpd/modules/mod_wsgi.so
systemctl status -l httpd ● httpd.service - The Apache HTTP Server Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled) Active: active (running) since Fri 2018-11-23 14:26:16 EET; 13min ago Docs: man:httpd(8) man:apachectl(8) Main PID: 4837 (httpd) Status: "Total requests: 3; Current requests/sec: 0; Current traffic: 0 B/sec" CGroup: /system.slice/httpd.service ├─4837 /usr/sbin/httpd -DFOREGROUND ├─4839 /usr/sbin/httpd -DFOREGROUND ├─4840 /usr/sbin/httpd -DFOREGROUND ├─4841 /usr/sbin/httpd -DFOREGROUND ├─4842 /usr/sbin/httpd -DFOREGROUND ├─4843 /usr/sbin/httpd -DFOREGROUND └─4850 /usr/sbin/httpd -DFOREGROUND Nov 23 14:26:16 www1 systemd[1]: Starting The Apache HTTP Server... Nov 23 14:26:16 www1 httpd[4837]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using fe80::a7a7:b61c:5ffc:b91a. Set the 'ServerName' directive globally to suppress this message Nov 23 14:26:16 www1 systemd[1]: Started The Apache HTTP Server.
我得到了一切工作。以下是完整的步驟:
yum install epel-release centos-release-scl yum install python36 python36-devel httpd httpd-devel rh-python36-mod_wsgi`
檢查是否
rh-python36-mod_wsgi
將所有內容都放在正確的目錄中rpm -ql rh-python36-mod_wsgi
如果發現於:
/opt/rh/httpd24/root/etc/httpd/conf.modules.d/10-rh-python36-wsgi.conf /opt/rh/httpd24/root/usr/lib64/httpd/modules/mod_rh-python36-wsgi.so
將它們移至:
/etc/httpd/conf.modules.d/10-rh-python36-wsgi.conf /etc/httpd/modules/mod_rh-python36-wsgi.so
創建 python3 venv。如果你是從某個地方搬來的,而不是在裡面創建,請檢查 SEL 以確保 apache 可以使用它
cd /var/www python36 -m venv django-venv source /var/www/django-venv/activate
在 /etc/httpd/conf.d/django.conf 配置虛擬主機
<VirtualHost *:80> ServerName mysite.com ServerAlias www.mysite.com WSGIDaemonProcess mysite python-home=/var/www/django-venv python-path=/var/www/mysite WSGIProcessGroup mysite WSGIScriptAlias / /var/www/mysite/mysite/wsgi.py Alias /static /var/www/mysite/static <Directory /var/www/mysite/static> Require all granted </Directory> <Directory /var/www/mysite/mysite> <Files wsgi.py> Require all granted </Files> </Directory> </VirtualHost>
如果項目移至
/var/www
,請恢復正確的 SEL 標籤:restorecon -Rv /var/www
使項目目錄歸 apache 所有:
chown -R apache:apache /var/www/mysite
如果使用 SQLite,允許 SEL 允許 apache 訪問它(需要改進):
semanage boolean -p http_unified on