1. 程式人生 > >redhat + apache2(httpd) + django2.0 + python3.6 搭建流程

redhat + apache2(httpd) + django2.0 + python3.6 搭建流程

安裝並啟動 httpd
sudo yum install httpd
sudo systemctl enable httpd
sudo systemctl start httpd

主配置檔案是/etc/httpd/conf/httpd.conf。

配置儲存在的/etc/httpd/conf.d/目錄。



# wsgi
#LoadModule  wsgi_module modules/mod_wsgi.so
LoadModule wsgi_module "/usr/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/usr"

WSGIPythonPath /var/git-project/mypythonwork/django_project/mysite


<VirtualHost *:80>

WSGIScriptAlias /test /var/git-project/mypythonwork/django_project/mysite/mysite/wsgi.py
Alias /static/ /var/git-project/mypythonwork/django_project/mysite/static/


ServerName xxx.xxx.xxx.xxx 
 

<Directory /var/git-project/mypythonwork/django_project/mysite/static>
    Options Indexes  FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>


<Directory /var/git-project/mypythonwork/django_project/mysite/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>


ErrorLog   /etc/httpd/logs/myproject.error.log
LogLevel warn

</VirtualHost>

配置檔案需要根據下文進行對應修改

安裝mod_wsgi

安裝httpd python36 開發包,

yum install -y httpd-devel

yum install python36-devel

然後用 pip 安裝 mod-wsgi

 pip3 install mod-wsgi

然後 呼叫 mod_wsgi-express module-config  就可以得到 前面需要的配置

LoadModule wsgi_module "/usr/lib64/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so"
WSGIPythonHome "/usr"

跟隨官方教程 建立 django 專案

然後 在 settings.py專案設定中

     配置 ALLOWED_HOSTS 為伺服器IP

     最後一行新增STATIC_ROOT = '/var/django-project/mysite/static/'

收集靜態檔案

執行 python36 manage.py collectstatic 

重啟httpd

apachectl configtest

apachectl restart

配置完成