Lunix下部署Django web
1、安装python3
2、virtualenv虚拟环境安装及创建一个虚拟环境
mkvirtualenv 新建虚拟环境
workon 列出所有虚拟环境
workon xxx 进入xx虚拟环境
2.1 在虚拟环境中 安装项目所需要的模块
- windows下 进入项目虚拟环境 导出项目所依赖的安装包名称 > pip freeze > requirements.txt
- lunix下安装项目所需要的包
- 进入项目虚拟环境 $ vim requirements.txt 把 window下安装包名称粘贴过来
- 下载安装包 $ pip install -r requirements.txt
3、安装mysql、连接windows下navicat
- mysql下载 $ sudo apt-get install mysql-server ==> 下载完成后会自动让你输入数据库的账户及密码,记好了别忘了;
- 让虚拟机中的数据库能和windows下的navicat连接需要 $ sudo vim /etc/mysql//mysql.conf.d/mysqld.cnf ==> 修改 bind-address = 0.0.0.0
- mysql> GRANT ALL PRIVLEGES ON *.* TO ''ROOT'@'$' IDENTIFIED BY 'root' WITH GRANT OPTION;
- mysql> FLUSH PRIVILEGES;
- 完成虚拟机下的mysql数据库修改,可以在windows下进行用navicat进行连接了,navicat连接地址 ($ ifconfig) ==>连接成功后创建数据库(与项目数据库名要一致) ==> 注意新建连接要修改字符集(utf8 --UTF-8 Unicode)和排序规则 (utf8_general_ci)==> 数据传输 ==> 完成
3、安装nginx
- $ sudo apt-get install nginx
- ifconfig 查看ip地址即可访问 显示welcome to nginx 表示成功
4、uwsgi安装
安装
pip install uwsgi
测试
uwsgi --http :8000 --module MxOnline.wsgi
实现了 192.168.175.342:8000 访问了而不再是127.0.0.1:8000
5、nginx与uwsgi连接
- 配置nginx ,且
/etc/nginx/conf.d/建立软连接
新建uc_nginx.conf # the upstream component nginx needs to connect to upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8000; # for a web port socket (we'll use this first) } # configuration of the server server { # the port your site will be served on listen 80; # the domain name it will serve for server_name 你的ip地址 ; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias 你的目录/Mxonline/media; # 指向django的media目录 } location /static { alias 你的目录/Mxonline/static; # 指向django的static目录 } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include uwsgi_params; # the uwsgi_params file you installed } }
不仅要写ip地址而且还要填写一个域名,不然会 welcome to nginx!
- 新建uwsgi启动文件
# mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /home/bobby/Projects/MxOnline # Django's wsgi file module = MxOnline.wsgi # the virtualenv (full path) # process-related settings # master master = true # maximum number of worker processes processes = 10 # the socket (use the full path to be safe socket = 127.0.0.1:8000 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true virtualenv = /home/bobby/.virtualenvs/mxonline logto = /tmp/mylog.log
注:
chdir: 表示需要操作的目录,也就是项目的目录
module: wsgi文件的路径
processes: 进程数
virtualenv:虚拟环境的目录
- 静态文件及media上传:
STATIC_ROOT = os.path.join(BASE_DIR, "static/")
python manage.py collectstatic
- 启动
uwsgi -i 你的目录/Mxonline/conf/uwsgi.ini