1.新服入手
首先在阿里云或其他平台购买服务器并开放相关端口,安装debian系统镜像,使用ssh工具连接上服务器,这里推荐xshell,直接百度有免费版
(针对ubuntu,debian等)运行下面两句
apt-get update
apt-get upgrade
2.安装appache及依赖
apt-get install apache2
apt-get install libapache2-mod-wsgi-py3
3.新建用户和用户组
adduser username -g groupname
4.配置python虚拟环境
第一次需安装python的虚拟环境,之后就不需要了
注意:所有的过程需要最好在root环境中进行,不然有很多麻烦
pip freeze > requirments.txt #生成依赖文件
apt-get install python3-venv # 安装虚拟环境
python3 -m venv venv # 生成虚拟环境文件夹
source venv/bin/activate # 进入虚拟环境
pip install -r requirments.txt # 安装所有的依赖库
deactivate # 退出虚拟环境
5.写配置文件
需要写的文件有三个
第一个:项目文件夹里的wsgi文件
# flask.wsgi
import site
site.addsitedir('/home/xgkx/say/venv/lib/python3.7/site-packages')
# 虚拟环境里的site-packages文件夹路径
import sys
sys.path.insert(0, '/home/xgkx/say')
# 项目路径
from flasky import app as application
第二个:apache项目配置文件
除标注的地方需要更改其他的复制即可(端口要开放且未被占用)
# /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80> # 使用的端口
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerName api.hguxgkx.com
ServerAdmin xgkx@hgu.edu.cn
DocumentRoot /home/xgkx/xgkx # 这一行是项目文件夹
WSGIDaemonProcess xgkx user=xgkx group=xgkx processes=4 threads=4 # 用户名和组名,进程线程
WSGIScriptAlias / /home/xgkx/xgkx/xgkxflask.wsgi # wsgi文件的地址
LoadModule headers_module /usr/lib/apache2/modules/mod_headers.so
# Header always set Access-Control-Allow-Origin "*"
# Header always set Access-Control-Allow-Methods "GET,POST,PUT,DELETE,OPTIONS"
# Header always set Access-Control-Allow-Headers "authorization,content-type"
WSGIPassAuthorization On
<Directory /home/xgkx/xgkx> # 项目文件夹
WSGIProcessGroup xgkx # 组名
WSGIApplicationGroup %{GLOBAL}
Order allow,deny
Allow from all
Require all granted
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
第三个:端口监听文件
# /etc/apache2/ports.conf
# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf
Listen 80 # 监听的端口需要开放
Listen 5000
Listen 8080
Listen 8000
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
6.启动服务
sudo /etc/init.d/apache2 start # 启动
sudo /etc/init.d/apache2 restart # 重启
sudo /etc/init.d/apache2 stop # 停止
到这里,基本上配置完成,中途遇到一些小错误可以百度解决!