目录

  • 超详细Windows环境下使用Apache部署Django项目教程
    • 1.什么是Apache
    • 2.安装并配置Apache
      • 2.1 下载Apache
      • 2.2 解压Apache到文件夹
      • 2.3 配置conf文件
    • 3.安装mod_wsgi
      • 3.1 什么是mod_wsgi
      • 3.2 下载mod_wsgi
      • 3.3 安装mod_wsgi
    • 4.配置Django项目信息
      • 4.1 迁移Django项目
      • 4.2 在.conf文件中配置Django项目信息
      • 4.3 配置Django项目中的settings文件
    • 5.注册Apache服务并启动项目
    • 6.遇到的一些问题

超详细Windows环境下使用Apache部署Django项目教程

最近在做一个Django项目,学习了一下Django项目开发完成后在服务器上部署的方法,这里简单总结一下,作为一个参考回顾。
所使用的工具如下:
Django__3.0.7
python__3.8.1
Apache__2.4.46

1.什么是Apache

首先,了解一下什么是Apache。

Apache HTTP Server(简称Apache)是Apache软件基金会的一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由于其多平台和安全性被广泛使用,是最流行的Web服务器端软件之一。它快速、可靠并且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中。

Apache 起初由伊利诺伊大学香槟分校的国家超级电脑应用中心(NCSA)开发。此后,Apache 被开放源代码团体的成员不断的发展和加强。Apache 服务器拥有牢靠可信的美誉,已用在超过半数的因特网站中-特别是几乎所有最热门和访问量最大的网站。

开始,Apache 只是 Netscape 网页服务器(现在是 Sun ONE)之外的开放源代码选择。渐渐的,它开始在功能和速度超越其他的基于 Unix 的 HTTP 服务器。1996年4月以来,Apache 一直是 Internet 上最流行的 HTTP服务器: 1999年5月它在 57% 的网页服务器上运行;到了2005年7月这个比例上升到了69%。在2005年11月的时候达到接近70%的市占率,不过随着拥有大量域名数量的主机域名商转换为微软 IIS 平台,Apache 市占率近年来呈现些微下滑。而 Google 自己的网页服务器平台 GWS 推出后,加上 Lighttpd 这 个轻量化网页服务器软件使用的网站慢慢增加,反应在整体网页服务器市占率上,根据 netcraft 在2007年7月的最新统计数据,Apache的市占率已经降为52.65%,8月时又滑落到50.92%。尽管如此,它仍旧是现阶段因特网市场上,市占率最高的网页服务器软件。

作者宣称因为这个名字好记才在最初选择它,但是流传最广的解释是(也是最显而易见的):这个名字来自这么一个事实:当 Apache 在1995年初开发的时候,它是由当时最流行的 HTTP 服务器 NCSA HTTPd 1.3 的代码修改而成的,因此是“一个修补的(a patchy)”服务器。然而在服务器官方网站的 FAQ 中是这么解释的:“‘Apache’这个名字是为了纪念名为 Apache(印地语)的美洲印第安人土著的一支,众所周知他们拥有高超的作战策略和无穷的耐性”。无论如何,Apache 2.x 分支不包含任何 NCSA 的代码。

Apache 支持许多特性,大部分通过编译的模块实现。这些特性从服务器端的编程语言支持到身份认证方案。一些通用的语言接口支持Perl,Python, TCL, 和 PHP。流行的认证模块包括 mod_access, mod_auth 和 mod_digest。其他的例子有 SSL 和 TLS 支持 (mod_ssl), 代理服务器 (proxy) 模块,很有用的URL重写(由 mod_rewrite 实现),定制日志文件 (mod_log_config),以及过滤支持(mod_include 和 mod_ext_filter)。Apache 日志可以通过网页浏览器使用免费的脚本 AWStats 或 Visitors 来进行分析。

2.安装并配置Apache

2.1 下载Apache

进入 Apache官网下载页面下载windows版本的Apache,因为电脑是64位,所以这里我选择的是,如图所示红框内的版本。

2.2 解压Apache到文件夹

下载完成后,解压到你要部署服务器的文件夹。建议在你要部署服务器的电脑磁盘下建立一个英文目录,如F:\djangowebroot\Apache24,目录结构如下图所示。

2.3 配置conf文件

打开conf文件夹内的httpd.conf文件,对以下几个地方进行配置。

配置服务器所在的路径:

设置路由及端口,注意不要与其他服务端口冲突,默认端口为80,可能与windows自带的IIS服务冲突。这里我设置为8001端口,查看电脑的端口占用可以使用在CMD下使用netstat -ano命令查看。

设置服务名:

这时候Apache服务器就配置完成了。打开F:\djangowebroot\Apache24\bin\httpd.exe程序运行。
在浏览器键入127.0.0.1:8001,出现以下界面表示配置成功。

