Wednesday, August 27, 2014

Install the high performance Nginx web server on Ubuntu

Look out Apache, there's a web server -- Nginx (pronounced Engine X) -- that means to dismantle you as the defacto standard web server. The Nginx project started development in 2002, but it's just now really showing its strength and starting to gain a serious following. Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server, and it's known for high performance, stability, a vast feature set, easy configuration, and very low resource consumption.
Since Nginx is new to many users, I thought I should begin a series of articles to get everyone up to speed on using this fantastic web server. Throughout this series, I'll be using the Ubuntu platform. You can use this series as the basis for other platforms -- just pay close attention to the variances of installation technology and (in some cases) directory structure. With that said, let's install Nginx on the Ubuntu platform.
Note: As this series won't bother with basic platform setup and security, I'll assume that you already have the server up and running and secure to your liking. I'll also assume that you have MySQL up and running (along with PHP support).


Nginx has few dependencies that Ubuntu doesn't already take care of. Here's what you'll need:
  • The Gzip modules requires the zlib library
  • The rewrite module requires the pcre library
  • SSL support requires the openssl library
With the Ubuntu platform, the only library from the list above that you'll need to install is the pcre library. To install this, follow these steps:
  1. Open a terminal window
  2. Issue the command:
    sudo apt-get install libpcre3-dev
  3. Type your sudo password and hit Enter
  4. Accept the installation
  5. Allow the installation to complete
Nginx can use PHP5 with the help of PHP-FPM (an alternative PHP FastCGI implementation with additional features, which is useful to busier sites). Here's how to install PHP-FPM:
  1. Open a terminal window
  2. Issue the command:
    sudo apt-get install php5-fpm
  3. Type your sudo password (if prompted) and hit Enter
  4. Accept the installation
  5. Allow the installation to complete

Installing Nginx

As you might expect, the installation of Nginx is quite simple. Just follow these steps:
  1. Open a terminal window
  2. Issue the command:
    sudo apt-get install nginx
  3. Type your sudo password (if prompted) and hit Enter
  4. Accept the installation
  5. Allow the installation to complete
That's it. The new web server is installed. Now, let's peek around a bit.

Starting and stopping the server

To start the Nginx server, issue the following command:
sudo service nginx start
Top stop the Nginx server, issue the following command:
sudo service nginx stop
With the Nginx service started, fire up your browser and point it to the IP address (or domain) of the hosting server. If you see the “Welcome to Nginx” page, everything is good to go.
Before you close that terminal window, it's necessary to set the Nginx service to start at boot. Just issue the following command:
update-rc.d nginx defaults
If you see:
System start/stop links for /etc/init.d/nginx already exist
That means Nginx is already set to start at boot time. You can now rest assured that you won't have to manually start your web server upon reboot.

Configuration files/folders

  • The main configuration file for Nginx is /etc/nginx/nginx.conf
  • Virtual hosts are defined in /etc/nginx/sites-available/default
  • PHP will be configured in /etc/php5/fpm/php.ini

That should do it for Nginx installation on the Ubuntu platform. In upcoming posts, we'll start working through various configurations and optimizations to round out the series. Enjoy your new web server!

Nginx and Android: A great on-the-go web dev tool

There are times when you just need to develop on the go. When this happens, you might not want to carry around that bulky laptop -- or maybe your only option is a tablet or smartphone. If that's the case, and you have an Android device handy, you're in luck! The Nginx(pronounced engine-x) web server is great way to have a portable web server for testing, developing, and even serving up web pages.
NAMP (nginx android web server) is a 10-day trial app (after the trial, the cost of a license is $0.99 until Sept 1st, 2014, after which the price will raise to $4.99). Here are some of the app features:
  • Nginx v1.5.0
  • PHP v5.4.13
  • MySQL v5.1.62
  • msmtp 1.4.30
  • NAMPFTP v1.0
  • Export MySQL backups to Dropbox
  • Export backups of sites to Dropbox
  • Backup of MySQL databases
  • Easy management of virtual hosts directly from the application
  • Add FTP users with custom privileges
  • Run the server on port 80 or 8080
You can connect to the server either locally, on the Wi-Fi LAN, or from a WAN address. With the addition of a solid text editor (such as Jota+), your Android device will become a powerful web-development tool.
Let's install and use NAMP.


Installation of NAMP is quite simple. Just follow these steps:
  1. Open the Google Play Store on your Android device
  2. Search for NAMP
  3. Locate and tap the entry by NAMP Ltd.
  4. Tap Install
  5. Read the permissions listing
  6. If the permissions listing is acceptable, tap Accept
Once the installation is complete, you'll see an icon for NAMP on your home screen (or in your app drawer -- or both). Tap that, and you'll be presented with the licensing information (plus information about when your trial period will expire). Once all of that information is gone, you'll see that the various services are started and the addresses to reach the default web page (Figure A).
Figure A
Figure A
NGINX running on a Verizon-branded LG G Pad.


If you tap the Status button (upper left corner of the NAMP main window), you gain access to each of the services installed. Tap on the nginx entry, and you can start/stop the service, edit the configuration file, and even add virtual hosts (Figure B).
Figure B
Figure B
The nginx service control panel.
If you tap the Settings entry (from the Settings menu), you can set NAMP to autostart on boot, enable an external .ini file, allow the usage of Root, and more (Figure C).
Figure C
Figure C
The NAMP Settings window.
The most important thing you'll need to know is where to store your files. Open up your file manager (if you don't have one, I suggest Astro File Manager) and look for a folder called htdocs. Within that folder is where you'll place all of your html and other source files.

Database management

If you're wanting to work with databases, NAMP has you covered. There is a special version of phpMyAdmin you can run with NAMP. Here's how you do it:
  1. From the NAMP main window, tap the Settings button
  2. Tap the Tools entry
  3. Tap phpMyAdmin
  4. Allow phpMyAdmin to download
  5. Tap OK when prompted (this will restart the nginx services)
  6. Tap phpMyAdmin again (this will open the web browser to localhost:8000)
By default, the MySQL username is root and the password is blank (as in no password). At this point, you should be able to use phpMyAdmin as you would if it were running on a standard server.
If you need to edit the MySQL configuration file, you can do so by selecting MySQL from the main window Settings menu and tapping the EDIT button (Figure D).
Figure D
Figure D
The MySQL window within the NAMP server.
If you're looking for an easy way to work with web development on the go, you should give NAMP and nginx a try. Outside of logging into a remote server to work on your web development, this might well be your best bet.