apache在centos7中是Apache HTTP server。如下對(duì)httpd的解釋就是Apache HTTP Server。所以想安裝apache其實(shí)是要安裝httpd。
httpd.x86_64 : Apache HTTP Server
安裝:
# yum install httpd
設(shè)置httpd服務(wù)開機(jī)啟動(dòng)
[root@yl-web httpd]# /sbin/chkconfig httpd onNote: Forwarding request to 'systemctl enable httpd.service'.ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'
啟動(dòng)httpd服務(wù)
[root@yl-web httpd]# /sbin/service httpd startRedirecting to /bin/systemctl start httpd.service
訪問ip驗(yàn)證一下,成功!

httpd默認(rèn)的配置文件目錄為
[root@yl-web httpd]# cd /etc/httpd/[root@yl-web httpd]# lsconf conf.d conf.modules.d logs modules run
主配置文件是/etc/httpd/conf/httpd.conf。
配置存儲(chǔ)在的/etc/httpd/conf.d/目錄。
1、主配置文件看一下主配置文件httpd.conf里有用的配置項(xiàng)
#服務(wù)器根目錄ServerRoot "/etc/httpd"#端口#Listen 12.34.56.78:80Listen 80#域名+端口來標(biāo)識(shí)服務(wù)器,沒有域名用ip也可以#ServerName www.example.com:80#不許訪問根目錄<Directory /> AllowOverride none Require all denied</Directory># 文檔目錄DocumentRoot "/var/www/html"# 對(duì) /var/www目錄訪問限制<Directory "/var/www"> AllowOverride None # Allow open access: Require all granted</Directory># 對(duì)/var/www/html目錄訪問限制<Directory "/var/www/html"> Options Indexes FollowSymLinks AllowOverride None Require all granted</Directory># 默認(rèn)編碼AddDefaultCharset UTF-8#EnableMMAP offEnableSendfile on# include進(jìn)來其它配置文件IncludeOptional conf.d/*.conf2、下載配置mod_wsgi
安裝mod_wsgi前先進(jìn)行apache的apxs擴(kuò)展
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
在項(xiàng)目目錄下新建wsgi,里面新建django.wsgi,內(nèi)容如下
import osimport sysimport django.core.handlers.wsgifrom django.conf import settings# Add this file path to sys.path in order to import settingssys.path.insert(0, os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))os.environ['DJANGO_SETTINGS_MODULE'] = 'lxyPRoject.settings'sys.stdout = sys.stderrDEBUG = Trueapplication = django.core.handlers.wsgi.WSGIHandler()
配置wsgi時(shí),
修改了wsgi的配置后必須重啟httpd服務(wù)。
3、配置django項(xiàng)目虛擬主機(jī)在/etc/httpd/conf.d中添加配置文件lxyproject.conf,內(nèi)容如下
<VirtualHost *:80>WSGIScriptAlias / /srv/lxyproject/wsgi/django.wsgiAlias /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.logLogLevel warn</VirtualHost>
其中
WSGIScriptAlias 直接告訴apache,這個(gè)虛擬主機(jī)中,請(qǐng)求/就交給WSGI處理,也就是項(xiàng)目中配置的django.wsgi會(huì)指明。
Alias 說明訪問/static/直接從DocumentRoot中獲取,而無需經(jīng)過WSGI處理。
現(xiàn)在就可以通過apache服務(wù)器配置的IP訪問django項(xiàng)目了。

How to use django with mod_wsgi
本文作者starof,因知識(shí)本身在變化,作者也在不斷學(xué)習(xí)成長(zhǎng),文章內(nèi)容也不定時(shí)更新,為避免誤導(dǎo)讀者,方便追根溯源,請(qǐng)諸位轉(zhuǎn)載注明出處:http://www.CUOXin.com/starof/p/4685132.html有問題歡迎與我討論,共同進(jìn)步。
新聞熱點(diǎn)
疑難解答
圖片精選