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有问题欢迎与我讨论,共同进步。

相关文章
|
12月前
|
应用服务中间件 Linux 网络安全
Centos 8.0中Nginx配置文件和https正书添加配置
这是一份Nginx配置文件,包含HTTP与HTTPS服务设置。主要功能如下:1) 将HTTP(80端口)请求重定向至HTTPS(443端口),增强安全性;2) 配置SSL证书,支持TLSv1.1至TLSv1.3协议;3) 使用uWSGI与后端应用通信(如Django);4) 静态文件托管路径设为`/root/code/static/`;5) 定制错误页面(404、50x)。适用于Web应用部署场景。
1031 87
|
10月前
|
存储 Linux Apache
在CentOS上配置SVN至Web目录的自动同步
通过上述配置,每次当SVN仓库中提交新的更改时,`post-commit`钩子将被触发,SVN仓库的内容会自动同步到指定的Web目录,从而实现代码的连续部署。
290 16
|
10月前
|
NoSQL 安全 Linux
设置Redis在CentOS7上的自启动配置
这些步骤总结了在CentOS 7系统上设置Redis服务自启动的过程。这些命令提供了一个直接且明了的方式,确保Redis作为关键组件在系统启动时能自动运行,保障了依赖于Redis服务的应用的稳定性和可用性。
733 9
|
Linux
Centos6配置阿里云yum源报错
在CentOS 6配置阿里云Yum源时,可能出现EPEL仓库访问报错(404 Not Found)。解决方法:编辑`/etc/yum.repos.d/epel.repo`文件,将`enabled`和`gpgcheck`参数设为0 ``` 此设置可解决仓库无法访问的问题。
2319 29
|
12月前
|
Ubuntu 安全 Linux
CentOS与Ubuntu中防火墙配置命令集汇
有了这些,你就能遨游在 CentOS 和 Ubuntu 的海洋中,频繁地改变你的防火墙设置,快速地应对各种安全威胁,同时也能保证你的系统可以正常工作。出发吧,勇敢的编程者,随着这些命令集的涌动,扬帆起航,走向安全的网络世界!
404 5
|
关系型数据库 MySQL Linux
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
3131 26
|
安全 Linux 网络安全
在Linux(CentOS和AWS)上安装更新的git2的方法并配置github-ssh
经过以上这些步骤,你现在就能在GitHub上顺利往返,如同海洋中的航海者自由驰骋。欢迎你加入码农的世界,享受这编程的乐趣吧!
537 10
|
Java
CentOS7.8配置Adoptium-Java17运行环境
本指南介绍如何设置清华镜像源并安装 Temurin-17-JRE 运行环境。首先,编辑 `/etc/yum.repos.d/adoptium.repo` 文件,配置清华镜像源。接着,使用 `yum install -y temurin-17-jre` 命令安装 Temurin-17-JRE,并通过 `java --version` 验证安装成功。相关配置和操作界面截图附后。
803 8
|
数据管理 数据库 数据安全/隐私保护
Django—同一项目不同app使用不同数据库
在Django项目中实现不同app使用不同数据库的配置,可以通过配置多数据库、创建数据库路由和配置路由来实现。通过这种方法,可以有效地将数据隔离到不同的数据库,提高数据管理的灵活性和系统的可扩展性。希望本文能为开发者在Django项目中使用多数据库提供清晰的指导。
460 4