Part 1 of 2: How to setup a Debian based Django web development server. This tutorial is the exact instructions I am using in my development environment.
Why do I want to learn Django? Because I have a few ideas of some dynamic, database driven websites I want to launch in the near future and Django is known for it’s rapid development workflow.
Not long ago, I started learning the basics of Python. I feel I know enough to be dangerous. So, I started diving into Django web-framework. The early stages of my Django learning were pretty tough. I couldn’t wrap my head around a lot of the basics.
I’m at the point where I feel I have a handle on the basics. This article is going to show how I setup and configured a bare-bones debian server to be my Django web development environment. I have rebuilt my server at least 5 times during this learning phase so I can provide you the most accurate guide and information possible.
One quick note I would like to add is the fact that we are not using any type of version control in this article since it’s out of the scope of this tutorial. I do recommend you learn and use version control.
This Article’s Goals
- Setup and configure a bare bones Debian server.
- Install some basic utilities.
- Configure Sudo
- Remotely manage the server using SSH.
- Install PostgreSQL or MySQL database.
- Configure PostgreSQL or MySQL database.
- Install PiP.
- Install and configure Virtualenv.
1. Install the OS
Follow along with that article but make sure to select the “Standard System Utilities” option as in the screenshot below:
Also a quick tip for configuring the networking settings during the install:
- If it’s a physical server, unplug the network cable before beginning the setup.
- If it’s a virtual machine, go into the networking settings and uncheck the cable connected option.
This will bring up an option to manually configure the networking settings during the install. Then after manually configuring the settings, you can plug the cable back in if it’s a physical machine, or check the cable connected option within the VM settings.
2. Install Some Basic Utilities
These are a few of my favorite Linux utilities that I like to install.
Now that we have our base system ready, let’s review what utilities I like installing and why.
- openssh-server: So I can remotely connect to this server from my laptop.
- screen: Use multiple windows with one SSH session. Also save us from disconnection disasters.
- tree: Nice visual tree representation of a folder structure. I like this tool while learning Django since there is a lot happening with many directories and files.
- vim: By default Vim is already installed but only a very basic version. This will give us more functionality within Vim.
- sudo: Give us the ability to run root commands via sudo rather than logging in as root.
Log into the server as the non-root account, then switch user to root:
Re-synchronize the package index files from their sources and upgrade and then install new packages currently installed on the system:
apt-get update && apt-get upgrade
Install the utilities:
apt-get install openssh-server screen curl tree vim sudo
cp /usr/share/vim/vim73/vimrc_example.vim ~/.vimrc
3. Configure Sudo (Sudoers)
We need to configure sudo to allow our non-root account root access on-the-fly rather than having to switch the user account to root.
While still logged-in as root, run the following command to add your user account to the sudo group:
substitute cdavis for your user account
usermod -aG sudo cdavis
Log all the way out of the server (both root and non-root accounts) by typing exit then enter twice. You want to be at the login prompt.
4. Remotely Connect Using SSH
Now that we are logged completely out of the server, let’s remotely manage the server from the comfort of our desktop or laptop using SSH.
To read my quick tip on connecting to the Debian Linux server using SSH from either Windows 8.1 or Mac OS X, check out this article.
After you successfully connect to the server over SSH, run screen in order to start a screen session (in case we get disconnected from the server we will be able to reconnect to that session later.)
If you want to learn more about screen, check out this resource.
5. Install Database
Depending on what database you want to use, here I will give you two options:
sudo apt-get install libpq-dev python-dev
sudo apt-get install postgresql postgresql-contrib
Now let’s create a new database for our project:
sudo apt-get install mysql-server
6. Configure Database
Now let’s setup a new database and database user:
sudo su – postgres
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
If MySQL isn’t running, start up the MySQL server:
Login to MySQL:
mysql -u root -p
Create the database:
CREATE DATABASE django_db;
7. Install PIP
PIP is a tool for assisting with the installation and management of Python packages.
There are a couple ways of installing PIP. The way I am going to demonstrate will be to use the built-in package manager.
sudo apt-get install python-pip
Finally let’s create a directory where we are going to work out of during development:
8. Install Virtualenvwrapper
Virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool.
- Virtualenvwrapper will automatically install Virtualenv.
- Virtualenv will allow you to install Python packages in isolated environments.
- Virtualenvwrapper will allow us to easily work on more than one project at a time.
sudo pip install virtualenvwrapper
After virtualenvwrapper is installed, we need to add a few lines to our shell’s startup file.
On my system, I am using bash.
substitute .bashrc for whatever shell you are using. Also use whatever editor you are most comfortable with. I like Vim.
Add the following lines to the bottom of the file:
substitute dev for whatever folder you are using for your development work
Reload your terminal session:
substitute .bashrc for whatever shell you are using.
Check out part 2 (coming soon) of this tutorial where I will guide you through the following:
- Create a new virtual environment (aka sandbox).
- Install Django.
- Start a project.
- Launch and test our development server