{"id":367,"date":"2015-10-27T07:08:32","date_gmt":"2015-10-27T07:08:32","guid":{"rendered":"http:\/\/onlinelab.info\/?p=367"},"modified":"2015-10-27T07:08:32","modified_gmt":"2015-10-27T07:08:32","slug":"deploying-django-project-with-nginx-and-uwsgi","status":"publish","type":"post","link":"https:\/\/www.asianux.org.vn\/index.php\/2015\/10\/27\/deploying-django-project-with-nginx-and-uwsgi\/","title":{"rendered":"Deploying Django project with Nginx and Uwsgi"},"content":{"rendered":"<p>Sorry for a small pause, this time I will try to show you how to deploy a django project with Nginx and Uwsgi.<\/p>\n<p>Speaking you have debian, follow these steps:<\/p>\n<p><strong>we first install virtualenv<\/strong>:<\/p>\n<p><code>pip install virtualenv<\/p>\n<p><\/code>then we open a new virtual environment for our new django project. Virtualenv is a cool stuff, you won&#8217;t have dependency hell with it, just your own virtualenv where you can install and run any modules you want without messing up with the modules that are installed systemwide or etc..<\/p>\n<p>lets now\u00a0create and activate our own virtualenv:<\/p>\n<p><code>virtualenv project_env<br \/>\ncd project_env<br \/>\nsource bin\/activate<\/p>\n<p><\/code>and now we will install our wonderful django framework making sure that our virtualenv is activated. It is not important that we are inside the virtualenv folder, main thing virtualenv is activated:<\/p>\n<p><code>pip install Django<\/code><br \/>\n<code>django-admin.py startproject mysite<\/code><\/p>\n<p><strong>now we install uwsgi into our virtualenv:<\/strong><\/p>\n<p><code>pip install uwsgi<\/code><\/p>\n<p>Now, we need to install Nginx, here we go out of our virtualenv by doing:<\/p>\n<p><code>deactivate<\/code><\/p>\n<p>and install nginx:<br \/>\nsudo apt-get install nginx<\/p>\n<p><strong>Configure now Nginx:<\/strong><\/p>\n<p>first, you need to <code>uwsgi_params<\/code> file which is available<a href=\"http:\/\/here:%26nbsp%3Bhttps\/\/github.com\/nginx\/nginx\/blob\/master\/conf\/uwsgi_params%3C\/p%3E\" rel=\"nofollow\">here:\u00a0https:\/\/github.com\/nginx\/nginx\/blob\/master\/conf\/uwsgi_params<\/a><\/p>\n<p>Just copy this into your project directory where nginx will refer to. I will you this in a while.<\/p>\n<p>Now create a mysite_nginx.conf file and put this content into it:<\/p>\n<pre>upstream mysite {\n    server unix:\/\/\/var\/www\/mysite\/mysite.sock;\n}\n\nserver {\n    listen      80;\n    server_name .mysite.com;\n    charset     utf-8;\n    client_max_body_size 75M;\n\n    location \/media  {\n        alias \/var\/www\/mysite\/mysite\/media;\n    }\n\n    location \/static {\n        alias \/var\/www\/mysite\/mysite\/static;\n    }\n\n    location \/ {\n        uwsgi_pass  mysite;\n        include     \/etc\/nginx\/uwsgi_params;\n        proxy_read_timeout 150;\n    }\n\n    location \/nginx_status {\n        stub_status on;\n        access_log   off;\n        # allow 127.0.0.1;\n        # deny all;\n    }\n}<\/pre>\n<p>Now we simlink this file to nginx sites-enabled folder:<\/p>\n<p><code>sudo ln -s ~\/path\/to\/your\/mysite\/mysite_nginx.conf \/etc\/nginx\/sites-enabled\/<\/code><\/p>\n<p>and collect all django static files into static folder:<br \/>\n<code>python manage.py collectstatic<\/code><\/p>\n<p>and restart the nginx:<br \/>\n<code>sudo \/etc\/init.d\/nginx restart<\/code><\/p>\n<p>Now we create a ini file and put this content into it:<\/p>\n<pre>[uwsgi]\nsocket = \/var\/www\/mysite\/mysite.sock\nchdir = \/var\/www\/mysite\nmodule = mysite.wsgi:application\nvirtualenv = \/var\/www\/virtualenvs\/project_env\/\nbuffer-size = 8192\nprocesses = 60\nenv = DJANGO_SETTINGS_MODULE=mysite.settings\nenable-threads = True\nsingle-interpreter = True\nuid = www-data\ngid = www-data\nvacuum = True\ndisable-logging = true\nlogger = file:\/var\/www\/mysite\/log\/uwsgi_err.log<\/pre>\n<p>with this, we have all our uwsgi configurations in one single file &#8211; easy to handle and configure.<\/p>\n<p>As you noticed, we installed uwsgi only in our virtualenv, now we need to install it systemwide (look at deactivate with which we leave the virtualenv).<\/p>\n<p><code>deactivate<\/code><br \/>\n<code>sudo pip install uwsgi<\/code><\/p>\n<p>and create vassals folder<\/p>\n<p><code>mkdir \/etc\/uwsgi<\/code><br \/>\n<code>mkdir \/etc\/uwsgi\/vassals<\/code><br \/>\n<code>cd \/etc\/uwsgi\/vassals\/<\/code><\/p>\n<p>and simlink our ini file to this vassals folder:<\/p>\n<p><code>ln -s \/var\/www\/mysite\/conf\/uwsgi.ini\u00a0mysite.ini<\/code><\/p>\n<p>We are almost ready. Now we open the rc.local file and put this command into it before &#8220;exit 0&#8221;<\/p>\n<p><code>\/usr\/local\/bin\/uwsgi --emperor \/etc\/uwsgi\/vassals --uid www-data --gid www-data --daemonize \/var\/log\/uwsgi\/emperor.log<\/code><\/p>\n<p>and save the file. With this, the uwsgi runs as daemon and restarts automatically if system reboots etc etc.. nice stuff<\/p>\n<p>aaand last step: give this command into your console:<\/p>\n<p><code>\/usr\/local\/bin\/uwsgi --emperor \/etc\/uwsgi\/vassals --uid www-data --gid www-data --daemonize \/var\/log\/uwsgi\/emperor.log<\/code><\/p>\n<p>and restart nginx:<br \/>\n<code>\/etc\/init.d\/nginx restart<\/code><\/p>\n<p>Hope, you didnot get any errors while deploying and that this post will help someone! have fun!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sorry for a small pause, this time I will try to show you how to deploy a django project with Nginx and Uwsgi. Speaking you have debian, follow these steps: we first install virtualenv: pip&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-367","post","type-post","status-publish","format-standard","hentry","category-programing"],"_links":{"self":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/comments?post=367"}],"version-history":[{"count":0,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.asianux.org.vn\/index.php\/wp-json\/wp\/v2\/tags?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}