首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django 2.1在centos 7上使用apache、mod_wsgi、python3 venv部署

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

Server Fault用户
提问于 2018-11-23 12:40:28
回答 1查看 8.6K关注 0票数 1

我遇到了这个看似最相关的部署选项,因为指南似乎要么引用了mod_wsgi和python2,要么在基于deb的系统上使用了不同的预期路径。

因此,我遵循以下步骤:

代码语言:javascript
复制
#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内容提供服务

代码语言:javascript
复制
<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成功启动,但我一直在错误日志中获得以下内容:

代码语言:javascript
复制
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

代码语言:javascript
复制
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.
EN

回答 1

Server Fault用户

回答已采纳

发布于 2018-11-27 17:47:35

我有所有事情要做。以下是完整的步骤:

代码语言:javascript
复制
yum install epel-release centos-release-scl 
yum install python36 python36-devel httpd httpd-devel rh-python36-mod_wsgi`  

检查rh-python36-mod_wsgi是否将所有内容放置在正确的dirs中

代码语言:javascript
复制
rpm -ql rh-python36-mod_wsgi

如在下列文件中发现:

代码语言:javascript
复制
/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  

将其移至:

代码语言:javascript
复制
/etc/httpd/conf.modules.d/10-rh-python36-wsgi.conf  
/etc/httpd/modules/mod_rh-python36-wsgi.so  

创建python3 venv。如果您正在从某个地方迁移,而不是在其中创建,请选中SEL以确保apache可以使用它。

代码语言:javascript
复制
cd /var/www
python36 -m venv django-venv
source /var/www/django-venv/activate

在/etc/httpd/conf.d/django.conf配置虚拟主机

代码语言:javascript
复制
<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标签:

代码语言:javascript
复制
restorecon -Rv /var/www

使apache拥有的项目dir:

代码语言:javascript
复制
chown -R apache:apache /var/www/mysite

如果使用SQLite,允许SEL允许apache访问它(需要改进):

代码语言:javascript
复制
semanage boolean -p http_unified on
票数 7
EN
页面原文内容由Server Fault提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://serverfault.com/questions/941341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档