通过nginx+uwsgi部署django应用在ubuntu

2019-07-19 15:49:34 浏览数 (1)

路过的关注一下哈 :

1.安装python3-pip -------------sudo apt-get install python3-pip 2.安装虚拟环境和虚拟环境管理包 sudo pip3 install virtualenv (报错的话需要把pip也安装一下) sudo pip3 virtualenvwrapper 在~/.bashrc 添加 export WORKON_HOME=~/Envs source /usr/local/bin/virtualenvwrapper.sh (如果没有virtualenvwrapper.sh就在这目录下创建这个文件) source .bashrc 创建虚拟环境:mkvirtualenv ShangOnline –p /usr/bin/python3 3.虚拟环境当中安装项目所依赖的包(mysqlclient会出一个问题,需要首先安装下面这个包) 我们可以通过 pip freeze > requirements.txt 将windows的虚拟环境安装包相应信息导出来移动到ubuntu pip3 install -r requirements.txt 在安装过程中mysqlclient会报错,按照下面两步走 sudo apt-get install libmysqlclient-dev pip install mysqlclient

4. 数据库: sudo apt-get install mysql-server 进入数据库创建自己的账户并且分配所有的权限,并且刷新权限,在配置文件当中bind 0.0.0.0 在ubuntu 当中创建我们用的数据库shangonline,通过windows的Navicat将windows数据库中的数据传输到ubuntu当中 将我们的项目文件夹拖入到我们的虚拟环境当中,进入项目python manage.py runserver保证能拉起项目 5.安装uwsgi-------pip3 install uwsgi 测试uwsgi---------uwsgi --http :8000 --module ShangOnline.wsgi

6. 安装配置nginx------sudo apt-get install nginx 安装完成会自动启动nginx的服务,我们在外部windows浏览器直接访问ubuntu ip 会进入到nginx的环境界面 在项目 根目录新建文件夹config-------新建sol_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 /home/ly/ShangOnline/static/media; # 指向django的media目录 }

location /static { alias /home/ly/ShangOnline/static; # 指向django的static目录 }

# Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed } } 7. 将该配置文件加入到nginx的启动配置文件中 sudo cp /home/ly/ShangOnline/config/sol_nginx.conf /etc/nginx/conf.d/ 8. 拉取所有需要的static file 到同一个目录 在django的setting文件中,添加下面一行内容:

STATIC_ROOT = os.path.join(BASE_DIR, "static/") 运行命令 python manage.py collectstatic 9. 运行nginx sudo /usr/sbin/nginx 这里需要注意 一定是直接用nginx命令启动, 不要用systemctl启动nginx不然会有权限问题 10. 通过配置文件启动uwsgi 新建uwsgi.ini 配置文件, 内容如下:

# mysite_uwsgi.ini file [uwsgi]

# Django-related settings # the base directory (full path) chdir = /home/ly/ShangOnline/ # Django's wsgi file module = ShangOnline.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/ly/Envs/ShangOnline

logto = /tmp/mylog.log

******注: chdir: 表示需要操作的目录,也就是项目的目录 module: wsgi文件的路径 processes: 进程数 virtualenv:虚拟环境的目录

workon ShangOnline uwsgi -i /home/ly/ShangOnline/config/uwsgi.ini & 10.访问 http://你的ip地址/

0 人点赞