3.安装mod_wsgi

3.1 什么是mod_wsgi

官方介绍:

3.2 下载mod_wsgi

前往 链接:https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi 下载适合自己的mod_wsgi模块,比如我的python版本为3.8,电脑为64位,就选择cp38,win_amd64那一项。

3.3 安装mod_wsgi

CMD下使用pip install mod_wsgi-4.7.1-cp38-cp38-win_amd64.whl安装mod_wsgi,使用mod_wsgi-express module-config查看模块配置信息。

4.配置Django项目信息

4.1 迁移Django项目

将Django项目复制一份移动到Apache同级目录下,目录结构如下:

Django项目中的文件结构如下:

4.2 在.conf文件中配置Django项目信息

打开Apache的配置文件httpd.conf,在配置文件的末尾,配置你的Django项目信息如下。

4.3 配置Django项目中的settings文件

打开Django项目中的settings文件,将DEBUG设置为False,ALLOWED_HOSTS = [’*’],ALLOWED_HOSTS列表为了防止黑客入侵,只允许列表中的ip地址访问,这里我设置为通配符表示允许所有地址访问。

5.注册Apache服务并启动项目

在F:\djangowebroot\Apache24\bin目录下以管理员权限打开CMD,使用httpd.exe -k install -n “服务名” 注册服务,服务名可以自定义,当然你也可以使用httpd.exe -k uninstall -n “服务名” 来卸载你的服务,也可以不加-n “服务名” ,即只使用命令httpd.exe -k install来注册服务,注册成功后,在电脑右下角则会出现Apache的小图标,可以再次快速开启、停止、重启Apache服务。

6.遇到的一些问题

注意!如果出现以下报错信息,请将F:\djangowebroot\Apache24\bin路径添加到环境变量的PATH中,稳妥起见,用户环境变量和系统环境变量均添加,然后重启电脑尝试。

如果出现拒绝访问等报警信息,务必确认是否使用了管理员模式启动CMD。

当以上问题解决后,在浏览器输入http://127.0.0.1:8001/,即可访问你的Django项目啦~

另外,如果当你进入Django的admin后台,发现后台界面不能正常显示,这是因为设置了项目的settings文件后,在debug为True的模式下,django的静态文件都有内部自己管理,文件都在django内部,而部署线上之后无法正常显示后台admin的静态文件,就需要指定静态文件的目录。
方法:直接在settings.py中设置指定的静态文件目录,如图,在STATICFILES_DIRS后(或其他位置也不影响)增加STATIC_ROOT = os.path.join(BASE_DIR,‘static’)代码,然后保存。

在项目路径下打开CMD,执行

python manage.py collectstatic

运行这个命令之后,就会自动把后台CSS样式和其他相关文件资源收集到/static/目录下,刷新页面,后台admin页面就正常显示啦,这时,再去看static文件夹,会发现,多了一个样式文件夹admin,后台界面所用到的样式都在这里啦~

至此,整个项目基本就部署完成啦~

2021.6.24新增问题(更新)

时隔6个月,再次使用此教程部署Django项目,出现了一个问题,当所有操作完成后,访问项目地址,页面出现:
Internal Server Error

查找了试了很多方法都不行,最终查看错误日志如下:

[Thu Jun 24 10:18:42.975414 2021] [mpm_winnt:notice] [pid 7548:tid 1424] AH00354: Child: Starting 64 worker threads.
[Thu Jun 24 10:18:47.522265 2021] [wsgi:error] [pid 7548:tid 1952] [client 127.0.0.1:7157] mod_wsgi (pid=7548): Failed to exec Python script file 'D:/Gongyisys_product_env_wwwrooot/Apache24/htdocs/Django_Proj_GongyiSys/Django_Proj_GongyiSys/wsgi.py'.
[Thu Jun 24 10:18:47.522265 2021] [wsgi:error] [pid 7548:tid 1952] [client 127.0.0.1:7157] mod_wsgi (pid=7548): Exception occurred processing WSGI script 'D:/Gongyisys_product_env_wwwrooot/Apache24/htdocs/Django_Proj_GongyiSys/Django_Proj_GongyiSys/wsgi.py'.
[Thu Jun 24 10:18:47.526254 2021] [wsgi:error] [pid 7548:tid 1952] [client 127.0.0.1:7157] Traceback (most recent call last):\r
[Thu Jun 24 10:18:47.526254 2021] [wsgi:error] [pid 7548:tid 1952] [client 127.0.0.1:7157]   File "D:/Gongyisys_product_env_wwwrooot/Apache24/htdocs/Django_Proj_GongyiSys/Django_Proj_GongyiSys/wsgi.py", line 12, in <module>\r
[Thu Jun 24 10:18:47.526254 2021] [wsgi:error] [pid 7548:tid 1952] [client 127.0.0.1:7157]     from django.core.wsgi import get_wsgi_application\r
[Thu Jun 24 10:18:47.526254 2021] [wsgi:error] [pid 7548:tid 1952] [client 127.0.0.1:7157] ModuleNotFoundError: No module named 'django'\r

