centos7 apache httpd安裝和配置django項目


一、安裝httpd服務

apache在centos7中是Apache HTTP server。如下對httpd的解釋就是Apache HTTP Server。所以想安裝apache其實是要安裝httpd。

httpd.x86_64 : Apache HTTP Server

安裝:

# yum install httpd

設置httpd服務開機啟動

[root@yl-web httpd]# /sbin/chkconfig httpd on Note: Forwarding request to 'systemctl enable httpd.service'. ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

啟動httpd服務

[root@yl-web httpd]# /sbin/service httpd start Redirecting to /bin/systemctl start httpd.service

訪問ip驗證一下,成功!

二、配置

httpd默認的配置文件目錄為

[root@yl-web httpd]# cd /etc/httpd/
[root@yl-web httpd]# ls
conf  conf.d  conf.modules.d  logs  modules  run

主配置文件是/etc/httpd/conf/httpd.conf。

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

1、主配置文件

看一下主配置文件httpd.conf里有用的配置項

#服務器根目錄
ServerRoot "/etc/httpd" #端口
#Listen 12.34.56.78:80 Listen 80 #域名+端口來標識服務器,沒有域名用ip也可以
#ServerName www.example.com:80 #不許訪問根目錄
<Directory /> AllowOverride none Require all denied </Directory> # 文檔目錄
DocumentRoot "/var/www/html" # 對 /var/www目錄訪問限制
<Directory "/var/www"> AllowOverride None # Allow open access: Require all granted </Directory> # 對/var/www/html目錄訪問限制 <Directory "/var/www/html">    Options Indexes FollowSymLinks    AllowOverride None   Require all granted </Directory> # 默認編碼
AddDefaultCharset UTF-8 #EnableMMAP off EnableSendfile on
# include進來其它配置文件

IncludeOptional conf.d/*.conf

2、下載配置mod_wsgi

安裝mod_wsgi前先進行apache的apxs擴展

http-devel 是為了apxs,yum后你可以whereis apxs去尋找他,然后后邊編譯使用。

# yum install -y httpd-devel

下載

[root@yl-web collectedstatic]# yum install mod_wsgi

在httpd.conf中增加下面配置:

LoadModule  wsgi_module modules/mod_wsgi.so

該配置用來連接django.wsgi,使工程被apache加載。

配置django wsgi

在項目目錄下新建wsgi,里面新建django.wsgi,內容如下

import os
import sys
import django.core.handlers.wsgi
from django.conf import settings

# Add this file path to sys.path in order to import settings
sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))

os.environ['DJANGO_SETTINGS_MODULE'] = 'lxyproject.settings'

sys.stdout = sys.stderr

DEBUG = True

application = django.core.handlers.wsgi.WSGIHandler()

配置wsgi時,

  • 必須配置項目路徑到系統路徑中,因為要通過項目路徑找到settings.py配置文件。也就是sys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
  • DJANGO_SETTINGS_MODULE必須指向項目的settings.py文件。

修改了wsgi的配置后必須重啟httpd服務。

3、配置django項目虛擬主機

在/etc/httpd/conf.d中添加配置文件lxyproject.conf,內容如下

<VirtualHost *:80>

WSGIScriptAlias / /srv/lxyproject/wsgi/django.wsgi
Alias /static/ /srv/lxyproject/collectedstatic/

ServerName 10.1.101.31
#ServerName example.com
#ServerAlias www.example.com

<Directory /srv/lxyproject/collectedstatic>
    Options Indexes  FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<Directory /srv/lxyproject/wsgi/>
    Require all granted
</Directory>
ErrorLog   /etc/httpd/logs/lxyproject.error.log
LogLevel warn
</VirtualHost>

 其中

  • WSGIScriptAlias 直接告訴apache,這個虛擬主機中,請求/就交給WSGI處理,也就是項目中配置的django.wsgi會指明。
  • Alias 說明訪問/static/直接從DocumentRoot中獲取,而無需經過WSGI處理。

現在就可以通過apache服務器配置的IP訪問django項目了。

三、資源鏈接

How to use django with mod_wsgi

 

本文作者starof,因知識本身在變化,作者也在不斷學習成長,文章內容也不定時更新,為避免誤導讀者,方便追根溯源,請諸位轉載注明出處:http://www.cnblogs.com/starof/p/4685132.html有問題歡迎與我討論,共同進步。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM