Tips about Jupyter Notebook

This post provides some tips about Jupyter Notebook.

  • When you open a pdf file in a browser via your running Jupyter Notebook.

You may encounter some error similar to this “Error! (file) is not UTF-8 encoded”,

the solution:

check the address in your browser, for example, for the following case,

http://localhost:8000/edit/confusionmatrix.pdf

if I change the edit in the url address given above to  view

http://localhost:8000/view/confusionmatrix.pdf

You will see your beautiful fig or document in pdf format.

  • Another similar situation:

When you open a html file in your browser via your running Jupyter Notebook, and you would like to edit the html file, instead of seeing its display,

the solution:

change view to edit in the url address of the file, see the following for an example, then you should be able to edit your html file.

http://localhost:8000/view/index.html 

to

http://localhost:8000/edit/index.html

Happy Jupyter Notebooking!

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