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 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'
啟動httpd服務
[root@yl-web httpd]# /sbin/service httpd startRedirecting to /bin/systemctl start httpd.service
訪問ip驗證一下,成功!
httpd默認的配置文件目錄為
[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。
配置存儲在的/etc/httpd/conf.d/目錄。
1、主配置文件看一下主配置文件httpd.conf里有用的配置項
#服務器根目錄ServerRoot "/etc/httpd"#端口#Listen 12.34.56.78:80Listen 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 offEnableSendfile on# include進來其它配置文件IncludeOptional conf.d/*.conf2、下載配置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 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時,
修改了wsgi的配置后必須重啟httpd服務。
3、配置django項目虛擬主機在/etc/httpd/conf.d中添加配置文件lxyproject.conf,內容如下
<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,這個虛擬主機中,請求/就交給WSGI處理,也就是項目中配置的django.wsgi會指明。
Alias 說明訪問/static/直接從DocumentRoot中獲取,而無需經過WSGI處理。
現在就可以通過apache服務器配置的IP訪問django項目了。
How to use django with mod_wsgi
本文作者starof,因知識本身在變化,作者也在不斷學習成長,文章內容也不定時更新,為避免誤導讀者,方便追根溯源,請諸位轉載注明出處:http://www.CUOXin.com/starof/p/4685132.html有問題歡迎與我討論,共同進步。
|
新聞熱點
疑難解答