目录html
sudo apt-get update
python
sudo apt-get install python3.6
linux
sudo apt-get install software-properties-common python-software-properties
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6
curl https://bootstrap.pypa.io/ez_setup.py -o - | python3.6 && python3.6 -m easy_install pip
pip install --upgrade pip
git
python --version && pip --version
检验版本github
pip install virtualenv
pip install virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
mkdir -p $WORKON_HOME
vim ~/.bashrc
,底部加入下面两行后,source ~/.bashrc
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
lsvirtualenv
看是否安装成功
find / -name virtualenvwrapper.sh
,结果替换/usr/local/bin/virtualenvwrapper.sh
再执行上述操做~/.bashrc
会在每次登录当前用户时自动执行,设置虚拟环境保存目录WORKON_HOMEsql
所以,使用其余用户登录则须要手动执行上述命令来设置环境变量。数据库
virtualenvs 使用详见:https://virtualenv.pypa.io/en/latest/apache
mkvirtualenv --python=python3.6 your_env_name
pip install django
sudo apt-get install apache2
:安装apache2服务,注意观察是否存在 perl: warning: Falling back to a fallback locale
异常,须要解决django
perl: warning: Setting locale failed. perl: warning: Please check that your locale settings: LANGUAGE = (unset), LC_ALL = (unset), LC_CTYPE = "zh_CN.UTF-8", LC_TERMINAL_VERSION = "3.3.1", LC_TERMINAL = "iTerm2", LANG = "en_US.UTF-8" are supported and installed on your system. perl: warning: Falling back to a fallback locale ("en_US.UTF-8").
vim ~/.bashrc
->最底部增长export LC_ALL=en_US.UTF-8
,en_US.UTF-8
需和报错信息说明的一致。source ~/.bashrc
从新加载环境变量/.bashrc
会在每次登录当前用户时执行,可是其余用户不会执行该文件,即只能在当前用户下正常使用 apache2。sudo apt-get install apache2-dev
:安装 apache2 开发工具bootstrap
该环节也可采起从官网下载源码编译安装的方式。
若使用 python3.5 可直接 sudo apt install libapache2-mod-wsgi-py3
,是以 python3.5编译好的,不须要如下安装步骤。
安装步骤来自官方文档:https://modwsgi.readthedocs.io/en/develop/user-guides/quick-installation-guide.html#apache-requirements
依赖项:
sudo apt-get install apache2-dev
:安装 apache2 开发工具,若是是以源码编译安装的 apache2,则可省略该步骤。apt-get 安装的 apache2 不包含开发工具。sudo apt-get install python3.6-dev
:若使用其余版本的python注意调整该命令正式安装:
wget https://codeload.github.com/GrahamDumpleton/mod_wsgi/tar.gz/4.5.24
:可访问官方,根据须要下载目标版本,下载内容为源码压缩包tar zxvf 4.5.24 && cd mod_wsgi-4.5.24
:解压并进入--with-apxs
:编译工具的地址,不设置则会自动在默认位置搜索。可先不设置尝试编译安装,若出错再根据平台(unbuntu、debian)查找位置
find / -name '*apxs*' -print
:检索当前目录下是否含 apxs--with-python
:指示编译对应的python版本,注意编译使用的python版本必须和后面网站执行使用的版本彻底相同,最好在同一路径!可参考下例
./configure --with-apxs=/usr/local/apache24/bin/apxs --with-python=/usr/local/python36/bin/python3.6
./configure --with-python=/home/XXX/.virtualenvs/env_name/bin/python3.6
make && make install
:权限不足请登陆 root 用户或前加 sudo
sudo chmod 644 /usr/lib/apache2/modules/mod_wsgi.so
make clean
后google一下。mod_wsgi.so
至 apache2
sudo vim /atc/apache2/apache.conf
LoadModule wsgi_module 刚刚打印出的mod_wsgi.so绝对路径
sudo service apache2 restart
or sudo service apache2 start
上传项目至服务器
(可选) 重置项目:测试时产生的数据库以及迁移记录。慎重!
rm db.sqlite3
rm -rf */migrations/
迁移
python manage.py migrate
python manage.py makemigrations
(可选) 建立管理帐号
python manage.py createsuperuser
测试
settings.py
:DEBUG=True
workon your_env
python manage.py runserver ).0.0.0:8000
django 部署静态文件
参考自:https://docs.djangoproject.com/zh-hans/3.0/howto/deployment/wsgi/modwsgi/
django设置:settings.py
STATIC_ROOT
:执行 python manage.py collectstatic
后,STATICFILES_FINDERS
找到的文件将被拷贝到该目录下
STATIC_URL
:用户以URL:ip:port/path?key=a&key=...
访问时,处于path
开头的 $STATIC_URL
部分将按照 apache_site.conf
进行替换获得服务器上的绝对路径。apache_site.conf
的配置在下一环节介绍。这种替换彻底是服务器框架的操做,和django无关。非 apache2 框架也有相似的设置方式。
STATICFILES_DIRS
:一个目录列表,若是在STATICFILES_FINDERS
中设置 'django.contrib.staticfiles.finders.FileSystemFinder'
则被STATICFILES_FINDERS
找到。
STATICFILES_FINDERS
:按规则查找静态文件
[ # STATICFILES_DIRS 'django.contrib.staticfiles.finders.FileSystemFinder', # $(APPs_path)/static 'django.contrib.staticfiles.finders.AppDirectoriesFinder', ]
详见:https://docs.djangoproject.com/zh-hans/3.0/ref/settings/#std:setting-STATIC_ROOT
python manage.py collectstatic
STATICFILES_DIRS
查找到的文件,复制到 STATIC_ROOT
下# 配置文件保存在 /etc/apache2/sites-available/ # /etc/apache2/sites-available/ 下新建你的网站文件 your-site-name.conf # 在其中配置如下内容 # 指定 80 端口 <VirtualHost *:80> # 指定网站的地址(域名或者 IP 地址) # 该地址必须在 ALLOWED_HOSTS 列表中 ServerName your_ip_addr # ServerAlias www.example1.com www.example2.com # ServerAdmin 0.0.0.0 # Alias URL=schema://ip:port/path?中path的前缀 实际服务器地址 # 该命令可以要求 apache2 自动对用户的访问 URL:path 进行转义 # 这里保证和 django setting 中 一致, # 若 STATIC_URL='/static/' STATIC_ROOT='/var/static_root/',则以下 Alias /static/ /var/static_root/ # 同时设置目录权限 <Directory /var/static_root> Require all granted </Directory> # 若是你还有其余文件夹须要转义,好比说存储视频的文件夹、或者存储你自定义内容的静态文件夹,均可以用这种方式来设置 # 指定 wsgi.py 脚本位置,ajango 项目中自动建立的 wsgi.py 是和 mod_wsgi 的挂载点 WSGIScriptAlias / /home/XXX/my_django_site/my_django_site/wsgi.py # "WSGIDaemonProcess" 是对守护进程的设置,具体参数参见 https://modwsgi.readthedocs.io/en/develop/configuration-directives/WSGIDaemonProcess.html # 主要设置两个参数: # python-path: 冒号分割的目录,这里的内容将被加入到python的检索范围内 # python-home:指明使用的python版本,须要指向包含 /bin/python 的目录,虚拟环境或直装python均适用 WSGIDaemonProcess your_ip_addr python-home=/home/my_usr_name/.virtualenvs/my_env_name python-path=/home/my_usr_name/.virtualenvs/my_env_name/lib/python3.6/site-packages:other_path_you_like WSGIProcessGroup your_ip_addr # 若是你对这里的设置感到迷惑,很是推荐去阅读一下上述连接,官方文档说的很是详细 # 还有其余功能好比说设置进程数等强大功能。 <Directory /home/XXX/my_django_site/my_django_site> <Files wsgi.py> Require all granted </Files> </Directory> </VirtualHost>
Apache 默认运行网站的用户是 www-data
,而咱们在上述设置中使用并非 www-data
因此须要修改项目文件、目录的权限,使得 Apache 可以对所须要的文件进行 RWX。
ls -al
# 请在理解下列命令的基础上修改、调整、执行 # 目录必须有 x 权限才能进入,所以须要可读可执行 # 通常文件须要可读的权限 sudo chmod -R 644 my_project sudo find my_project -type d | xargs chmod 755 # 若存在上传目录,则须要可写的权限 sudo chgrp www-data my_project/upload sudo chmod g+w my_project/upload # sqlite3 会生成一个db.sqlite3文件,因此须要修改这个文件的权限,加上写的权限 sudo chgrp www-data my_project sudo chmod g+w my_project sudo chgrp www-data my_project/db.sqlite3 sudo chmod g+w my_project/db.sqlite3 # 设置的 python-path python-home 或许也须要权限,根据你的设置,可先不设置,后面根据报错来调整
部署过程当中出现的问题80%都与权限有关,每一个平台、每一个环境都有所不一样,须要在理解权限的基础上根据后面的调试环境自行修正。
vim django_site/django_site/settings.py
:DEBUG = False
vim /var/log/apache2
查看日志
vim 快捷指令:
esc :e enter
shift+g
# 配置文件设置在 /etc/apache2/sites-available/ 目录下,这里只写名字,可在任何目录下执行 sudo a2ensite my_site.conf sudo service apache2 reload sudo service apache2 restart err: Invalid command 'WSGIScriptAlias', perhaps misspelled or defined by a module not included in the server configuration WSGIScriptAlias有问题的缘由一般是 mod-wsgi 安装有问题 检查 export LC_ALL=en_US.UTF-8 注意~/.bashrc 只针对当前用户有效 err: [Thu Feb 13 15:17:10.020824 2020] [wsgi:error] [pid 25702:tid 140038272866048] [remote 1.194.132.43:44] from django.core.wsgi import get_wsgi_application [Thu Feb 13 15:17:10.020840 2020] [wsgi:error] [pid 25702:tid 140038272866048] [remote 1.194.132.43:44] ImportError: No module named 'django' 首先检查 conf 文件中的python-path 是否配置正确,若是正确,则检查其下的权限是否正确,即路径目录均rx,全部文件均r err: 因为apache2 默认 挂载点是 '/' 所以在代码中写的相对路径将以该点从新计算,可能形成错误。
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the ECDSA key sent by the remote host is SHA256:ONuB9t+zfCAiryLoQ7eYnVE2FnG4IoH/hP4MMYGgOMY. Please contact your system administrator. Add correct host key in /Users/XXX/.ssh/known_hosts to get rid of this message. Offending RSA key in /Users/XXX/.ssh/known_hosts:2 ECDSA host key for X.X.X.X has changed and you have requested strict checking. Host key verification failed.
缘由:目标主机的公钥已经改变,为了防护中间人攻击,须要终止连接。具体动机可百度。
解决方案:
在服务器和本地端从新配置公私钥
本地:sudo chmod 600 私钥文件地址
Permissions 0644 for '/Users/XXX/.ssh/XXX.pem' are too open
ssh-keygen -R 目标访问ip地址(这个操做会删除.ssh/known_hosts
中记录的近期公钥
https://www.ruanyifeng.com/blog/2011/12/ssh_remote_login.html)
重启服务器
本地端访问服务器
sudo: unable to resolve host iZ2ze73nro8igc5iw6qmkgZ 缘由:http://code.sike.wang/code/show-5188.html 解决方案:在 etc/hosts 第一行 localhost 后添加这串乱码,这是阿里云建立给的默认主机名
本文由 ArrogantL 整理并在 CC BY-NC-SA 3.0 协议下发布。有任何问题请邮件联系 arrogant262@gmail.com
请各位遵循 Markdown: License 及其它参考文献的共享协议来使用、修改和发布。