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

目录

  • 超详细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位,所以这里我选择的是,如图所示红框内的版本。
超详细Windows环境下使用Apache部署Django项目教程_第1张图片

2.2 解压Apache到文件夹

下载完成后,解压到你要部署服务器的文件夹。建议在你要部署服务器的电脑磁盘下建立一个英文目录,如F:\djangowebroot\Apache24,目录结构如下图所示。
超详细Windows环境下使用Apache部署Django项目教程_第2张图片

2.3 配置conf文件

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

配置服务器所在的路径:
超详细Windows环境下使用Apache部署Django项目教程_第3张图片
设置路由及端口,注意不要与其他服务端口冲突,默认端口为80,可能与windows自带的IIS服务冲突。这里我设置为8001端口,查看电脑的端口占用可以使用在CMD下使用netstat -ano命令查看。
超详细Windows环境下使用Apache部署Django项目教程_第4张图片
设置服务名:
超详细Windows环境下使用Apache部署Django项目教程_第5张图片
这时候Apache服务器就配置完成了。打开F:\djangowebroot\Apache24\bin\httpd.exe程序运行。
在浏览器键入127.0.0.1:8001,出现以下界面表示配置成功。
超详细Windows环境下使用Apache部署Django项目教程_第6张图片

3.安装mod_wsgi

3.1 什么是mod_wsgi

官方介绍:
超详细Windows环境下使用Apache部署Django项目教程_第7张图片

3.2 下载mod_wsgi

前往 链接:https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi 下载适合自己的mod_wsgi模块,比如我的python版本为3.8,电脑为64位,就选择cp38,win_amd64那一项。
超详细Windows环境下使用Apache部署Django项目教程_第8张图片

3.3 安装mod_wsgi

CMD下使用pip install mod_wsgi-4.7.1-cp38-cp38-win_amd64.whl安装mod_wsgi,使用mod_wsgi-express module-config查看模块配置信息。
超详细Windows环境下使用Apache部署Django项目教程_第9张图片

4.配置Django项目信息

4.1 迁移Django项目

将Django项目复制一份移动到Apache同级目录下,目录结构如下:
超详细Windows环境下使用Apache部署Django项目教程_第10张图片
Django项目中的文件结构如下:
超详细Windows环境下使用Apache部署Django项目教程_第11张图片

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

打开Apache的配置文件httpd.conf,在配置文件的末尾,配置你的Django项目信息如下。
超详细Windows环境下使用Apache部署Django项目教程_第12张图片

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服务。
超详细Windows环境下使用Apache部署Django项目教程_第13张图片

6.遇到的一些问题

注意!如果出现以下报错信息,请将F:\djangowebroot\Apache24\bin路径添加到环境变量的PATH中,稳妥起见,用户环境变量和系统环境变量均添加,然后重启电脑尝试。
超详细Windows环境下使用Apache部署Django项目教程_第14张图片
如果出现拒绝访问等报警信息,务必确认是否使用了管理员模式启动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’)代码,然后保存。
超详细Windows环境下使用Apache部署Django项目教程_第15张图片
在项目路径下打开CMD,执行

python manage.py collectstatic

运行这个命令之后,就会自动把后台CSS样式和其他相关文件资源收集到/static/目录下,刷新页面,后台admin页面就正常显示啦,这时,再去看static文件夹,会发现,多了一个样式文件夹admin,后台界面所用到的样式都在这里啦~
超详细Windows环境下使用Apache部署Django项目教程_第16张图片
超详细Windows环境下使用Apache部署Django项目教程_第17张图片

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

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 \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  for detailed information.
#In particular, see
#
#for a discussion of each configuration directive.
#这是主apachehttp服务器配置文件。它包含
#向服务器提供其指令的配置指令。
#请参见有关详细信息。
#具体见
#<网址: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 
# 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


#
# 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



# '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
#  definition.  These values also provide defaults for
# any  containers you may define later in the file.
#
# All of these directives may appear inside  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. [email protected]
#
ServerAdmin [email protected]

#
# 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
#  blocks below.
# 拒绝访问服务器的整个文件系统。你必须
# 显式允许访问其他目录中的web内容目录
# 块。
#

    AllowOverride All
    Require all denied


#
# 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"

    #
    # 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


#
# DirectoryIndex: sets the file that Apache will serve if a directory
# is requested.
#

    DirectoryIndex index.html


#
# The following lines prevent .htaccess and .htpasswd files from being 
# viewed by Web clients. 
#

    Require all denied


#
# ErrorLog: The location of the error log file.
# If you do not specify an ErrorLog directive within a 
# container, error messages relating to that virtual host will be
# logged here.  If you *do* define an error logfile for a 
# 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


    #
    # 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\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    
      # You need to enable mod_logio.c to use %I and %O
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    

    #
    # The location and format of the access logfile (Common Logfile Format).
    # If you do not define any access logfiles within a 
    # container, they will be logged here.  Contrariwise, if you *do*
    # define per- 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



    #
    # 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  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/"




    #
    # ScriptSock: On threaded servers, designate the path to the UNIX
    # socket used to communicate with the CGI daemon of mod_cgid.
    #
    #Scriptsock logs/cgisock


#
# "${SRVROOT}/cgi-bin" should be changed to whatever your ScriptAliased
# CGI directory exists, if you have that configured.
#

    AllowOverride None
    Options None
    Require all granted



    #
    # 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 .Z
    AddType 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


#
# 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

Include conf/extra/httpd-proxy-html.conf


# 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.
#

#Include conf/extra/httpd-ssl.conf
Include conf/extra/httpd-ahssl.conf
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin


    ProtocolsHonorOrder On
    Protocols h2 h2c http/1.1


# 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


  AddHandler lua-script .lua




#配置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
#


    Require all granted


#
#4.配置静态文件路径
Alias /static d:/Gongyisys_product_env_wwwrooot/Apache24/htdocs/Django_Proj_GongyiSys/static

  Options FollowSymlinks
  AllowOverride none
  Require all granted




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就成功了!

你可能感兴趣的:(服务器项目部署,环境配置,Python,python,apache,pycharm,mysql,django)