最后一行,ModuleNotFoundError: No module named 'django'\r让我意识到,前不久把电脑的所有全局环境中的python库都卸载了,只保留了项目虚拟环境的,于是乎,我又在全局环境下安装了Django库,果不其然,成功了!!!

另外附上conf配置文件的信息,方便参考:

# Apache Hause .conf file for TLS/1.3 supported versions
#
#This is the main Apache HTTP server configuration file.  It contains the
#configuration directives that give the server its instructions.
#See <URL:http://httpd.apache.org/docs/2.4/> for detailed information.
#In particular, see
#<URL:http://httpd.apache.org/docs/2.4/mod/directives.html>
#for a discussion of each configuration directive.
#这是主apachehttp服务器配置文件。它包含
#向服务器提供其指令的配置指令。
#请参见<URL:http://httpd.apache.org/docs/2.4/>有关详细信息。
#具体见
#<网址:http://httpd.apache.org/docs/2.4/mod/directives.html>
#有关每个配置指令的讨论。
#
#Do NOT simply read the instructions in here without understanding
#what they do.  They're here only as hints or reminders.  If you are unsure
#consult the online docs. You have been warned.
#不要不理解地简单地阅读这里的说明
#他们做什么。它们只是作为提示或提醒。如果你不确定
#查阅在线文档。你已经被警告了。
#
#Configuration and logfile names: If the filenames you specify for many
#of the server's control files begin with "/" (or "drive:/" for Win32), the
#server will use that explicit path.  If the filenames do *not* begin
#with "/", the value of ServerRoot is prepended -- so "logs/access_log"
#with ServerRoot set to "/usr/local/apache2" will be interpreted by the
#server as "/usr/local/apache2/logs/access_log", whereas "/logs/access_log"
#will be interpreted as '/logs/access_log'.
#配置和日志文件名:如果为多个
#在服务器的控制文件中,以“/”开头(对于Win32为“drive:/”)
#服务器将使用该显式路径。如果文件名不以*开头
#使用“/”时,ServerRoot的值被加上前缀--“logs/access\u log”
#将ServerRoot设置为“/usr/local/apache2”将由
#服务器为“/usr/local/apache2/logs/access\u log”,而“/logs/access\u log”
#将被解释为“/logs/access\u log”。
#
#NOTE: Where filenames are specified, you must use forward slashes
#instead of backslashes (e.g., "c:/apache" instead of "c:\apache").
#If a drive letter is omitted, the drive on which httpd.exe is located
#will be used by default.  It is recommended that you always supply
#an explicit drive letter in absolute paths to avoid confusion.
#注意:如果指定了文件名,则必须使用正斜杠
#而不是反斜杠(例如,“c:/apache”而不是“c:\apache”)。
#如果省略驱动器号,则为httpd.exe所在的驱动器
#默认情况下将使用。建议您始终提供
#绝对路径中的显式驱动器号以避免混淆。
#
#ServerRoot: The top of the directory tree under which the server's
#configuration, error, and log files are kept.
#ServerRoot:目录树的顶部,在该目录树下服务器的
#保留配置、错误和日志文件。#
#
#Do not add a slash at the end of the directory path.  If you point
#ServerRoot at a non-local disk, be sure to specify a local disk on the
#Mutex directive, if file-based mutexes are used.  If you wish to share the
#same ServerRoot for multiple httpd daemons, you will need to change at
#least PidFile.
#不要在目录路径的末尾添加斜杠。如果你指的是
#ServerRoot在非本地磁盘上,请确保在
#Mutex指令,如果使用基于文件的Mutex。如果你想分享
#对于多个httpd守护进程,需要在
#最少的文件。#
#Define SRVROOT "D:/Gongyisys_product_env_wwwrooot/Apache24"
ServerRoot "${SRVROOT}"#
# Uncomment below line to enable TLS/1.3 in server.
# (Requires OpenSSL 1.1.1 and up)
# 取消第行下面的注释以在服务器中启用TLS/1.3。
# (需要OpenSSL 1.1.1及更高版本)
#
Define ENABLE_TLS13 "Yes"#
# Mutex: Allows you to set the mutex mechanism and mutex file directory
# for individual mutexes, or change the global defaults
# 互斥:允许您设置互斥机制和互斥文件目录
# 或更改全局默认值
#
# Uncomment and change the directory if mutexes are file-based and the default
# mutex file directory is not on a local disk or is not appropriate for some
# other reason.
#
# Mutex default:logs#
# Listen: Allows you to bind Apache to specific IP addresses and/or
# ports, instead of the default. See also the <VirtualHost>
# directive.
#
# Change this to Listen on specific IP addresses as shown below to
# prevent Apache from glomming onto all bound IP addresses.
# 将此更改为侦听特定IP地址,如下所示
# 防止Apache对所有绑定的IP地址进行加密。#
#
#Listen 12.34.56.78:80
Listen 10.64.35.70:8002#
# Dynamic Shared Object (DSO) Support
#
# To be able to use the functionality of a module which was built as a DSO you
# have to place corresponding `LoadModule' lines at this location so the
# directives contained in it are actually available _before_ they are used.
# Statically compiled modules (those listed by `httpd -l') do not need
# to be loaded here.
#
# Example:
# LoadModule foo_module modules/mod_foo.so
#
#LoadModule access_compat_module modules/mod_access_compat.so
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule allowmethods_module modules/mod_allowmethods.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modules/mod_auth_basic.so
#LoadModule auth_digest_module modules/mod_auth_digest.so
#LoadModule auth_form_module modules/mod_auth_form.so
#LoadModule authn_anon_module modules/mod_authn_anon.so
LoadModule authn_core_module modules/mod_authn_core.so
#LoadModule authn_dbd_module modules/mod_authn_dbd.so
#LoadModule authn_dbm_module modules/mod_authn_dbm.so
LoadModule authn_file_module modules/mod_authn_file.so
#LoadModule authn_socache_module modules/mod_authn_socache.so
#LoadModule authnz_fcgi_module modules/mod_authnz_fcgi.so
#LoadModule authnz_ldap_module modules/mod_authnz_ldap.so
LoadModule authz_core_module modules/mod_authz_core.so
#LoadModule authz_dbd_module modules/mod_authz_dbd.so
#LoadModule authz_dbm_module modules/mod_authz_dbm.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
#LoadModule authz_owner_module modules/mod_authz_owner.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
#LoadModule buffer_module modules/mod_buffer.so
#LoadModule cache_module modules/mod_cache.so
#LoadModule cache_disk_module modules/mod_cache_disk.so
#LoadModule cache_socache_module modules/mod_cache_socache.so
#LoadModule cern_meta_module modules/mod_cern_meta.so
LoadModule cgi_module modules/mod_cgi.so
#LoadModule charset_lite_module modules/mod_charset_lite.so
#LoadModule data_module modules/mod_data.so
#LoadModule dav_module modules/mod_dav.so
#LoadModule dav_fs_module modules/mod_dav_fs.so
#LoadModule dav_lock_module modules/mod_dav_lock.so
#LoadModule dbd_module modules/mod_dbd.so
#LoadModule deflate_module modules/mod_deflate.so
LoadModule dir_module modules/mod_dir.so
#LoadModule dumpio_module modules/mod_dumpio.so
LoadModule env_module modules/mod_env.so
#LoadModule expires_module modules/mod_expires.so
#LoadModule ext_filter_module modules/mod_ext_filter.so
#LoadModule file_cache_module modules/mod_file_cache.so
#LoadModule filter_module modules/mod_filter.so
#LoadModule headers_module modules/mod_headers.so
#LoadModule heartbeat_module modules/mod_heartbeat.so
#LoadModule heartmonitor_module modules/mod_heartmonitor.so
LoadModule http2_module modules/mod_http2.so
#LoadModule ident_module modules/mod_ident.so
#LoadModule imagemap_module modules/mod_imagemap.so
LoadModule include_module modules/mod_include.so
LoadModule info_module modules/mod_info.so
LoadModule isapi_module modules/mod_isapi.so
#LoadModule lbmethod_bybusyness_module modules/mod_lbmethod_bybusyness.so
#LoadModule lbmethod_byrequests_module modules/mod_lbmethod_byrequests.so
#LoadModule lbmethod_bytraffic_module modules/mod_lbmethod_bytraffic.so
#LoadModule lbmethod_heartbeat_module modules/mod_lbmethod_heartbeat.so
#LoadModule ldap_module modules/mod_ldap.so
#LoadModule logio_module modules/mod_logio.so
LoadModule log_config_module modules/mod_log_config.so
#LoadModule log_debug_module modules/mod_log_debug.so
#LoadModule log_forensic_module modules/mod_log_forensic.so
#LoadModule lua_module modules/mod_lua.so
#LoadModule macro_module modules/mod_macro.so
#LoadModule md_module modules/mod_md.so
LoadModule mime_module modules/mod_mime.so
#LoadModule mime_magic_module modules/mod_mime_magic.so
LoadModule negotiation_module modules/mod_negotiation.so
#LoadModule proxy_module modules/mod_proxy.so
#LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
#LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
#LoadModule proxy_connect_module modules/mod_proxy_connect.so
#LoadModule proxy_express_module modules/mod_proxy_express.so
#LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so
#LoadModule proxy_ftp_module modules/mod_proxy_ftp.so
#LoadModule proxy_html_module modules/mod_proxy_html.so
#LoadModule proxy_http_module modules/mod_proxy_http.so
#LoadModule proxy_http2_module modules/mod_proxy_http2.so
#LoadModule proxy_scgi_module modules/mod_proxy_scgi.so
#LoadModule proxy_uwsgi_module modules/mod_proxy_uwsgi.so
#LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
#LoadModule ratelimit_module modules/mod_ratelimit.so
#LoadModule reflector_module modules/mod_reflector.so
#LoadModule remoteip_module modules/mod_remoteip.so
#LoadModule request_module modules/mod_request.so
#LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule rewrite_module modules/mod_rewrite.so
#LoadModule sed_module modules/mod_sed.so
#LoadModule session_module modules/mod_session.so
#LoadModule session_cookie_module modules/mod_session_cookie.so
#LoadModule session_crypto_module modules/mod_session_crypto.so
#LoadModule session_dbd_module modules/mod_session_dbd.so
LoadModule setenvif_module modules/mod_setenvif.so
#LoadModule slotmem_plain_module modules/mod_slotmem_plain.so
#LoadModule slotmem_shm_module modules/mod_slotmem_shm.so
#LoadModule socache_dbm_module modules/mod_socache_dbm.so
#LoadModule socache_memcache_module modules/mod_socache_memcache.so
#LoadModule socache_redis_module modules/mod_socache_redis.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
#LoadModule speling_module modules/mod_speling.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule status_module modules/mod_status.so
#LoadModule substitute_module modules/mod_substitute.so
#LoadModule unique_id_module modules/mod_unique_id.so
#LoadModule userdir_module modules/mod_userdir.so
#LoadModule usertrack_module modules/mod_usertrack.so
#LoadModule version_module modules/mod_version.so
#LoadModule vhost_alias_module modules/mod_vhost_alias.so
#LoadModule watchdog_module modules/mod_watchdog.so
#LoadModule xml2enc_module modules/mod_xml2enc.so<IfModule unixd_module>
#
# If you wish httpd to run as a different user or group, you must run
# httpd as root initially and it will switch.
# User/Group: The name (or #number) of the user/group to run httpd as.
# It is usually good practice to create a dedicated user and group for
# running httpd, as with most system services.
# 如果希望httpd作为不同的用户或组运行,则必须运行
# httpd最初是根,它将切换。
# User/Group:要作为运行httpd的用户/组的名称(或#编号)。
# 通常,创建专用用户和组是一个很好的实践
# 运行httpd,与大多数系统服务一样。
#
User daemon
Group daemon</IfModule># 'Main' server configuration
#
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
# <VirtualHost> definition.  These values also provide defaults for
# any <VirtualHost> containers you may define later in the file.
#
# All of these directives may appear inside <VirtualHost> containers,
# in which case these default settings will be overridden for the
# virtual host being defined.
##
# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. admin@your-domain.com
#
ServerAdmin admin@example.com#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
# If your host doesn't have a registered DNS name, enter its IP address here.
# ServerName提供服务器用来标识自身的名称和端口。
# 这通常可以自动确定,但我们建议您指定
# 这是为了防止启动过程中出现问题。
# 如果您的主机没有注册的DNS名称,请在此处输入其IP地址。#
#
ServerName 10.64.35.70:8002#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
# 拒绝访问服务器的整个文件系统。你必须
# 显式允许访问其他目录中的web内容目录
# <Directory>块。
#
<Directory />AllowOverride AllRequire all denied
</Directory>#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
# 请注意,从这一点开始,您必须特别允许
# 要启用的特定功能-因此如果某些功能无法正常工作
# 您可能会期望,确保您已经专门启用了它#
# 在下面。
##
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
# DocumentRoot:您将从中服务的目录
# 文件。默认情况下,所有请求都来自此目录,但是
# 符号链接和别名可用于指向其他位置。
#
DocumentRoot "${SRVROOT}/htdocs"
<Directory "${SRVROOT}/htdocs">## Possible values for the Options directive are "None", "All",# or any combination of:#   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews## Note that "MultiViews" must be named *explicitly* --- "Options All"# doesn't give it to you.## The Options directive is both complicated and important.  Please see# http://httpd.apache.org/docs/2.4/mod/core.html#options# for more information.# Options指令的可能值为“None”、“All”,# 或以下任何组合:# 索引包括以下符号链接符号链接所有者匹配执行cgi多视图# 请注意,“多视图”必须显式命名为**--“Options All”# 不是给你的。# 期权指令既复杂又重要。请看# http://httpd.apache.org/docs/2.4/mod/core.html#options# 更多信息。#Options Indexes FollowSymLinks## AllowOverride controls what directives may be placed in .htaccess files.# It can be "All", "None", or any combination of the keywords:#   Options FileInfo AuthConfig Limit#AllowOverride All## Controls who can get stuff from this server.#Require all granted
</Directory>#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#
<IfModule dir_module>DirectoryIndex index.html
</IfModule>#
# The following lines prevent .htaccess and .htpasswd files from being
# viewed by Web clients.
#
<Files ".ht*">Require all denied
</Files>#
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a <VirtualHost>
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a <VirtualHost>
# container, that host's errors will be logged there and not here.
#
ErrorLog "logs/error.log"#
# LogLevel: Control the number of messages logged to the error_log.
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
#
LogLevel warn<IfModule log_config_module>## The following directives define some format nicknames for use with# a CustomLog directive (see below).#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combinedLogFormat "%h %l %u %t \"%r\" %>s %b" common<IfModule logio_module># You need to enable mod_logio.c to use %I and %OLogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio</IfModule>## The location and format of the access logfile (Common Logfile Format).# If you do not define any access logfiles within a <VirtualHost># container, they will be logged here.  Contrariwise, if you *do*# define per-<VirtualHost> access logfiles, transactions will be# logged therein and *not* in this file.#CustomLog "logs/access.log" common## If you prefer a logfile with access, agent, and referer information# (Combined Logfile Format) you can use the following directive.##CustomLog "logs/access.log" combined
</IfModule><IfModule alias_module>## Redirect: Allows you to tell clients about documents that used to # exist in your server's namespace, but do not anymore. The client # will make a new request for the document at its new location.# Example:# Redirect permanent /foo http://www.example.com/bar## Alias: Maps web paths into filesystem paths and is used to# access content that does not live under the DocumentRoot.# Example:# Alias /webpath /full/filesystem/path## If you include a trailing / on /webpath then the server will# require it to be present in the URL.  You will also likely# need to provide a <Directory> section to allow access to# the filesystem path.## ScriptAlias: This controls which directories contain server scripts. # ScriptAliases are essentially the same as Aliases, except that# documents in the target directory are treated as applications and# run by the server when requested rather than as documents sent to the# client.  The same rules about trailing "/" apply to ScriptAlias# directives as to Alias.#ScriptAlias /cgi-bin/ "${SRVROOT}/cgi-bin/"</IfModule><IfModule cgid_module>## ScriptSock: On threaded servers, designate the path to the UNIX# socket used to communicate with the CGI daemon of mod_cgid.##Scriptsock logs/cgisock
</IfModule>#
# "${SRVROOT}/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#
<Directory "${SRVROOT}/cgi-bin">AllowOverride NoneOptions NoneRequire all granted
</Directory><IfModule mime_module>## TypesConfig points to the file containing the list of mappings from# filename extension to MIME-type.#TypesConfig conf/mime.types## AddType allows you to add to or override the MIME configuration# file specified in TypesConfig for specific file types.##AddType application/x-gzip .tgz## AddEncoding allows you to have certain browsers uncompress# information on the fly. Note: Not all browsers support this.##AddEncoding x-compress .Z#AddEncoding x-gzip .gz .tgz## If the AddEncoding directives above are commented-out, then you# probably should define those extensions to indicate media types:#AddType application/x-compress .ZAddType application/x-gzip .gz .tgz## AddHandler allows you to map certain file extensions to "handlers":# actions unrelated to filetype. These can be either built into the server# or added with the Action directive (see below)## To use CGI scripts outside of ScriptAliased directories:# (You will also need to add "ExecCGI" to the "Options" directive.)##AddHandler cgi-script .cgi .pl# For type maps (negotiated resources):#AddHandler type-map var## Filters allow you to process content before it is sent to the client.## To parse .shtml files for server-side includes (SSI):# (You will also need to add "Includes" to the "Options" directive.)##AddType text/html .shtml#AddOutputFilter INCLUDES .shtml
</IfModule>#
# The mod_mime_magic module allows the server to use various hints from the
# contents of the file itself to determine its type.  The MIMEMagicFile
# directive tells the module where the hint definitions are located.
#
#MIMEMagicFile conf/magic#
# Customizable error responses come in three flavors:
# 1) plain text 2) local redirects 3) external redirects
#
# Some examples:
#ErrorDocument 500 "The server made a boo boo."
#ErrorDocument 404 /missing.html
#ErrorDocument 404 "/cgi-bin/missing_handler.pl"
#ErrorDocument 402 http://www.example.com/subscription_info.html
##
# MaxRanges: Maximum number of Ranges in a request before
# returning the entire resource, or one of the special
# values 'default', 'none' or 'unlimited'.
# Default setting is to accept 200 Ranges.
#MaxRanges unlimited#
# EnableMMAP and EnableSendfile: On systems that support it,
# memory-mapping or the sendfile syscall may be used to deliver
# files.  This usually improves server performance, but must
# be turned off when serving from networked-mounted
# filesystems or if support for these functions is otherwise
# broken on your system.
# Defaults: EnableMMAP On, EnableSendfile Off
#
#EnableMMAP off
#EnableSendfile on#AcceptFilter http none
#AcceptFilter https none# Supplemental configuration
#
# The configuration files in the conf/extra/ directory can be
# included to add extra features or to modify the default configuration of
# the server, or you may simply copy their contents here and change as
# necessary.# Server-pool management (MPM specific)
#Include conf/extra/httpd-mpm.conf# Multi-language error messages
#Include conf/extra/httpd-multilang-errordoc.conf# Fancy directory listings
Include conf/extra/httpd-autoindex.conf# Language settings
#Include conf/extra/httpd-languages.conf# User home directories
#Include conf/extra/httpd-userdir.conf# Real-time info on requests and configuration
Include conf/extra/httpd-info.conf# Virtual hosts
# Include conf/extra/httpd-vhosts.conf# Local access to the Apache HTTP Server Manual
#Include conf/extra/httpd-manual.conf# Distributed authoring and versioning (WebDAV)
#Include conf/extra/httpd-dav.conf# Various default settings
#Include conf/extra/httpd-default.conf# Configure mod_proxy_html to understand HTML4/XHTML1
<IfModule proxy_html_module>
Include conf/extra/httpd-proxy-html.conf
</IfModule># Secure (SSL/TLS) connections
# Note: The following must must be present to support
#       starting without SSL on platforms with no /dev/random equivalent
#       but a statically compiled-in mod_ssl.
#
<IfModule ssl_module>
#Include conf/extra/httpd-ssl.conf
Include conf/extra/httpd-ahssl.conf
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>
<IfModule http2_module>ProtocolsHonorOrder OnProtocols h2 h2c http/1.1
</IfModule># PHP FastCGI
#
# Edit conf/extra/httpd-fcgid.conf to match your php location
# and uncomment the Include line below.
#
#Include conf/extra/httpd-fcgid.conf<IfModule lua_module>AddHandler lua-script .lua
</IfModule>#配置python-Django项目
#1.添加mod_wsgi.so模块,这三行是上面命令行中显示出来的
LoadFile "c:/users/admin/appdata/local/programs/python/python38/python38.dll"
LoadModule wsgi_module "c:/users/admin/appdata/local/programs/python/python38/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd"
WSGIPythonHome "c:/users/admin/appdata/local/programs/python/python38"
WSGIApplicationGroup %{GLOBAL}
#
#2.指定项目的wsgi.py配置文件路径,这个py文件是在你的Django项目中
WSGIScriptAlias / d:/Gongyisys_product_env_wwwrooot/Apache24/htdocs/Django_Proj_GongyiSys/Django_Proj_GongyiSys/wsgi.py
#
#3.指定项目路径
WSGIPythonPath d:/Gongyisys_product_env_wwwrooot/Apache24/htdocs/Django_Proj_GongyiSys
#
<Directory d:/Gongyisys_product_env_wwwrooot/Apache24/htdocs/Django_Proj_GongyiSys/Django_Proj_GongyiSys>
<Files wsgi.py>Require all granted
</Files>
</Directory>
#
#4.配置静态文件路径
Alias /static d:/Gongyisys_product_env_wwwrooot/Apache24/htdocs/Django_Proj_GongyiSys/static
<Directory d:/Gongyisys_product_env_wwwrooot/Apache24/htdocs/Django_Proj_GongyiSys/static>Options FollowSymlinksAllowOverride noneRequire all granted
</Directory>

