How to Deploy a Django Application on a Server Running Ubuntu

This post provides tutorials on how to deploy a Django application on a server running Ubuntu.

Softwares needed to set up a server for Django application:

  • Virtualenv & Virtualenvwrapper / Conda
  • Django
  • Gunicorn
  • Nginx
  • Supervisor

Concept

Nginx will face the outside world. It will serve media files (images, CSS, etc.) directly from the file system. However, it can’t talk directly to Django applications; it needs something that will run the application, feed it requests from the web, and return responses.

That’s Gunicorn‘s job. Gunicorn will create a Unix socket, and serve responses to Nginx via the wsgi protocol – the socket passes data in both directions:

The outside world <-> Nginx <-> The socket <-> Gunicorn

All this family will live into a Virtualenv. Already wondered why Virtualenv is so useful when you develop Pythons’s applications? Continue to read and you will understand.

Before you start

References

I assume you have a server available on which you have root privileges. I am using a server running Debian 7, so everything here should also work on an Ubuntu server or other Debian-based distribution. If you’re using an RPM-based distro (such as CentOS), you will need to replace the aptitude commands by their yum counterparts and if you’re using FreeBSD you can install the components from ports.

Apache and Nginx together on Ubuntu

Is it possible to run  Nginx and Apache at the same time on the same machine?

The answer is YES.

This post provides instructions on how to configure Apache and Nginx  to work together on the same machine running Ubuntu.

Overview

Nginx and Apache are great and powerful web servers. However, they both have drawbacks; Apache uses up server memory while Nginx (best used for static files) require the help of php-fpm to process dynamic content.

Nginx is an excellent lightweight web server designed to serve high traffic while Apache is another popular web server serving more than half of all active websites in the world. One can combine the two web servers to significant effect, with Nginx serving as static web server front and while Apache is processing the back end. So let’s look into how to configure your Nginx to work with Apache side by side.

Configuring Apache and Nginx

Apache and Nginx can definitely run simultaneously. The default config will not allow them to start at the same time because they will both try to listen on the same port and the same IP.However, you can easily either change the ports or ports and IPs, or IPs. There are various ways to make them run either one behind the other(usually Apache behind Nginx because Nginx will be the first entry point in the chain since it’s faster for static resources, and Apache will then only be triggered for some advanced dynamic rendering/processing) or just side by side.

Set different ports for each server. That means you can leave port 80 for Nginx and assign Apache a different port.

Nginx

Install Nginx

Install and configure Nginx that will serve as the front end of your site.

$ sudo apt-get install nginx

Once it has downloaded, configure the virtual host to run on the front end. However, a few changes are required on the configuration file.

Configure Nginx

Open up the nginx configuration file

$ sudo nano /etc/nginx/sites-available/example

For example, you could tell apache to listen on 127.0.0.1:8080 and instruct Nginx to reverse –proxy traffic to Apache while still serving static content.

Those text in blue is where you should edit according to your server info.

 server {
listen 127.0.0.1:80;
server_name some.name another.dname;
access_log /var/log/nginx/something-access.log;
location / {
proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^.+\.(jpg|js|jpeg|png)$ {
root /some/where/on/your/disks;
}
# put your static hosting config here.
}

Activate the virtual host.

$ sudo ln -s /etc/nginx/sites-available/example /etc/nginx/sites-enabled/example

Delete the default Nginx server block.

$ sudo rm /etc/nginx/sites-enabled/default

Apache

Install Apache

Install the backend which is Apache

$ sudo apt-get install apache2

Apache starts running on port 80 as Nginx is not started.Let’s make Apache listen on a different port so that they can work together.

Configuring Apache

Open Apache ports.conf file using the below command

$ sudo nano /etc/apache2/ports.conf

Look for the following line;

Listen 127.0.0.1:80

and  change it to

Listen 127.0.0.1:8080

Save and Exit.

Next, edit the default virtual host file in Apache.

The <VirtualHost> in this file is set to serve sites only on port 80

$ sudo nano /etc/apache2/sites-available/000-default.conf

Look for the following line,

<VirtualHost 127.0.0.1:80>

then, change it to;

<VirtualHost 127.0.0.1:8080>

Save the file and reload Apache.

$ sudo service apache2 reload

Verify that Apache is now listening on 8080.

$ sudo netstat -tlpn

The output is shown below, with apache2 listening on :::8080.

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1086/sshd
tcp6 0 0 :::8080 :::* LISTEN 4678/apache2

Nginx is configured and running as the front web server on port while Apache is configured to run on the backend on port 8080. Nginx redirects proxy traffic as it still serves static content.

Conclusion

The most important thing we take from this simple configuration is that Apache and Nginx can and do work together. A problem may arise when they both listen to the same ports. By giving them different ports to listen to, your system functionality is assured.

References

 

 

 

Dynamic GPU usage monitoring (CUDA)

To dynamically  monitor NVIDIA GPU usage, here I introduce two methods:

method 1: use nvidia-smi

in your terminal, issue the following command:

$ watch -n 1 nvidia-smi

It will continually update the gpu usage info (every second, you can change the 1 to 2 or the time interval you want the usage info to be updated).

method 2: use the open source monitoring program glances with its GPU monitoring plugin

in your terminal, issue the following command to install glances with its GPU monitoring plugin

$ sudo pip install glances[gpu]

to launch it, in your terminal, issue the following command:

 $ sudo glances

Then you should see your GPU usage etc. It also monitors the CPU, disk IO, disk space, network, and a few other things

For more commonly used Linux commands, check my other posts at here  and here .

Install and use htop on Ubuntu 16.04 Desktop and Server

This post introduces an interactive tool for visually monitoring the memory and process usages of your Ubuntu 16.04 Desktop or Server in real time.

  • What is Htop?

Htop is an interactive system-monitor process-viewer and process-manager. It is designed as an alternative to the Unix program top. It shows a frequently updated list of the processes running on a computer, normally ordered by the amount of CPU usage. Unlike top, htop provides a full list of processes running, instead of the top resource-consuming processes. Htop uses color and gives visual information about processor, swap and memory status.

  • Install Htop on Ubuntu 16.04 LTS Desktop and Server

(This works on both an Ubuntu 16.04 Desktop and  Server.)

Installing htop package on Ubuntu 16.04 (Xenial Xerus) is as easy as running the following command on terminal:

Step 1. First make sure that all your system packages are up-to-date by running these following apt-get commands in the terminal.

$ sudo apt-get update

Step 2. Installing Htop. Install htop process monitoring tool using apt-get command:

$ sudo apt-get install htop
  • Use Htop to monitor your Ubuntu 16.04 LTS Desktop and Server in real-time

Now that htop is installed on your server you’ll want to start the program by running the following in a command prompt:

$ htop

This will open the program and you’ll see something similar to the following:

Leave this terminal open, you can use CTRL + ALT + T  to open another new terminal for your other work. Then htop will help you monitor your memory usage in real-time:)  Enjoy!

tmux resources

This post provides a brief introduction to tmux and some commonly used commands and useful resources about tmux.

(Thanks Davide for recommending such a handy tool to me.)

(Stay tuned — I will update this post while I am gaining new skills about tmux.)

======What is tmux?

According to the tmux authors:

tmux is a terminal multiplexer. What is a terminal multiplexer? It lets you switch easily between several programs in one terminal, detach them (they keep running in the background) and reattach them to a different terminal. And do a lot more.

 

====== tmux sessions, windows, and panes explained

One of these features is the ability to break your session into more discreet components, called windows and panes. These are good for organizing multiple varied activities in a logical way.

Let’s look at how they relate to each other.

Nesting

tmuxnesting

tmux sessions have windows, and windows have panes. Below you can see how how they are conceptualized:

  • Sessions are for an overall theme, such as work, or experimentation, or sysadmin.
  • Windows are for projects within that theme. So perhaps within your experimentation session you have a window titled noderestapi, and one titled lua sample.
  • Panes are for views within your current project. So within your sysadmin session, which has a logs window, you may have a few panes for access logs, error logs, and system logs.

It’s also possible to create panes within a session without first creating a separate window. I do this sometimes. Hopefully it isn’t as horrible as it sounds right after reading about nesting.

Q about differences between sessions and windows and panes:

Can someone explain how they use sessions and windows and panes?
It feels like one too many levels, why both sessions AND windows?
I’m willing to believe there is a use case, but for the life of me I can’t come up with one.

A: 

I use sessions for different projects, and windows and panes within a project. For example, I’ll have a session for my puppet code, and a session for a bash script I’m working on. Within the puppet session, if I’m going to work on a new module, I open a new window. If I’m writing code in vim, I usually split off a pane so I can test running it next to it. Having the error output next to the code makes debugging really fast.

In a lot of ways, it basically acts like a tiling window manager for the terminal. The advantage over a window manager is that the whole layout can be accessed remotely. So if I’m working on a project from work, I can quickly resume working on the project from my home computer after SSH’ing in and reattaching to the tmux session.

 

====== see below for commonly used tmux comands

By default, tmux uses Ctrl-b as its shortcut activation chord, which enables you perform a number of functions quickly.

  • Switch between different windows

Ctrl – b (presee the  ctrl and b keys at the same time and release them at the same time, and then immediately press window number)  window number

  • Cursor move within an active window

Ctrl-b (release the keys, and immediately press the “[” key ) the key with “[“.

when you see the cursor becomes a solid flashing diamond, you can use arrow key to move your cursor in the active terminal window.

Note: Press “q” to exit the cursor move mode.

  • Session management
  • s list sessions
  • $ rename the current session # default session name and window name is number, can rename session names to meaningful ones
  • d detach from the current session

tmux is developed on a client-server model which means that the session is stored on the server and persist beyond ssh logout.

The following command will create a new session called mysession:

tmux new-session -s mysession

To attach to a session run:

tmux attach -t mysession

To list all session run:

tmux ls

You can kill a session using the following command:

tmux kill-session -t mysession

you can grossly kill all tmux processes with the following command:

pkill -f tmux

Frequently used sessions commands

Ctrl-b d	  Detach from the current session 
Ctrl-b (          Go to previous session
Ctrl-b )          Go to next session
Ctrl-b L          Go to previously used session
Ctrl-b s          Choose a session from the sessions list

Ctrl + D    — exit tmux from terminal.

  • Windows (tabs) Management

Each session can have multiple windows. By default all windows are numbered starting from zero.

Frequently used windows (tabs) commands

Ctrl-b 1  Switch to window 1
Ctrl-b c  Create new window
Ctrl-b w  List all windows
Ctrl-b n  Go to next window
Ctrl-b p  Go to previous window
Ctrl-b f  Find window
Ctrl-b ,  Name window
Ctrl-b w  Choose a window from the windows list
Ctrl-b &  Kill the current window

 

  • One of the handy things about tmux is how easy it is to resize panes:

Ctrl +b, followed by holding down Alt, and using the arrow keys to resize.

 

======things about how to save sessions and recover sessions.

If you reboot you computer you will lose the sessions. Sessions cannot be saved. But, they can be scripted. What most do in fact is to script some sessions so that you can re-create them.

======Installing tmux

  • Installation with sudo privilege

Installation is pretty straightforward if you have Ubuntu or any other Debian-based distribution you can install tmux with:

sudo apt-get install tmux

on CentOS/Fedora:

yum install tmux

and on MacOS:

brew install tmux

Note: check the comments for some up-to-date scripts.

 

References and further reading list

This tutorial covers installation of tmux and some commonly used commands.

This is a pretty good introduction to tmux, it includes why tmux and the comaprision between tmux and screen, as well as some tmux shortcuts

 

 

 

Get the number of CPUs/cores in Linux from the command line

This post introduces how to find out the number of CPUs/cores on Linux machines from command line.

  • method 1:

$ nproc –all

20   # this means there are 20 cores on the linux machine.

  • method 2:

$ lscpu

CPU(s):                20

On-line CPU(s) list:   0-19

Thread(s) per core:    2

Core(s) per socket:    10

 

 

Install Node.js on Ubuntu 16.04 LTS

This post provides instructions about how to install Node.js on Ubuntu 16.04 LTS. See this post for Node.js resources. (Node.js Offical Github Repo.)

NPM (Node Package Manager) is the default package manager for the JavaScript runtime environment Node.js. NPM hosts thousands of free node packages. In general, NPM is installed on your computer after you install Node.js.

There are several ways to install Node.js on Ubuntu:

  • Method #1 (our choice in this tutorial): Install Node.js with Node Version Manager (NVM) to manage multiple active Node.js versions

Using nvm, we can install multiple, self-contained versions of Node.js, which will allow us to control our environment, and get access to the newest versions of Node.js, but will also allow us to keep previous releases that our applications may depend on. (nvm is just like Virtualenv in Python, if you are familiar with it, which allows us to install multiple version of the same Python library into “virtual folders” by pip.)

This is the method we will cover later in this tutorial.

  • Method #2: Install the bundled Distro-Stable Version Node.js (version 4.2.6) – it is very simple to install, just one or two commands.

Ubuntu 16.04 contains a version of Node.js in its default repositories that can be used to easily provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is version 4.2.6. This will not be the latest version, but it should be quite stable, and should be sufficient for quick experimentation with the language.

This tutorial picked the Node Version Manager (nvm) based method, because it is much more flexible.

See below for the step by step instructions. (Check out the reading list below if you need the install instructions for other methods listed above.)

Step 0: (Before we get started) Remove old Node package to avoid conflicts

Open a terminal (Ctrl + Alt + T), and type the following command. 

$ dpkg --get-selections | grep node

# If it says install in the right column, Node is on your system:
#ax25-node                                       install

#node                                            install

Step 1: Install prerequisite packages

We’ll need to get the software packages from our Ubuntu repositories that will allow us to build source packages. The nvm script will leverage these tools to build the necessary components.

First, we need to make sure we have a C++ compiler. Open a terminal window (Ctrl + Alt + T) and install the build-essential and libssl-dev packages. By default, Ubuntu does not come with these tools — but they can be installed by the following commands.

$ sudo apt-get update

$ sudo apt-get install build-essential libssl-dev

Step 2: Install nvm

Once the prerequisite packages are installed, we can install and update NVM(Node Version Manager) using cURL. (Note: to get the latest installation version link, on the page scroll down to “Install script”.) 

$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.2/install.sh | bash

Inspect the installation script with nano:

$ nano install_nvm.sh

#Note that we DO NOT need to add anything in the opened nano text editor window. We just need to create the .sh file. 
# Use Ctrl+O to save the file, and then hit Enter, and then Ctrl +X to close the file.

Run the script with bash (Note that run the following command in your terminal):

$ bash install_nvm.sh

It will install the software into a sub-directory under our home directory at ~/.nvm. It will also add some necessary lines to our ~/.profile file in order to use it.

To have access to the nvm functionality, we need to source the ~/.profile file so that our current session knows about the changes:

$ source ~/.profile

Now that we have nvm installed, we can install isolated Node.js versions.

Step 3: Install  Node.js

The following command will tell us which versions of Node.js are available for us to install:

$ nvm ls-remote
Output
...
    v6.10.3 (Latest LTS: Boron)
 v7.0.0
 v7.1.0
 v7.2.0
 v7.2.1
 v7.3.0
 v7.4.0
 v7.5.0
 v7.6.0
 v7.7.0
 v7.7.1
 v7.7.2
 v7.7.3
 v7.7.4
 v7.8.0
 v7.9.0
 v7.10.0

The newest version when I write this post is v7.10.0. We can install it by the following command:

$ nvm install 7.10.0

By default, nvm will switch to use the most recently installed version. We can explicitly tell nvm to use the version we just installed by the following command:

$ nvm use 7.10.0

When we install Node.js using nvm, the executable is called node (NOT nodejs that you may see in other tutorials). We can check the currently used Node and npm version by the following commands:

$ node -v  
# OR 
$ node --version

# Output
# v7.10.0


$ npm -v
# OR 
$ npm --version

# output
# 4.2.0

Step 4: using nvm to manage different versions of installed Node.js 

If you have multiple Node.js versions, you can see what are installed by the following command:

$ nvm ls

To set a default Node.js version to be used in any new shell, use the alias default command

$ nvm alias default 7.10.0

# This version will be automatically selected when a new session spawns. You can also reference it by the alias like this:

$ nvm use default

To learn more  about the options available to use with nvm, run the following command in your terminal:

$ nvm help

Step 5: using npm to install Node.js modules

Each version of Node.js will keep track of its own packages and has npm available to manage these.

We can use npm to install packages to the Node.js project’s ./node_modules directory by using the normal format. For example, for the express module:

$ npm install express

If you’d like to install it globally (i.e., making it available to other projects using the same Node.js version), you can add the -g flag:

$ npm install -g express

This will install the package in:

~/.nvm/node_version/lib/node_modules/package_name

Note that installing globally will allow us to run the commands from the command line, but we will have to link the package within a project in order to use it in that project:

$ npm link express

 

References and further reading list:

This tutorial covers two methods:

Method #1: Install the bundled distro specif Node.js version 4.2.6

Method #2: Install the latest version of Node.js version 6.x or 7.x

This post is very good — it covers the following ways to install Node.js:

Installing using the Official Repository
Installing using the Github Source Code Clone
Installing using Node Version Manager (NVM)

It covers How to Install Multiple Nodejs version with NVM and also covers how to remove Node.js 

This tutorial covers:

  • How To Install the Distro-Stable Version for Ubuntu
  • How To Install Using a PPA
  • How To Install Using NVM

there are a quite a few ways to get up and running with Node.js on your Ubuntu 16.04 server. Your circumstances will dictate which of the above methods is the best idea for your circumstance. While the packaged version in Ubuntu’s repository is the easiest, the nvm method is definitely much more flexible.

This is a pretty good tutorial. It covers 4 Ways to Install Node.js on Ubuntu. There are several ways to do this, but The author of this post recommended  Option 1: Node Version Manager (nvm). Here is the full list of options:

 

 

Run Java program on terminal with external library JAR on Ubuntu

This post provides the instructions how to run a Java program from Terminal with external library JAR.

When using Eclipse to code Java program, which imports some external JAR library, we can use Eclipse to compile/build/run the program.

But if we would like to run our Java program that used External library Jars from Terminal, where should we put those JAR files, and how to build and run the program.

  • For compiling the java file having dependency on a jar
$ javac -cp /path/to/jar/file Myprogram.java
  • For executing the class file
$ java -cp .:/path/to/jar/file Myprogram

Note: cp in the above commands refers to classpath, which is a parameter in Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.

For example,  if your current working directory in terminal is src/report/

$ javac -cp src/external/myImportedJarfile.jar myJavaProgram.java

$ java -cp .:src/external/myImportedJarfile.jar myJavaProgram

If you have multiple jar files a.jar,b.jar and c.jar. To add them to classpath

$javac -cp .:a.jar:b.jar:c.jar HelloWorld.java

$java -cp .:a.jar:b.jar:c.jar HelloWorld

Note: on Windows, use “;” instead of “:”

Using Java 6 or later, the classpath option supports wildcards. Note the following:

  • Use straight quotes (")
  • Use *, not *.jar

Wild cards were introduced from Java 6. Class path entries can contain the basename wildcard character *, which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR.

java -cp "lib/*" -jar %MAINJAR%

If you need only specific jars, you will need to add them individually. The classpath string does not accept generic wildcards like Jar*, .jar, hiber etc.

Example

The following entry does not work:

java -cp "Halo.jar;lib/*.jar" ni.package.MainClass

Correct entry is :

java -cp "Halo.jar;lib/*" ni.package.MainClass

 

 

Install Ubuntu 16.04 on Oracle VirtualBox that runs on Windows or Mac

This post provides some notes and useful resources about installing Ubuntu 16.04 on Oracle VirtualBox that runs on your Mac or Windows.

Note: check the RAM and hard disk size of your machine before creating a virtual machine on it.

Notes about which version of Ubuntu to download and install:

For Ubuntu, it is not always a wise choice to pick the newest version. My suggestion is that (unless you are aware that you need to install a particular version), download and install the latest LTS (Long Term Support) version (see the picture below from Ubuntu wiki page). Every two years, a Ubuntu LTS version is released, which will be supported for updates for five years. For example, as of now, Ubuntu 16.04 LTS is the latest LTS version.

The two main things you need to pay attention to when you create a virtual machine:

  • Memory allocation for your virtual machine.

You can set it as half of your RAM (e.g., if your RAM is 8 G, set it as 4 G or 5G for your virtual machine should be fine.)

  • Storage type:  Select “Dynamically allocated” if you are not sure how large storage you actually will need.

There are already several very good tutorials about this along with snapshots, so I won’t create a tutorial for this. See below for some useful resources I collected. (See some notes I wrote below for some posts.)

My notes: This one is very good (with snapshots), including  Guest additions and Shared folders settings. (Note that Guest additions are required if you want to set Shared folder, so be sure to install Guest additions first).

You can use the following command to check whether Guest additions were installed on your Ubuntu virtual machine if you are not sure because you installed your Ubuntu VM a while ago. (Note: even though you may find Guest additions was installed, you will still need to install Guest additions for your newly installed VM, otherwise the Shared folders wont work for you.)

Use lsmod from the command line, as it will tell you not only if it’s installed, but properly loaded:

$ lsmod | grep vboxguest
vboxguest             282624  6 vboxsf

I have tested Shared folders instructions (with pictures) in this tutorial on my Ubuntu 16.04 VM, and it works. The only difference is that on Ubuntu 16.04 VM, after you issued the following command on your terminal and  restart the Ubuntu guest machine, you do not need to do anything as the tutorial said, the shared folder is automatically mounted each time you start you Ubuntu VM. (After you restart, click the Files icon on the task bar, and you will see the shared folder you just set just now is automatically mounted there:))

  • sudo adduser brb vboxsf   # Replace 'brb' with your account name on Ubuntu. 

One more note: Although Shared Folder setting in VM is very convenient, using VirtualBox shared folder directly for fastq data, annotation or output directory can significantly reduce the performance compared to a native (Ubuntu) system or VirtualBox native system, so my recommendation is only use the folder to transfer files between windows/mac and your Ubuntu VM.

P.S. If you see some tutorials tell you that you need to enter some command like “sudo mount -t vboxsf sharing /mnt/share” to automatically mount the shared folder each time you start your Ubuntu VM, that is outdated instructions.

Fortunately, new VirtualBox version (4.x +) has a (GUI) Auto-mount option (see pics below) when you set your shared folder. (Note that you can choose your customized folder to share, instead of using a system predefined folder such as Documents or Downloads.)

If you want to share the clipboard between your host and your virtual machine, check out the picture below.

 

Answers to some frequently asked questions:

Q: Do I need to backup my files when I upgrade my VirtualBox to newer version.

A: just install the latest version and you will have all your files in the new one. You need not have to uninstall the old virtual machine.

Q: After I install the updates of Windows 10, my VirtualBox won’t start…

A: just install the latest version and you will have all your files in the new one. You need not have to uninstall the old virtual machine.

 

My notes:  this one is very good (with snapshots) on Mac. My notes above about VM settings running on Windows work the same for VM settings running on Mac.

 

Compile a .java file on Linux

To compile a .java file on Linux (e.g., Ubuntu), first you need to install Java JDK on your computer. You can install it with the instructions at How do I install Java? , here and here.

Once you have java JDK installed, open your terminal and cd to the direcotry where your .java file located and type the following:

$ javac filename.java

To run the generated class file, use the following command

$ java filename