摘要:
这篇写的是如何部署python3+django3项目的后端服务(前后端分离和不分离的都适用),下一篇会写如何部署前后端分离的前端服务。
本文中提到的安装包的下载地址(永久有效):
链接:https://pan.baidu.com/s/1XnKfVS8JiRa-n6eWhYpM4w
提取码:k4n5
适用环境说明:
mod_wsgi-4.5.24:表示当前mod_wsgi版本是4.5.24
ap24vc14:表示apache版本是2.4,基于vc2014编译出来
cp36-cp36m:表示对应的python版本是3.6系列
win_amd64:表示对应的平台是windows 64bit
一、python 3.6安装
1.1、 python36 安装,没啥特殊的,一直next的。
安装完python 3.6后,在script目录下会同时存在pip pip3 pip3.6。pip与pip3都是按照包,如果电脑上只有一个版本的,则效果相同。如果电脑同时有多个版本的Python的时候,用pip3就可以自动区分用Python3来安装库。是为了避免和Python2发生冲突的。
二、 mod_wsgi安装(4.5.24 4.7.1)
2.1 mod_wsgi文件命名说明
https://www.lfd.uci.edu/~gohlke/pythonlibs/
下载 【mod_wsgi-4.7.1+ap24vc14-cp36-cp36m-win_amd64.whl 】
2.2 mod_wsgi安装
复制下载的mod_wsgi-4.7.1+ap24vc14-cp36-cp36m-win_amd64.whl到python 安装目录下的scripts目录中,执行pip install mod_wsgi-4.7.1+ap24vc14-cp36-cp36m-win_amd64.whl。
如果安装成功,Python 目录下的scripts目录下出现mod_wsgi-express.exe,在任意目录的cmd中执行mod_wsgi-express.exe module-config,将执行结果,即
LoadFile "D:/Python36/python36.dll"
LoadModule wsgi_module "D:/Python36/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp36-win_amd64.pyd"
WSGIPythonHome "D:/Python36"
复制到apache conf目录下的httpd.conf文件中。
三、 Apache14 安装
apache的下载路径,可从下面的链接中下载 https://www.apachelounge.com/download/VC15/,下载下来的是免安装的,修改配置文件即可直接启动服务。
2.1 Apache conf配置
LoadModule wsgi_module modules/mod_wsgi.so
#适用于apache配置为python2系列的运行环境,以插件so方式load可直接使用。在python3系列时,apache的管网各个版本中都找不到mod_wsgi.so,只能在https://www.lfd.uci.edu/~gohlke/pythonlibs/ 这个网站中下载whl文件,自行通过pip命令编译,集成到python的site-package包中,然后再到apache的配置文件指定python执行环境。
下面是我的线上项目的配置文件,做个参考:
#
# 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.
#
# 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'.
#
# 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.
#
# ServerRoot: The top of the directory tree under which the server's
# configuration, error, and log files are kept.
#
# 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.
#
Define SRVROOT "c:/Apache24"
ServerRoot "${SRVROOT}"
#
# 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.
#
#Listen 12.34.56.78:80
Listen 8056
#
# 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_fi