更新:之前使用腾讯云的Ubuntu16.04+自带Python3.5.2,后来服务器过期之后就放弃了,现在换用阿里云Ubuntu18.04+自带Python3.6.9,但配置wsgi时使用venv虚拟环境,应该自装其他版本Python也没有关系。
阿里云ubuntu18.04 + apache2.4 + django2.1.7 + python3.6.9
在使用服务器最开始,先允许root远程登录
sudo passwd root
sudo /etc/init.d/ssh start
sudo vim /etc/ssh/sshd_config
找到 #PermitRootLogin prohibit-password
输入i
修改为 PermitRootLogin yes
先按Esc
再输入 :wq
退出service ssh restart
sudo apt-get install python3-venv
pyvenv -m /home/venv/wxapp
(现已改版为python3.6 -m venv web
venv是专门用来存放虚拟环境的目录,由于之后可能会创建其他的环境,因此该虚拟环境装在文件夹wxapp里source /home/venv/wxapp/bin/activate
激活虚拟环境,然后pip安装所需的各种包,或者把包放在txt文件里,然后pip install -r /home/requirements.txt
,下面是我用到的一些包Django==2.1.7
eyeD3==0.8.10
ffmpeg==1.4
librosa==0.6.0
llvmlite
mkl-fft
mkl-random
numba==0.38.0
numpy==1.14.5
pyAudioAnalysis==0.2.5
pydub==0.23.1
pyOpenSSL==18.0.0
python-magic==0.4.15
python-speech-features==0.6
scikit-image==0.13.1
scikit-learn==0.19.1
scipy==1.1.0
six==1.11.0
SoundFile==0.10.2
tensorboard==1.10.0
tensorflow==1.10.0
xlrd==1.1.0
XlsxWriter==1.0.4
xlwt==1.2.0
deactivate
注有时升级pip3后会报错:Traceback (most recent call last): File “/usr/bin/pip3”, line 9, in xxx from pip import main
这时就更改/usr/bin/pip3文件为:
from pip import __main__
if __name__ == '__main__':
sys.exit(__main__._main())
sudo apt-get install apache2
sudo apt-get install libapache2-mod-wsgi-py3
apachectl -v
sudo apt-get install openssl
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so-3.6
mod_wsgi.so-3.6与文件夹中的mod_wsgi.so版本对应,是什么填什么>
:443>
ServerAdmin email-address
DocumentRoot /home/AppServer
ServerAlias domain.com
ServerName domain.com
WSGIScriptAlias / /home/AppServer/AppServer/wsgi.py #第一个/表示访问的目录,假设域名是www.domain.com,wsgi中的路由为test,那么如果第一个是/,则www.domain.com/test即可到对应页面,如果第一个是/app,www.domain.com/app/test才到对应的页面
WSGIDaemonProcess miniapp python-home=/home/venv/wxapp
WSGIProcessGroup miniapp
WSGIScriptReloading On
WSGIApplicationGroup %{GLOBAL}
# 设定Apache访问django的项目目录
Alias /static/ /home/AppServer/static
Alias /miniprogram/ /home/AppServer/miniprogram
# 第一个这个应该没有可以,如果报错再加上
>
AllowOverride None
Require all granted
Options Indexes FollowSymLinks
>
>
AllowOverride None
Options Indexes FollowSymLinks
Require all granted
>
>
AllowOverride None
Options Indexes FollowSymLinks
Require all granted
>
>
>
AllowOverride None
Require all granted
>
>
#以下是从default-ssl.conf中没有注释掉的复制过来的
ErrorLog /home/logs/miniprogram-ssl-error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLEngine on
# 此处需要改为自己的证书地址
SSLCertificateFile /etc/apache2/ssl/2_xxx.pem
SSLCertificateKeyFile /etc/apache2/ssl/3_xxx.key
SSLCertificateChainFile /etc/apache2/ssl/1_root_bundle.pem
|shtml|phtml|php)$">
SSLOptions +StdEnvVars
>
-bin>
SSLOptions +StdEnvVars
>
>
>
对于上面配置文件中的目录,我把我自己的目录树放在下面
注:如果涉及到文件夹需要读写,需要打开rewrite权限:
a2enmod rewite
Alias /record/ /home/AppServer/record
>
AllowOverride All # 这里需要把None改为All
Require all granted
Options Indexes FollowSymLinks
>
接下来使配置生效
sudo /etc/init.d/apache2 start
sudo a2enmod ssl
a2dissite 000-default
a2ensite AppServer-ssl
systemctl reload apache2
wsgi.py
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'AppServer.settings')
application = get_wsgi_application()
改为
import os, sys
from os.path import join, dirname, abspath
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'AppServer.settings')
PROJECT_DIR = dirname(dirname(abspath(__file__)))
sys.path.insert(0, PROJECT_DIR)
application = get_wsgi_application()
settings.py
# 文件末尾添加
STATIC_ROOT = os.path.join(BASE_DIR, 'static/')
# 注释掉一个
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
# 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
# 修改为自己的域名,后面经过各种测试,发现还需要加上服务器ip地址,如果想要自己测试,还要加127.0.0.1
ALLOWED_HOSTS = ['domain.com', 'www.domain.com']
# 发布项目后把True改为False
DEBUG = True