Tech and Media Labs
This site uses cookies to improve the user experience.
Ubuntu as Web Server
  1. Ubuntu Linux as Web Server




Ubuntu Linux as Web Server

Jakob Jenkov
Last update: 2015-08-26

If you have a web application you need it to run on a web server. Sometimes you prefer running that web application on its own server. In this text I will quickly explain how to set up an Ubuntu Linux server so that it can be used as a web server.

Getting a Server

First of all, you need to get a server on which you can install Ubuntu. You can either buy your own server, rent a server, or rent a virtual server. Virtual servers are cheapest to get started, but if you need lots of performance, it is cheaper to rent a physical server.

You can get a cheap, virtual server for as little as $5 per month from https://www.digitalocean.com, or from $10 per month from https://www.linode.com . I have rented a $5 virtual server from Digital Ocean. It was very easy to choose a server with Ubuntu, and the server was up and running within a few minutes. The $5 server is fine for developing and testing a web app. You can always scale up later.

If you need a physical server you can rent some pretty powerful servers from http://www.hetzner.de. Physical server pricing starts around €49 per month.

Installing the Firewall

Before you can open / enable the firewall, you must first install ufw (Uncomplicated FireWall). If ufw is not installed on your Ubuntu system you can install it using the following commands:

$ apt-get update
$ apt-get install ufw

Opening the Firewall

For browsers to be able to access the web application, you need to open port 80 in the firewall on the server. Port 80 is the default TCP port for HTTP traffic. If you plan on using SSL with your website, you will also need to open port 443 which is the default port for HTTPS traffic.

You can open ports in the firewall using the ufw (Uncomplicated Firewall) command. First you must enable ufw. This in a command prompt:

ufw enable

After enabling ufw, open port 22. Port 22 is where SSH is running on. Otherwise you cannot access the server with SSH once the firewall is running! (yes, I have made that mistake, and I had to reinstall Ubuntu - luckily all that was done automatically for me by our server provider). Here is how to open port 22:

ufw allow 22

Second, open port 80 using the ufw command. Here is how you do that:

ufw allow 80

To open port 443 (HTTPS) you execute this command:

ufw allow 443

In case you need to close a port again, you can execute this command:

ufw deny 443

This example closes port 443.

You can also just delete a firewall rule like this:

ufw delete deny 443

Installing Nginx

Nginx is a high performance web server. Nginx can be used as a standalone web server, or as "front" or "reverse proxy" for other web servers. For instance, you can run Nginx on port 80, and then have different domains and URLs forwarded to one or more web servers on the back (e.g. Apache on port 8000, Jetty on port 8080 etc.).

When running as a reverse proxy for other web servers, Nginx can handle HTTP aspects like SSL (HTTPS), GZipping the HTTP response body (the content sent back from the web server on the back of Nginx), inserting HTTP cache headers into the HTTP response etc. You can also configure Nginx to perform load balancing. The web servers on the back of Nginx then do not need handle any of these aspects.

The advantage of using Nginx in front of other web servers is that you only need to learn how to configure SSL, Gzip, cache HTTP headers, load balancing etc. for one web server - Nginx. You don't have to learn how configure that for the various web servers used behind Nginx. This makes it a lot easier to use multiple different web servers, or change web servers on the back of Nginx.

I have a separate tutorial about installing and configuring Nginx.

Installing Java

If you need to run a Java web server on Ubuntu like Jetty, Tomcat, Glassfish, JBoss etc. you will need to install Java. You can typically install Java via apt-get command, but I prefer to download Java for Linux from Oracle, and unzip it on the server. Remember, these Java web servers typically need the full Java SDK, not just the JRE.

First you need to download Java. You can do so from Oracle's website: http://www.oracle.com/technetwork/java/javase/downloads/index.html . I usually download the .tar.gz version of the Java SDK.

Second you need to copy the Java SDK to you Ubuntu server. You can so do using WinSCP which is a FTP-like program with a graphical user interface which can copy files to and from a unix server via SCP / SSH (so you don't need an FTP server installed on your Ubuntu server).

Third you need to unzip and untar the Java SDK. You can do so using this command:

tar -zxvf file

Replace the file part with the name of the Java SDK file (with the .tar.gz ending).

Installing a Java Web Server

If you plan to use a Java web server on your Ubuntu Linux server, you will need to install that Java web server after you have installed Java. Installing and configuring Jetty, Tomcat, Glassfish, JBoss etc. is outside the scope of this text, but you can find lots of information about each of these web servers online. Here are some useful links to get you started:

Jetty
Tomcat
Glassfish
JBoss Wildfly

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC