If you are unfamiliar with Unix/Linux commands, work through this Unix tutorial. For general information about FreeBSD, The FreeBSD Handbook is usually the best place to look. For information about a specific command,
man is often the most informative.
It's generally considered good form to log in from a user account, and
sudo (or at least
su) into root. Here, we're essentially setting up a single-user system that is only running tasks that need to be configured by root, so the value of an additional user account is questionable. It's probably best to work from a user account, if only to develop good habits, but you will be working as root user almost exclusively. If you want
sudo, it has to be installed separately.
The first thing is to get all the available security patches and apply them:
# freebsd-update fetch
A list of the patches that have been downloaded will be returned. To install them, run:
# freebsd-update install
Now that the system is up to date, install the package management tool by typing
We'll need to build some of our packages from source, so update your ports tree, too.
Install A Web Server
I really like nginx (pronounced 'engine-x'), and use it whenever possible as my web server. Apache is more popular and has lots of bells and whistles, but I've always found nginx to be a lot more intuitive to configure. It's known to be lightweight and excel at high loads, so if any pages were to do well on Reddit, they would likely stay up longer than if I was running Apache. The primary reason I like nginx, though, is that I have found it to be the easiest of the two to get configured.
That being said, if you'd rather run Apache, it's the most common server, so you should have no trouble finding advice or support. I'm only covering nginx for now:
# pkg install nginx
Python 3 is the recommended language for new Python projects, but FreeBSD doesn't have a
python3 binary package available yet, so we'll need to compile it ourselves. If you prefer, you can install Python 3 with
pkg, but you won't get the
python3 executable, so you'll have to link it yourself. Building from source is frequently required, though, so this seems like as good a time as any to get started:
# cd /usr/ports/lang/python3 # make all install clean
Virtual Environments and FreeBSD
If we were using Linux, you might want to install a Python Virtual Environment.
pip for Python 3 doesn't work on FreeBSD, though, so setting up a virtual environment is kind of pointless since you have to use the system libraries anyway. I haven't had any problems when copying virtual environments from Linux to FreeBSD, but I usually prefer to use Virtual Machines where possible.
If you do want to run a virtual environment in spite of those limitations, there are a few caveats for FreeBSD:
pyvenv command, so you'll have to use
$ python3 -m venv ~/project-name
When you try to activate your environment in csh, you'll get an error.
source bin/activate needs to be run from bash, so you'll have to install it:
# pkg install bash
Now run bash as your user:
You should now see your working directory before your cursor, indicating you're in bash. You should be able to activate your virtual environment now:
$ cd ~/project-name $ source bin/activate
Install A Database Server
psycopg2 is the most popular interface to Postgres for Python. Since FreeBSD uses Python 2.7 by default, you can't just use
pkg to install any Python 3 modules.
In order to get a module that will work with Python 3, it has to be built from source using
make will also build for 2.7 by default, but by adding the PYTHON_VERSION variable, the right module will be built. The
python3 port that was installed in the last section used Python 3.3 in my case:
# cd /usr/ports/databases/py-psycopg2 # make PYTHON_VERSION=3.3 all install clean
Psycopg will install a postgres-client package. You should install the -server of that version. You can tell what's installed using #pkg info | grep postgresql
I see that my system returns
so I install
# pkg install postgresql90-server
Django is a "web framework". Web frameworks usually contain at least three components:
- URL Handler
- Templating Engine
- Database Interface
A URL handler takes the information that is typed into the address bar of your computer and routes it to the right place. Sometimes there might be data to be extracted from URL parameters. YouTube is a good example. In the URL
https://www.youtube.com/watch?v=hRubq5D-3kM, everything after
www.youtube.com/ is being processed by an application on YouTube's servers.
watch is the name of the command it's sending the server,
? signifies the barrier between command and data, and
v=hRubq5D-3kM specifies the video. Different URL parameters are usually separated by an ampersand (&).
A templating engine lets you build parts of your website separately, so you only have one copy of the header or sidebar of your site to edit if you want to change something. It also means there is only one place for a bug to occur, so it's a lot safer, too.
A database interface makes it so you only have to learn Python, and don't have to worry about writing SQL.
Other Nice Things
Django also has an admin interface, so you can edit your web app from your browser instead of your text editor.
It's also used for some of the largest scale sites around: Pinterest, Instagram, and Disqus all use Django, so if you ever hit the big time and write a billion dollar app, it will be easy (easier) to scale.
Django is also a Python package, so it will have to be built from source:
# cd /usr/ports/www/py-django # make PYTHON_VERSION=3.3 all install clean
# cd /usr/ports/www/uwsgi # make PYTHON_VERSION=3.3 all install clean
Git helps you keep track of where you broke your code, and lets you go back to a point where it was working. Install it:
# pkg install git
Create a new directory to house your project and initialize a git repository there:
$ cd $ mkdir project-name $ cd project-name $ git init