pip freeze > requirements.txt
将当前环境的包导出到requirements.txt
文件中,方便在部署的时候安装。/srv
目录下。这里以git
的形式为例,打开终端,依次输入以下命令
安装好项目用到的Python
。python
安装virtualenv
以及virutalenvwrapper
,并建立虚拟环境。mysql
pip install virtualenvnginx
pip install virtualenvwrappergit
sudo apt install vimweb
vim ~/.bashrc
进入文件中,填入如下两行代码:sql
export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh
source ~/.bashrc数据库
安装git
:django
sudo apt install git
为了方便XShell或者CRT链接服务器,建议安装OpenSSH
:vim
sudo apt install openssh-server openssh-client service ssh restart
安装MySQL
服务器和客户端:浏览器
sudo apt install mysql-server mysql-client sudo apt-get install libmysqld-dev
进入虚拟环境中,而后进入到项目所在目录,执行命令:pip install -r requirements.txt
,安装好相应的包。
在mysql
数据库中,建立相应的数据库。
执行python manage.py migrate
命令,将迁移文件,映射到数据库中,建立相应的表。
执行python manage.py runserver 0.0.0.0:8000
,而后在你本身电脑上,在浏览器中输入http://你的服务器的ip地址:8000/
,访问下网站全部页 面,确保全部页面都没有错误。
设置ALLOW_HOST
为你的域名,以及ip地址。
设置DEBUG=False
,避免若是你的网站产生错误,而将错误信息暴漏给用户。
收集静态文件:python manage.py collectstatic
。
pip install uwsgi
就能够了。(uwsgi必须安装在系统级别的Python环境中,不要安装到虚拟环境中)。uwsgi --http :8000 --module test.wsgi --vritualenv=/root/.virtualenvs/django-env-py36
。用uwsgi
启动项目,若是可以在浏览器中访问到这个页面,说明uwsgi
能够加载项目了。在项目的路径下面,建立一个文件叫作djangotest.ini
的文件,而后填写如下代码:
[uwsgi] # Django相关的配置 # 必须所有为绝对路径 # 项目的路径 chdir=/srv/djangotest # Django的wsgi文件 module=djangotest.wsgi # Python虚拟环境的路径 home=/root/.virtualenvs/django-env-py36 # 进程相关的设置 # 主进程 master=true # 最大数量的工做进程 processes=10 # socket文件路径,绝对路径 socket=/srv/djangotest/djangotest.sock # 设置socket的权限 chmod-socket=666 # 退出的时候是否清理环境 vacuum=true
而后使用命令uwsgi --ini djangotest.ini
,看下是否还能启动这个项目。
nginx
是一个web
服务器。用来加载静态文件和接收http
请求的。经过命令sudo apt install nginx
便可安装。nginx
经常使用命令:
静态文件应该让nginx来服务,而不是让django来作。首先确保你的settings.py
文件中有一个STATIC_ROOT
配置,这个配置应该指定你的静态文件要放在哪一个目录下。那么咱们能够执行如下命令:python manage.py collectstatic
来收集全部静态文件,将这些静态文件放在指定的目录下。
在/etc/nginx/conf.d
目录下,新建一个文件,叫作djangotest.conf
,而后将如下代码粘贴进去:
upstream djangotest { server unix:///srv/djangotest/djangotest.sock; } # 配置服务器 server { # 监听的端口号 listen 80; # 域名 server_name 192.168.0.101; charset utf-8; # 最大的文件上传尺寸 client_max_body_size 75M; # 静态文件访问的url location /static { # 静态文件地址 alias /srv/djangotest/static_dist; } # 最后,发送全部非静态文件请求到django服务器 location / { uwsgi_pass djangotest; # uwsgi_params文件地址 include /etc/nginx/uwsgi_params; } }
写完配置文件后,为了测试配置文件是否设置成功,运行命令:service nginx configtest
,若是不报错,说明成功。每次修改完了配置文件,都要记得运行service nginx restart
。
让supervisor管理uwsgi,能够在uwsgi发生意外的状况下,会自动的重启。
supervisor
的安装:在系统级别的python环境下pip install supervisor
。my_supervisor.conf
。内容以下:# supervisor的程序名字 [program:mysite] # supervisor执行的命令 command=uwsgi --ini zlkt_uwsgi.ini # 项目的目录 directory = /srv/djangotest # 开始的时候等待多少秒 startsecs=0 # 中止的时候等待多少秒 stopwaitsecs=0 # 自动开始 autostart=true # 程序挂了后自动重启 autorestart=true # 输出的log文件 stdout_logfile=/srv/djangotest/log/supervisord.log # 输出的错误文件 stderr_logfile=/srv/djangotest/log/supervisord.err [supervisord] # log的级别 loglevel=info # 使用supervisorctl的配置 [supervisorctl] # 使用supervisorctl登陆的地址和端口号 serverurl = http://127.0.0.1:9001 # 登陆supervisorctl的用户名和密码 username = admin password = 123 [inet_http_server] # supervisor的服务器 port = :9001 # 用户名和密码 username = admin password = 123 [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
而后使用命令supervisord -c my_supervisor.conf
运行就能够了。 之后若是想要启动uwsgi
,就能够经过命令supervisorctl -c my_supervisor.conf
进入到管理控制台,而后能够执行相关的命令进行管理:
# 查看状态 status # 启动程序 start program_name # 从新启动程序 restart program_name # 关闭程序 stop program_name # 从新加载配置文件 reload # 退出控制台 quit