{"id":2655,"date":"2018-10-05T21:00:11","date_gmt":"2018-10-05T15:30:11","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=2655"},"modified":"2019-04-05T18:07:57","modified_gmt":"2019-04-05T12:37:57","slug":"setting-up-django-and-your-web-server-with-uwsgi-and-nginx-on-ubuntu","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/setting-up-django-and-your-web-server-with-uwsgi-and-nginx-on-ubuntu\/","title":{"rendered":"Setting up Django and your web server with uWSGI and nginx on Ubuntu"},"content":{"rendered":"<h2>Setting up Django and your web server with uWSGI and nginx<\/h2>\n<p><strong>Date posted: 05-oct-2018<\/strong><\/p>\n<p>\u00a0<\/p>\n<p>In this article, we will see how to setup a Django web application on Ubuntu 16.04 server. We&#8217;ll use UWSGIservice to deploy our webapp along with Nginx.<\/p>\n<h4>Requirements:-<\/h4>\n<p>Python 3.6<\/p>\n<p>Django\u00a02.0.7<\/p>\n<p>Nginx\u00a01.10.3<\/p>\n<p>Ubuntu 16.04<\/p>\n<h4>Install Python basic packages<\/h4>\n<p class=\"p1\"><code>sudo apt-get install python3-dev\u00a0\u00a0python3-pip\u00a0<\/code><\/p>\n<p class=\"p1\"><code>sudo apt-get install python3-setuptools<\/code><\/p>\n<p class=\"p1\"><code>sudo apt-get install python3-kivy<\/code><\/p>\n<h4 class=\"p1\">Install Virutal Environment<\/h4>\n<p><code>pip3 install virtualenv virtualenvwrapper<\/code><code><\/code><\/p>\n<h4>Install Django<\/h4>\n<p><code>pip3 install django<\/code><\/p>\n<h4 class=\"p1\">Install UWSGI<\/h4>\n<h4 class=\"p1\"><code>sudo pip install uwsgi<\/code><\/h4>\n<p class=\"p1\">Now we need to create the vassals folder, which is the folder where our <code>.ini<\/code> files (called <code>vassals<\/code>) will reside upon. Inside vassals we will have two other folders: <code>available<\/code> and <code>enabled<\/code>. The first will contain all the (.ini) configuration files. While the second (<code>enabled<\/code>) will contain a symlink to the files at <code>available<\/code><\/p>\n<p class=\"p1\"><code>cd \/etc\/<\/code><\/p>\n<p class=\"p1\"><code>sudo mkdir uwsgi<\/code><\/p>\n<p class=\"p1\"><code>cd uwsgi<\/code><\/p>\n<p class=\"p1\"><code>sudo mkdir vassals<\/code><\/p>\n<p class=\"p1\"><code>cd vassals<\/code><\/p>\n<p class=\"p1\"><code>sudo mkdir enabled<\/code><\/p>\n<p class=\"p1\"><code>sudo mkdir available<\/code><\/p>\n<p class=\"p1\">Now we need to create an <code>emperor.ini<\/code> file, which will contain the path for the other vassals at <code>enabled<\/code>:<\/p>\n<p class=\"p1\"><code>sudo nano \/etc\/uwsgi\/vassals\/emperor.ini<\/code><\/p>\n<p class=\"p1\">Add this lines to the file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">[uwsgi]\n\nemperor = \/etc\/uwsgi\/vassals\/enabled<\/pre>\n<p class=\"p1\">Now, it&#8217;s time to deamonize uwsgi. In order to do it, create a <code>usgi.service<\/code> file at <code>\/etc\/systemd\/system\/<\/code>:<\/p>\n<p class=\"p1\"><code>sudo vi \/etc\/systemd\/system\/uwsgi.service<\/code><\/p>\n<p class=\"p1\">Now, paste the following content into it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">[Unit]\nDescription=uWSGI Emperor\nAfter=syslog.target\n\n[Service]\nExecStart=\/usr\/local\/bin\/uwsgi --ini \/etc\/uwsgi\/vassals\/emperor.ini\n# Requires systemd version 211 or newer\nRuntimeDirectory=uwsgi\nRestart=always\nKillSignal=SIGQUIT\nType=notify\nStandardError=syslog\nNotifyAccess=all\n\n[Install]\nWantedBy=multi-user.target<\/pre>\n<p class=\"p1\">To start the service<\/p>\n<p class=\"p1\"><code>sudo systemctl start uwsgi<\/code><\/p>\n<p class=\"p1\">To check service status<\/p>\n<p class=\"p1\"><code>systemctl status uwsgi<\/code><\/p>\n<p class=\"p1\">Lets enable the service. So that it starts at boot<\/p>\n<p class=\"p1\"><code>sudo systemctl enable uwsgi<\/code><\/p>\n<p class=\"p1\">To stop the service<\/p>\n<p class=\"p1\"><code>sudo systemctl stop uwsgi<\/code><\/p>\n<p>Lets configure our django project to run with uwsgi.<\/p>\n<p>Lets create a file\u00a0\/etc\/uwsgi\/vassals\/available\/kb_web.ini<\/p>\n<p><code>vi\u00a0\/etc\/uwsgi\/vassals\/available\/web_app.ini<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"># webapp_uwsgi.ini file\n[uwsgi]\n\n# Django-related settings\n# the base directory (full path)\nchdir           = \/var\/www\/webapp\n# Django's wsgi file\nmodule          = webapp.wsgi\nenable-threads = true\n\n# process-related settings\n# master\nmaster          = true\n# maximum number of worker processes\nprocesses       = 10\nlisten  = 100\n# the socket (use the full path to be safe\nsocket          = \/var\/www\/webapp\/webapp\/web_app.sock\n# ... with appropriate permissions - may be needed\nchmod-socket    = 666\n# clear environment on exit\nvacuum          = true\nmax-requests = 1000\n#limit-as = 128\ndaemonize =  \/var\/log\/uwsgi-emperor.log<\/pre>\n<p>Lets create a symlink to <code>enabled<\/code> folder<\/p>\n<p><code>ln -s\u00a0\/etc\/uwsgi\/vassals\/available\/kb_web.ini\u00a0\/etc\/uwsgi\/vassals\/enabled\/<\/code><\/p>\n<p>Please make sure you&#8217;ve uploaded your project folder under \/var\/www<\/p>\n<p><span style=\"color: #222222;\"><span style=\"background-color: #f2f4f5;\"><code>ls -l \/var\/www\/webapp<\/code><\/span><\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">drwxr-xr-x 3 www-data www-data 4096 Sep 14 05:41 bin\ndrwxr-xr-x 6 root     root     4096 Oct  4 13:46 webapp\n-rwxr-xr-x 1 root     root      538 Aug 10 02:14 manage.py\n-rw-r--r-- 1 root     root      275 Aug 10 02:14 requirements<\/pre>\n<p>Lets manually verify the application by running this command<\/p>\n<p><code>python3 manage.py runserver 0.0.0.0:8000<\/code><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\"># python3 manage.py runserver 0.0.0.0:8000\nPerforming system checks...\n\nSystem check identified no issues (0 silenced).\nOctober 05, 2018 - 12:17:12\nDjango version 2.0.7, using settings 'kb1988.settings'\nStarting development server at http:\/\/0.0.0.0:8000\/\nQuit the server with CONTROL-C.<\/pre>\n<p>It looks fine and no python module errors.<\/p>\n<h4>Configure nginx domain<\/h4>\n<p>Lets configure nginx virtual host<\/p>\n<p><code>vi \/etc\/nginx\/sites-available\/webapp.com<\/code><\/p>\n<p>Enter the below contents. You can adjust the folder path as per your needs.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">upstream web_app {\n     server unix:\/\/\/var\/www\/webapp\/webapp\/kb_web.sock; # for a file socket\n}\nserver {\n    # the port your site will be served on\n    listen      80;\n    # the domain name it will serve for\n    server_name pheonixsolutions.com\n    charset     utf-8;\n\n    # max upload size\n    client_max_body_size 75M;   # adjust to taste\n    location \/media  {\n        alias \/var\/www\/webapp\/webapp\/media;  # your Django project's media files - amend as required\n    }\n\n    location \/static {\n        alias \/var\/www\/webapp\/webapp\/static;\n    }\n\n    # Finally, send all non-media requests to the Django server.\n    location \/ {\n        uwsgi_pass  web_app;\n        include     \/etc\/nginx\/uwsgi_params; # the uwsgi_params file you installed\n    }\n}<\/pre>\n<p>Verify nginx syntax test<\/p>\n<p><code>nginx -t<\/code><\/p>\n<p>Reload nginx service<\/p>\n<p><code>\/etc\/init.d\/nginx reload<\/code><\/p>\n<p>Restart UWSGI<\/p>\n<p><code>service restart uwsgi\u00a0<\/code><\/p>\n<p>Your domain should work at <code>http:\/\/pheonixsolutions.com<\/code><\/p>\n<h4>Added tips:-<\/h4>\n<p>To create superuser admin<br \/><br \/>virtualenv command , which will give platform to run source\/bin\/activate.<br \/><code><br \/>source \/var\/www\/webapp\/bin\/activate<\/code><\/p>\n<p><code>python3 manage.py createsuperuser --username=devops --<\/code><\/p>\n<p><code>email=devops@pheonixsolutions.com<\/code><code><br \/>\nEnter password: ********<\/code><\/p>\n<p>To collect static files from all the apps in one place<\/p>\n<p><code>python3 manage.py collectstatic<\/code><\/p>\n<p>If you run into errors, You should mention STATIC_ROOT path in your settings.py file. And then run above command again.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">STATIC_ROOT = '\/var\/www\/webapp\/webapp\/static\/'<\/pre>\n<p>\u00a0<\/p>\n<h2><strong>Steps to eradicate Django migration errors-&#8220;core_module issue&#8221; :<\/strong><\/h2>\n<p>In python you need to do migrate the changes on prod while its running. If we didn&#8217;t complete the migration properly.<\/p>\n<p>\u00a0<\/p>\n<p>This kind of error will make us more disappointing at a last stage of migration. we will see errors like shown below.<\/p>\n<p>\u00a0<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/03\/Screen-Shot-2019-03-29-at-1.23.41-PM-1-1024x343.png\" alt=\"This image has an empty alt attribute; its file name is Screen-Shot-2019-03-29-at-1.23.41-PM-1-1024x343.png\" \/><\/p>\n\n\n<p class=\"wp-block-paragraph\">As the error stats that this is an issue with core module. We believe that the code migration didn&#8217;t applied completely. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To resolve that we need to run below commands:-<\/p>\n\n\n<pre class=\"wp-block-code\"><code>python3.6 manage.py makemigrations core\npython3.6 manage.py migrate core<\/code><\/pre>\n<p>Now login to your django admin panel and check. The errors should be gone.<\/p>\n\n\n<p class=\"wp-block-paragraph\">That&#8217;s it. We&#8217;re done with the setup!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Setting up Django and your web server with uWSGI and nginx Date posted: 05-oct-2018 \u00a0 In this article, we will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-2655","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-GP","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=2655"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/2655\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=2655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=2655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=2655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}