2021.6.24新增问题(更新)
项目部署成功后,进入Django的admin后台,发现后台界面不能正常显示,这是因为设置了项目的settings文件后,在debug为True的模式下,django的静态文件都有内部自己管理,文件都在django内部,而部署线上之后无法正常显示后台admin的静态文件,就需要指定静态文件的目录。
方法:直接在settings.py中设置指定的静态文件目录,如图,在STATICFILES_DIRS后(或其他位置也不影响)增加以下代码:

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),
)
STATIC_ROOT = os.path.join(BASE_DIR, '/static/')

之前的时候STATIC_ROOT = os.path.join(BASE_DIR, '/static/')这句用的STATIC_ROOT = os.path.join(BASE_DIR, 'static'),结果出现报错:

(venv) D:\Gongyisys_product_env_wwwrooot\Apache24\htdocs\Django_Proj_GongyiSys>python manage.py collectstatic
SystemCheckError: System check identified some issues:ERRORS:
?: (staticfiles.E002) The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting.

当改为STATIC_ROOT = os.path.join(BASE_DIR, '/static/')后,再运行python manage.py collectstatic就成功了!

超详细Windows环境下使用Apache部署Django项目教程相关推荐

  1. Android 开发之Windows环境下Android Studio安装和使用教程(图文详细步骤)

    鉴于谷歌最新推出的Android Studio备受开发者的推崇,所以也跟着体验一下. 一.介绍Android Studio  Android Studio 是一个Android开发环境,基于Intel ...

  2. centos7根据端口查进程_记录一次CentOs7下Nginx+WSGI部署Django项目(超详细)

    记录一次Django部署的文章,不是很熟悉Linux系统,踩了不少坑,本篇文章相当于是一个总结,我会在本文中详细介绍,部署单个Django项目和多个Django的方法,如读者有更好的方法,欢迎留言一起 ...

  3. Android笔记——Windows环境下Android Studio v1.0安装教程

    本文主要讲解Windows环境下Android Studio的安装教程,Mac的Android Studio安装与此类似不在赘述,另外友情提示Windows下的SDK与Mac的SDK是通用的,可以直接 ...

  4. Windows环境下yolov3+darknet批量处理图片完整教程

    windows环境下,需要用到VS 1.下载Darknet和yolov3 下载Darknet 如果有git的话 git clone https://github.com/AlexeyAB/darkne ...

  5. 在Linux环境下使用Apache部署ASP.NET Core

    在前几篇文章中我们一起探讨了如何在Linux环境中安装ASP.NET Core运行时环境及将ASP.NET Core项目部署在Jexus中,这篇文章中我们将探讨如何将ASP.NET Core部署于Ap ...

  6. Geoscene Enterprise2.1 windows环境下的安装部署

    1 前言 呕心沥血肝两天,一起学习GeoScene Enterprise在window环境下的安装与部署,同时希望能够帮助到大家,如果有不正确的地方希望各位大佬指出. 2 准备阶段 本次安装选择了Ja ...

  7. 【超详细附参考】阿里云部署spring项目基本流程详解及踩坑经验

    文章目录 环境及前期准备 基本流程 购买云服务器 连接云服务器 工具准备 Xshell连接: Xftp连接: 环境配置 JDK安装 Tomcat安装 Mysql安装 Maven安装 项目打包部署 数据 ...

  8. Python进阶(三十七)Windows7使用nginx+apache部署django项目

    文章目录 一.前言 二.nginx安装 三.初识fastcgi 一.前言 Django的部署可以有很多方式,采用nginx+uwsgi的方式是其中比较常见的一种方式. 目前国内各大门户网站已经部署了n ...

  9. linux nginx django,如何在Linux下使用Nginx部署Django项目

    Django项目使用nginx + uwsgi进行部署,是比较常见的方式. 系统环境 这里列出自己的,就算不一样也不用太担心. Liunx系统版本:CentOS Linux release 7.8.2 ...

