Web Programming and Applications

Lecture 11 - Web Server

Dr Steven Halim



Target Audience: Some of You

Some of you may never apply the techniques in this lecture as you do not have (or do not want to pay to have) access to a root (Administrator) account of a web server

But some others probably already have (e.g. because of Lab2) or will have such access, so this short lecture may contain some interesting knowledge for these students

Reason 1: Shared Droplet Issue

In Lab2, I have asked each group (a pair/triple at that time) to spin a Digital Ocean (DO) droplet using (the fortunately free GitHub Developer Pack) and to share it among yourself

But once you graduate from NUS, you will not have access to such education pack

How to make web applications that you build in CS3226 live for longer period beyond this semester?
(similarly for your future potential web application(s))

One valid answer: Set up and maintain your own web server
(Note: Not just about using a web hosting service out there)

Reason 2: Performance

Some of the Performance and/or Scalability tips mentioned in the previous lecture can only be applied if you have access to the root (Administrator) account of a web server

Otherwise you are basically tied to whatever hardware specifications and/or software libraries that are available in that web server, especially the server-side technologies...

Getting Your Own Web Server

There are two ways:

  1. Sacrifice (one of) your own personal computer (usually a dedicated desktop* computer), setup and run a web server on it 24/7, pay a huge electricity and Internet bill...
  2. You can purchase* cloud-based web (hosting) service
    1. Digital Ocean (the basis of this lecture note for now)
    2. Amazon Web Services (I haven't try, but promising)
    3. Google App Engine (I haven't try, but promising)
    4. IBM Bluemix (I haven't try)
    5. There are many others, search the Internet for more options (and more confusion)...

What's Next?

Once you have access to your own root (Administrator) account of a web server, you can set up the web server, upload your web application files and setup its settings, and let the web application run 24/7

But once a while, you have to maintain your web server as no one else will (or can) do it for you, failing which your web server (and thus your web application) performance will gets slower and slower and its security will get more and more compromised

Web Server Setup

Focus on LAMP Stack

LAMP = Linux, Apache, MySQL, and PHP

For this lecture note, we will not digress too much and talk about many other web server options out there that I have not tried (WA/IMP*, MEAN, etc)...

Digital Ocean* Droplet

Some of you have done this in Lab2:
spinning up a new DO droplet

Things to be considered:

  1. Choice of Linux distro (Ubuntu, Debian, CentOS, Fedora, etc...) or from a software bundle (this will save you time from installing the "AMP" component yourself) — unfortunately there is no Laravel one-click-app yet...
  2. How much do you want to pay per month (or per hour)
  3. Data center region (where your main* web application visitors are geographically located)...

What's Next?

You can now host your web application by copying/uploading your web application files to a certain designated document directory (depends on your Apache setting, the default for Ubuntu 16.04 LTS is /var/www/html)

The index.html (or index.php) stored in that directory is is the one that will be served when you entered the IP address of your Digital Ocean's droplet in a web browser, e.g. visit (we will talk about Domain Name registration in the next lecture)

Maintenance - Linux Specific

Every year, there are various (new) Linux distro released to public, e.g. the 2016 version

Upgrading the OS (the "L" in the LAMP stack) will likely entail the need of upgrading everything else (the "AMP"), so do such upgrade when there is a (critical) security upgrade (painful verbal story in class)...

As root, you can perform these tasks (not exhaustive):

  1. Set up cron jobs, e.g. periodic database backups, read this

Apache Specific

Likely the no 1 web server software as of 2017
(active sites metric, see this)

As root, you can perform these tasks (not exhaustive):

  1. Turn off automatic directory listing (a2dismod autoindex)
  2. Remove big/old access/error log or change log setting
  3. Turn on/off per-directory setting (.htaccess)
  4. Set Virtual Host: 1 web server, multiple websites/apps
  5. Set file caching options (especially for static files, example for setting up long cache expiry @ VisuAlgo)

MySQL Specific

As a root user, you have a root MySQL account that you should NOT directly use in your PHP (Laravel) script

With that MySQL root account, you can perform the following tasks (not exhaustive):

  1. Create other MySQL account(s) and granting only specific priviliges for that account(s)

PHP Specific

As root, you can perform these tasks (not exhaustive):

  1. Edit PHP configuration file (php.ini), e.g. display_errors, file upload settings

Upgrading the LAMP Stack

Sooner or later, the Linux distro, the Apache version, the PHP version, and/or the MySQL version that you have will get outdated

When a new version appears, you have to decide whether to upgrade and when to do so...

The End

All the best in setting up and maintaining your own (cloud-based) web server