使用 apache、mod_wsgi、python3 venv 在 centos 7 上部署 Django 2.1

使用 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

apache 配置为提供位于 /var/www/mysite 的 django 内容

<VirtualHost *:80>
 ServerAdmin [email protected]
 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.

答案1

我让所有东西都正常工作了。以下是完整步骤:

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

相关内容