最新文章

  1. Java+MyEclipse+Tomcat (一)配置过程及jsp网站开发入门
  2. 技术实践 | 用 NetworkX + Gephi + Nebula Graph 分析权力的游戏人物关系(上篇)
  3. springMVC一些实践总结
  4. vantUI 弹出层(轻提示)案例 - 踩坑篇
  5. 5G(5)---7个方面读懂5G标准
  6. Vue中mounted和created的区别
  7. 说唱天王 Eminem 自传《The Way I am》1
  8. 《Android移动应用基础教程》之川菜菜谱
  9. QGIS3.20 制作栅格动画
  10. html5快捷键自动对齐,【答疑】pr自动对齐的快捷键是什么 - 视频教程线上学
  11. PHP开源公司订餐系统Food Online System Version1.6
  12. 年薪百万,到手能拿到多少钱?
  13. java我的世界非正常退出_我的世界非正常退出怎么办???
  14. win11蓝牙无法使用 Windows11蓝牙无法使用的解决方法
  15. 输入一个字符串,将其逆序输出。
  16. mac允许安装任何来源的软件,命令,(记录下)
  17. P2746 P2812 [USACO5.3]校园网Network of Schools[SCC缩点]
  18. 使用爱码哥之后的心得
  19. 3000左右元笔记本电脑推荐 2022 3000元笔记本电脑性价比排行
  20. MacOS APP 打包成 dmg - 使用 create-dmg

热门文章

  1. OpenStack(一)——OpenStack的相关概念
  2. linux第三方软件安装目录
  3. 从Firechat软件追踪移动自组网技术发展
  4. wireless-ac 9462驱动下载_驱动精灵 v9.61去广告清爽版绿单文件版 下载更新备份电脑驱动工具...
  5. 长了一副可以当明星的脸,身材却惨不忍睹是种怎样的体验?
  6. 列表标签,自定义列表,img标签,超链接a标签
  7. Qt/C++ 模仿 酷狗音乐播放器Qt/C++ 模仿 酷狗音乐播放器
  8. 关于虚拟机纯命令行centos解决web上网认证的解决方案
  9. 2015移动开发之iOS回顾
  10. Win32 下安装配置OpenLdap