频道栏目
首页 > 资讯 > 其他 > 正文

centos7安装httpd服务

17-06-09        来源:[db:作者]  
收藏   我要投稿

centos7安装httpd服务。

一、安装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

#不许访问根目录

    AllowOverride none
    Require all denied


# 文档目录
DocumentRoot "/var/www/html"

# 对 /var/www目录访问限制

    AllowOverride None
    # Allow open access:
    Require all granted


# 对/var/www/html目录访问限制

   Options Indexes FollowSymLinks
   AllowOverride None
   Require all granted


# 默认编码
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,内容如下



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

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


    Options Indexes  FollowSymLinks
    AllowOverride None
    Require all granted



    Require all granted

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

其中

WSGIScriptAlias 直接告诉apache,这个虚拟主机中,请求/就交给WSGI处理,也就是项目中配置的django.wsgi会指明。
Alias 说明访问/static/直接从DocumentRoot中获取,而无需经过WSGI处理。

现在就可以通过apache服务器配置的IP访问django项目了。

三、资源链接

How to use django with mod_wsgi

相关TAG标签
上一篇:PHP基础学习之文件操作
下一篇:互联网的接入方式
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站