Hi all,
I recently gave DigitalOcean a shot on the recommendation of someone at HackerNews. DigitalOcean has a lot of custom droplets, which are basically Virtual Machines, that you can use to set up a cloud server on which to host your website.
I had been building a Django website that I wanted to put up, and while setting it up on DigitalOcean, I ran into the following problem: The default Django droplet uses a base Django project located in the 'home' directory of the UNIX installation DigitalOcean sets up for you. I wanted it to point to a custom directory in the /opt folder that I had set up.
In order to do this, remote into the DigitalOcean droplet. Once there, run the following command:
sudo nano /etc/init/gunicorn.conf
In this file, change 'chdir /home/django' to point to the directory that contains your django project. In my case, I changed it to '/opt/venv'.
Change the 'name' field to refer to the folder that contains your 'wsgi' file. In my case, I changed it to 'name=jobs'.
Change the 'pythonpath' field to refer to folder that contains your 'manage.py' file. In my case, I changed it to 'pythonpath=search-jobs'.
In 'django_project.wsgi:application' change 'django_project' to the name of your Django app (ie. what you changed your 'name' field to).
Now run the following command:
sudo nano /etc/nginx/sites-enabled/django
In this file, change the media and static locations to point to where you need them to point to. In my case I changed:
alias /home/django/django_project/django_project/media;
to:
alias /opt/venv/search-jobs/media;
and:
alias /home/django/django_project/django_project/static;
to:
alias /opt/venv/search-federal-jobs/staticfiles;
That should set up the default nginx and gunicorn to serve your django project located in your custom path properly!
If you have any feedback on this post, or if it helped, drop me a comment and let me know.
I recently gave DigitalOcean a shot on the recommendation of someone at HackerNews. DigitalOcean has a lot of custom droplets, which are basically Virtual Machines, that you can use to set up a cloud server on which to host your website.
I had been building a Django website that I wanted to put up, and while setting it up on DigitalOcean, I ran into the following problem: The default Django droplet uses a base Django project located in the 'home' directory of the UNIX installation DigitalOcean sets up for you. I wanted it to point to a custom directory in the /opt folder that I had set up.
In order to do this, remote into the DigitalOcean droplet. Once there, run the following command:
sudo nano /etc/init/gunicorn.conf
In this file, change 'chdir /home/django' to point to the directory that contains your django project. In my case, I changed it to '/opt/venv'.
Change the 'name' field to refer to the folder that contains your 'wsgi' file. In my case, I changed it to 'name=jobs'.
Change the 'pythonpath' field to refer to folder that contains your 'manage.py' file. In my case, I changed it to 'pythonpath=search-jobs'.
In 'django_project.wsgi:application' change 'django_project' to the name of your Django app (ie. what you changed your 'name' field to).
Now run the following command:
sudo nano /etc/nginx/sites-enabled/django
In this file, change the media and static locations to point to where you need them to point to. In my case I changed:
alias /home/django/django_project/django_project/media;
to:
alias /opt/venv/search-jobs/media;
and:
alias /home/django/django_project/django_project/static;
to:
alias /opt/venv/search-federal-jobs/staticfiles;
That should set up the default nginx and gunicorn to serve your django project located in your custom path properly!
If you have any feedback on this post, or if it helped, drop me a comment and let me know.
Hi! Thanks for posting this it is very helpful.
ReplyDeleteI have one quick question. So my django application is located in /var/TourApp. That folder contains everything that was created when I initially started developing the application. It also contains my manage.py file. So what do I put as my pythonpath? I have the chdir line as "chdir/var/TourApp". Since TourApp is the folder that contains manage.py, do I just leave it blank?