本文主要讲解 nginx + uwsgi 的方式来部署 Django网站,服务器为 Ubuntu 14.04.5 LTS (GNU/Linux 3.13.0-107-generic x86_64)。
步骤为1.工程文件传输到服务器上;2.在服务器上运行开发环境测试;3.设置Nginx和uwsgi配置文件。
第二步:运行开发环境
(1)安装虚拟环境,下载需要的包。
(2)在虚拟环境下,测试开发环境。
python manage.py makemigrations python manage.py migrate python manage.py createsuperuser python manage.py collectstatic python manage.py runserver
第三步:设置Nginx和uwsgi配置文件
(1)安装Nginx和uwsgi。
(2)设置配置文件
Nginx配置文件
# mysite_nginx.conf # the upstream component nginx needs to connect to upstream django { # server unix:///home/ubuntu/blogproject.sock; # for a file socket server 127.0.0.1:3400; # 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 www.summerdawn.top www.summerdawn.xyz summerdawn.top summerdawn.xyz; # 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/ubuntu/blogproject/media; # your Django project's media files - amend as required } location /static { alias /home/ubuntu/blogproject/static; # your Django project's static files - amend as required } # 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 } }
# mysite_uwsgi.ini file [uwsgi] # Django-related settings # the base directory (full path) chdir = /home/ubuntu/blogproject # Django's wsgi file module = blogproject.wsgi # the virtualenv (full path) home = /home/ubuntu/blog_env # process-related settings # master master = true # maximum number of worker processes processes = 4 # the socket (use the full path to be safe #socket = /home/ubuntu/blogproject.sock socket = 127.0.0.1:3400 # ... with appropriate permissions - may be needed chmod-socket = 664 # clear environment on exit vacuum = true
配置文件均放网站工程下方便,运行如下命令
sudo ln -s /home/ubuntu/blogproject/mysite_nginx.conf /etc/nginx/sites-enabled/ sudo /etc/init.d/nginx restart # create a directory for the vassals sudo mkdir /etc/uwsgi sudo mkdir /etc/uwsgi/vassals # symlink from the default config directory to your config file sudo ln -s /home/ubuntu/blogproject/blogproject.ini /etc/uwsgi/vassals/ # run the emperor uwsgi --emperor /etc/uwsgi/vassals --uid www-data --gid www-data
如此操作,Bingo
您尚未登录,请先登录才能评论。