Hello, and welcome!
If you’ve recently upgraded to Ubuntu 16.04 LTS, you probably are wondering if there are any revamps you might need to do. In my case, I had to.
Short storyI deploy mystuffentirely on Digital Ocean. Since 16.04 was recently released, I checked my DO control panel and realized I could move from the 14.04 to the 16.04. From the control panel, I follow the instructions and got the upgrade done.
However, the upgrade actually wasn’t done! The CP showed linux kernel 4.4 on 16.04 Ubuntu, whereas doing a simple uname -rvs in my terminal on the server box printed that I am still on 14.04.
This came to light after I realized I couldn’t install LetsEncrypt. Issuing the command to install on 16.04 failed, as my box was still on 14.04.
Fast-forward, I manually changed my sources.list of apt-get ; I simply replaced all trusty to xenial in the file, after I made sure I had a backup. sudo apt-get update && sudo apt-get dist-upgrade was all left.
After about 10 minutes, everything came back to normal, except my Nginx had gone up to 1.1 and my php has shifted to version 7. All were exciting news, however, I needed to put a couple of changes in to get things going!
So Django’s Turn CameIn this piece, I wish to share with you how to get a quick turn around with setting up your PostgreSQL, Django 1.9, Nginx, Gunicorn with VirtualEnvWrapper under minutes, less than 10, actually!
Prepping Surgery Table sudoapt-getinstallpython-pippython-dev \ python3-pippython3-devlibpq-devpostgresql \ postgresql-contribnginxThe above ensures you install the necessary packages, such as Python and Python3 and pip and pip3, along with Nginx and PostgreSQL. Oneliner, done! You might not need to install the python3 and pip3 if you’re not going to use any of them. It is up to you.
Our surgery table happens to be clean now, free of germs. Let’s bring the actual objects to operate on! In this case, the first in line:
PostgreSQL SetupWe’re using PostgreSQL as the database backend in our setup. PostgreSQL is a powerful relational database system, and perfectly marries Django in all of its glory. I personally have been using PostgreSQL, and the experience is solid. No wonder it is well and highly spoken of!
You probably have your PostgreSQL database setup already, since I assume you had everything running and only had to upgrade from 14.04 to 16.04. If that is what you did (like I did), you don’t have to set up anything anymore. Just jump to the Django part, and keep going.
Your databases, passwords etc are all available, and tyinginto your Django project should require details last used on 14.04.
If you are starting afresh with PostgreSQL setup, here we go:
sudo -u postgrespsql CREATEDATABASEnewproject; CREATEUSERprojectuserWITHPASSWORD 'password'; // django recommends values below set: // http://devdocs.io/django~1.9/ref/databases // using the ALTER ROLE to get things done! ALTERROLEprojectuserSETclient_encodingTO 'utf8'; ALTERROLEprojectuserSETdefault_transaction_isolationTO 'read committed'; ALTERROLEprojectuserSETtimezoneTO 'UTC'; GRANTALLPRIVILEGESONDATABASEnewprojectTO projectuser; // exit \qThe above are standard commands you probably have done already. Replace newproject and projectuser with actual names of your choice.
We entered the psql (PostgreSQL) shell and issued the commands to create the database, a new user and assign a password, set some settings on the created user which will be used for transactions. Anytime a transaction from this user goes to the DB, these settings will be attached.
Next, we assign the database to the user and exist.
Database setup wasdone!
Python Development EnvironmentIf you’ve not heard of VirtualEnv, then please go ask Google for more details. In short, it allows you to run your Python projects within encapsulated environments, such that, what packages or dependencies you install in project A, doesn’t interfere with project B.
We’ll be going an extra step in using VirtualenvWrapper to make development slightly easier.
So from the official docs, we do this:
sudopipinstallvirtualenvvirtualenvwrapperAfter install, we now have our virtualenv and its wrapper ready. To start making use of the commands given us by virtualenvnwrapper , you could add this to end ofthe file, ~/.bashrc
exportWORKON_HOME=/home/django/.virtualenvs source /usr/local/bin/virtualenvwrapper.shThen, on the terminal, enter:
source ~/.bashrc
To reload your bash, giving you access to all the new commands from virtualenvwrapper
Setup the environment for theproject with virtualenvwrapper mkvirtualenvmyProject workonmyProjectto create a new virtual environment called myProject . Notice where the project is initialized. Mine is at /home/khophi/.virtualenvs/myProject/bin/python .
After entering the workon myProject , your terminal should change to something like this:
(myProject)user@host:~/Then do the next:
(myProject) $ cd ~/ (myProject) $ installdjangogunicornpsycopg2 (myProject) $ django-admin.pystartprojectmyApp (myProject) $ cdmyApp/ Into Django’s RealmNow that we have our Django project started, in the name of myApp and we’re in the project’s root directory, let’s go ahead to make some changes to the settings file to point to our PostgreSQL database.
Using nano, open the Django settings file of the myApp project, like so: (myProject) myApp/ $ nano myApp/settings.py
Then, in there:
..... DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'newproject', 'USER': 'projectuser', 'PASSWORD': 'password', 'HOST': 'localhost', 'PORT': '', } } .... STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static/') .....With that out of the way, we need to test if our database connection is working.
To serve static files in production, you need to have your STATIC_URL and STATIC_ROOT set properly. In the above, we’re saying, ‘Look into the static/ folder within the root of the current project’.
Thus, whenever you do, {% static 'my/image/is.jpg'%} you’re saying, mywebsite.com/static/my/image/is.jpg which will be intercepted by our Nginx which is configured to serve files coming, having a starting URL of /static/ with the contents from the os.path.join(BASE_DIR, 'static/') , which in our case, is ~/myProject/static/
Testing database connection:
(myProject) $ cd ~/myApp (myProject) myApp $ ./manage.pymakemigrations (myProject) myApp $ ./manage.pymigrate (myProject) myApp $ ./manage.pycreatesuperuser (myProject) myApp $./manage.pycollectstatic --no-inputThe above are standard Django commands, you already know. Since we wish to use Nginx to serve the static files. So, let us ‘taste’ the fruits of our work now:
(myProject) myApp/ $ ./manage.pyrunserver 0.0.0.0:8000Assuming you don’t have firewall blocking the outside world in accessing your site, (of which you can remove by doing sudo ufw allow 8000 ), then visit
yourIpAddress:8000 in your browser, and you should see a Django ‘it worked’ page like so: