{"id":984,"date":"2023-10-22T17:41:09","date_gmt":"2023-10-22T07:41:09","guid":{"rendered":"https:\/\/www.nickdu.com\/?p=984"},"modified":"2025-02-24T19:12:10","modified_gmt":"2025-02-24T09:12:10","slug":"deploy-django-4-0-on-shared-hosting","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=984","title":{"rendered":"Deploy Django 4.0 on Shared Hosting"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"0-create-a-python-app\">Create a Python App<\/h2>\n\n\n\n<p>Log in to CPanel and open&nbsp;<strong><em>Setup Python App<\/em><\/strong>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-16-48-1024x493.png\" alt=\"\" class=\"wp-image-2210\"\/><\/figure>\n\n\n\n<p>Click on&nbsp;<strong><em>Create Application<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-17-04-1024x493.png\" alt=\"\" class=\"wp-image-2211\"\/><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Select&nbsp;<strong><em>Python Version 3.9.12<\/em><\/strong><\/li>\n\n\n\n<li><strong><em>Application Root<\/em><\/strong>&nbsp;is the directory where you will place the code files of your Django project. Make sure that it is a different folder than your domain root.<\/li>\n\n\n\n<li><strong><em>Application URL<\/em><\/strong>&nbsp;is the domain or subdomain where your app will run<\/li>\n\n\n\n<li>In the&nbsp;<strong><em>Application Startup File<\/em><\/strong>, type&nbsp;passenger_wsgi.py<\/li>\n\n\n\n<li>In the&nbsp;<strong><em>Application Entry Point<\/em><\/strong>, type&nbsp;application<\/li>\n<\/ul>\n\n\n\n<p>After setting all these, click on the&nbsp;<strong><em>Create<\/em><\/strong>&nbsp;button.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-18-13-1024x493.png\" alt=\"\" class=\"wp-image-2212\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-setup-the-database\">Setup the Database<\/h2>\n\n\n\n<p>Open&nbsp;<strong><em>MySQL Databases<\/em><\/strong>&nbsp;in Cpanel<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-20-16-1024x493.png\" alt=\"\" class=\"wp-image-2213\"\/><\/figure>\n\n\n\n<p>Create a new Database and note the database name. We will need it later.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-20-28-1024x493.png\" alt=\"\" class=\"wp-image-2214\"\/><\/figure>\n\n\n\n<p>Create a new MySQL user and note the username and password. We will need it later.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-21-00-1024x493.png\" alt=\"\" class=\"wp-image-2215\"\/><\/figure>\n\n\n\n<p>Add the new user to the new Database<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-21-39-1024x353.png\" alt=\"\" class=\"wp-image-2216\"\/><\/figure>\n\n\n\n<p>Grant all the permissions to the user and select&nbsp;<strong><em>Make Changes<\/em><\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-21-51-1024x353.png\" alt=\"\" class=\"wp-image-2217\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-21-56-1024x467.png\" alt=\"\" class=\"wp-image-2218\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-upload-your-project\">Upload your project<\/h2>\n\n\n\n<p>Open the&nbsp;<strong><em>File Manager<\/em><\/strong>&nbsp;and go to the&nbsp;<strong><em>Application root<\/em><\/strong>&nbsp;you specified in the part 1<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-27-51-1024x467.png\" alt=\"\" class=\"wp-image-2219\"\/><\/figure>\n\n\n\n<p>Zip your project. Upload it to this folder and extract the zip file. Your project files should be in the same folder as the&nbsp;<em>passenger_wsgi.py<\/em>&nbsp;file. Make sure that&nbsp;<em>manage.py<\/em>&nbsp;and&nbsp;<em>passenger_wsgi.py<\/em>&nbsp;are in the same folder.<\/p>\n\n\n\n<p>Edit the&nbsp;<strong><em>passenger_wsgi.py<\/em><\/strong>&nbsp;file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-29-33-1024x467.png\" alt=\"\" class=\"wp-image-2220\"\/><\/figure>\n\n\n\n<p>Delete everything from this file and add the following code:<\/p>\n\n\n\n<p><code>from base.wsgi import application<\/code><\/p>\n\n\n\n<p>Where&nbsp;<strong><em>base<\/em><\/strong>&nbsp;is your project folder. It is the same folder that contains your&nbsp;<em>settings.py<\/em>&nbsp;file. It will be different if you are not using the test project that I provided. You can locate your wsgi.py file and import application from it.<\/p>\n\n\n\n<p>Now edit your&nbsp;<em>settings.py<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-30-20-1024x467.png\" alt=\"\" class=\"wp-image-2221\"\/><\/figure>\n\n\n\n<p>Add your domain to the\u00a0<em>ALLOWED_HOSTS<\/em>\u00a0list. If there is a www version of your domain, add that too. Do not use http:\/\/ to https:\/\/<code>ALLOWED_HOSTS = ['django.umer.link']<\/code><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-30-54.png\" alt=\"\" class=\"wp-image-2222\"\/><\/figure>\n\n\n\n<p>In the\u00a0<em>DATABASES<\/em>\u00a0dictionary, modify the default database.<code> 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'databasename', 'USER': 'databaseusername', 'PASSWORD': 'databasepassword', 'HOST': 'localhost', 'PORT': '3306', }<\/code><\/p>\n\n\n\n<p>Make sure to replace&nbsp;<em>databasename<\/em>&nbsp;with the database name,&nbsp;<em>databaseusername<\/em>&nbsp;with the database username and&nbsp;<em>databasepassword<\/em>&nbsp;with the database password.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-31-53.png\" alt=\"\" class=\"wp-image-2223\"\/><\/figure>\n\n\n\n<p>Now go to the end of the file, modify STATIC_URL and add the STATIC_ROOT<code>STATIC_URL = '\/static\/' STATIC_ROOT = '\/home\/username\/domainroot\/static'<\/code><\/p>\n\n\n\n<p>Replace the&nbsp;<em>username<\/em>&nbsp;with your CPanel username. The&nbsp;<em>domainroot<\/em>&nbsp;will be&nbsp;<em>public_html<\/em>&nbsp;if you are deploying on the main domain of your hosting account. If you are deploying on a subdomain or an add on domain, it will be name of the addon domain or the subdomain.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-33-11.png\" alt=\"\" class=\"wp-image-2224\"\/><\/figure>\n\n\n\n<p>Now edit the&nbsp;<em>__init__.py<\/em>&nbsp;file<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-33-26-1024x375.png\" alt=\"\" class=\"wp-image-2225\"\/><\/figure>\n\n\n\n<p>Add the following code<code>import pymysql pymysql.install_as_MySQLdb()<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-complete-configuration-from-the-terminal\">Complete configuration from the Terminal<\/h2>\n\n\n\n<p>Open the&nbsp;<strong><em>Terminal<\/em><\/strong>&nbsp;in your CPanel. If you can not find the terminal, go to&nbsp;<strong><em>Manage Shell<\/em><\/strong>&nbsp;and&nbsp;<strong><em>Enable SSH Access<\/em><\/strong>. You can also do these steps using SSH but using the terminal is easy.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-37-25-1024x375.png\" alt=\"\" class=\"wp-image-2226\"\/><\/figure>\n\n\n\n<p>Copy the command to enter the virtual environment from your python app.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-37-40-1024x375.png\" alt=\"\" class=\"wp-image-2227\"\/><\/figure>\n\n\n\n<p>Run the copied command in the terminal and press enter to enter the virtual environment,<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-37-55-1024x375.png\" alt=\"\" class=\"wp-image-2228\"\/><\/figure>\n\n\n\n<p>Install Django 4.0.4 by running the following command.<code>pip install django==4.0.4<\/code><\/p>\n\n\n\n<p><strong><em>pymysql<\/em><\/strong>\u00a0is required for using the MySQL database. Install it using pip. Here you will install any other modules required by your Django app.<code>pip install pymysql<\/code><\/p>\n\n\n\n<p>If your migrations are not complete, then make migrations.<code>python manage.py makemigrations<\/code><\/p>\n\n\n\n<p>Run migrate to create tables in the database.<code>python manage.py migrate<\/code><\/p>\n\n\n\n<p>Run collectstatic to move the static files to the static root folder specified in settings.<code>python manage.py collectstatic<\/code><\/p>\n\n\n\n<p>Run createsuperuser to ass a user to the Django admin panel<code>python manage.py createsuperuser<\/code><\/p>\n\n\n\n<p>Finally, restart the python app.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-39-34-1024x375.png\" alt=\"\" class=\"wp-image-2229\"\/><\/figure>\n\n\n\n<p>Your Django app is deployed successfully. Open the url of your app to see if it is working fine. If you see the phusion passenger error page, you can find the error in the stderr.log file for debugging.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/pythonfusion.com\/wp-content\/uploads\/2022\/05\/Screenshot-from-2022-05-08-05-41-51.png\" alt=\"\" class=\"wp-image-2230\"\/><\/figure>\n\n\n\n<p>Reference: <a href=\"https:\/\/pythonfusion.com\/deploy-django-on-shared-hosting\/\">https:\/\/pythonfusion.com\/deploy-django-on-shared-hosting\/<\/a><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Create a Python App Log in to CPanel and open&nbsp;Setup Python App. Click on&nbsp;Create Application After setting all these, click on the&nbsp;Create&nbsp;button. Setup the Database Open&nbsp;MySQL Databases&nbsp;in Cpanel Create a new Database and note the database name. We will need it later. Create a new MySQL user and note the username and password. We will &hellip; <a href=\"https:\/\/nickdu.com\/?p=984\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Deploy Django 4.0 on Shared Hosting&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-984","post","type-post","status-publish","format-standard","hentry","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/984","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=984"}],"version-history":[{"count":2,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/984\/revisions"}],"predecessor-version":[{"id":1013,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/984\/revisions\/1013"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}