Menu

install_ubuntu_nginx

Dirk van der Walt

Background

  • Nginx is a web server that is gaining a lot of popularity today.
  • It is fresh, lightweight, fast, scales well and is able to take a lot of load without overwhelming your system.
  • Nginx is one of those things that will cross any web developer's path sooner or later.
  • This section will cover the steps you have to go through to get RADIUSdesk working with a LEMP stack on Ubuntu 12.04
    • * A LEMP stack is one of those acronyms you can impress your friends with. It stands for Linux NginX MySQL and PHP.

What do we require

  • A standard Nginx install on Ubuntu is actually very simple.
  • The part that is more involved is to tweak Nginx to do the following:
Requirement Comment
Interpret PHP Scripts We would like the web server to call the PHP interpreter when a page ending with .php is requested.
Be able to have access to the MySQL functions of PHP Since we set up a LEMP server, we need to have a MySQL server installed and accessible from PHP.
Be as fast as possible To be fast, especially with serving PHP scripts, we can use a caching module in PHP which will speed things up.
Modify the expiry date of http headers to encourage caching We want files that should should not change (e.g. css or images) to be cached on the client's side to make the client's experience more pleasant
Compress text before they are served to the client We can compress the text that flows between the client and the server and in this way reduce the over the line bytes which in turn should also give the client a more pleasant experience
Enable rewrite rules in CakePHP for pretty URL's CakePHP makes use of the .htaccess files in Apache to enable pretty URLs. Singe Nginx does not support .htaccess files, we need to change Nginx to behave in the same way.

HOWTO

Install Nginx

  • We assume you have a clean install of Ubuntu 12.04 WITHOUT Apache installed.
  • Install Nginx

    sudo apt-get install nginx
    
  • Ensure the web server starts up and is running

    sudo /etc/init.d/nginx stop
    sudo /etc/init.d/nginx start
    
  • Navigate to the IP Address of the server where you installed Nginx using a browser to ensure Nginx serves content e.g. http://127.0.0.1

  • The default directory where Nginx serves its content from on Ubuntu is /usr/share/nginx/www.

Configure Nginx to interpret .php files

php5-fpm

  • The default install of Nginx does not support the serving of .php files.
  • We will install a program (actually a service) called php5-fpm. This service will listen for requests to interpret.
  • By default it listens on a network socket (127.0.0.1:9000)
  • We will change it to rather listen on a UNIX socket which will make things a bit faster.
  • Install the php5-fpm service:

    sudo apt-get install php5-fpm
    
  • Edit the config file so it creates a unix socket instead of a network socket to listen on.

    sudo vi -c37 /etc/php5/fpm/pool.d/www.conf
    
  • Change the listen part to the following:

    ;listen = 127.0.0.1:9000
    listen = /var/run/php5-fpm.sock
    
  • Restart the php5-fpm service:

    sudo /etc/init.d/php5-fpm restart
    

Modify Nginx

  • Now that the php5-fpm service is configured we should change the default Nginx server to make use of it.
  • Edit the default server file:

    sudo vi /etc/nginx/sites-enabled/default
    
  • Add index.php to this line:

    #add index.php
    index index.php index.html index.htm;
    
  • Activate PHP precessing by uncommenting this this section. Note that we use the UNIX socket:

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
        #       fastcgi_pass 127.0.0.1:9000;
        #       # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
    
  • Enable the hiding of .htaccess files

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny all;
    }
    
  • Reload the Nginx web server's configuration

    sudo /etc/init.d/nginx reload
    
  • Create a test .php file to confirm that it does work

    sudo vi /usr/share/nginx/www/test.php
    
  • Contents:

    <?php
        phpinfo();
    ?>
    
  • Navigate to http://127.0.0.1/test.php and see if the page display the PHP info.


Install MySQL

  • Be sure to supply a root password for the MySQL database when asked for it if you are security conscious else simply hit the ESC key.
    sudo apt-get install mysql-server mysql-client php5-mysql
    

Performance tune Nginx

Enable caching of PHP code

  • Install and activate php5-xcache.
    sudo apt-get install php5-xcache
    sudo /etc/init.d/php5-fpm reload
    

Modify expiry date for certain files

  • Edit the /etc/nginx/sites-available/default file:

    sudo vi /etc/nginx/sites-available/default
    
  • Add the following inside the server section:

    location ~* ^.+\.(jpg|jpeg|gif|png|ico|js|css)$ {
        rewrite ^/c2/rd_cake/webroot/(.*)$ /c2/rd_cake/webroot/$1 break;
        rewrite ^/c2/rd_cake/(.*)$ /c2/rd_cake/webroot/$1 break;
        access_log off;
        expires max;
        add_header Cache-Control public;
    }
    
  • Reload Nginx:

    sudo /etc/init.d/nginx reload
    

Compress the text before sending it to client

  • Edit the main config file of Nginx.

    sudo vi /etc/nginx/nginx.conf
    
  • Change the compression section to contain the following:

    #gzip on;
    #gzip_disable "msie6";
    
    gzip  on;
    gzip_http_version 1.1;
    gzip_vary on;
    gzip_comp_level 6;
    gzip_proxied any;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript text/x-js;
    gzip_buffers 16 8k;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    
  • Restart Nginx

    sudo /etc/init.d/nginx restart
    

Install RADIUSdesk

  • The first part prepares everything to install RADIUSdesk. This part will go through the steps to install the latest RADIUSdesk.
  • RADIUSdesk consists of three components.
    • rd directory with its contents contains all the HTML and JavaScript code and is used as the presentation layer.
    • rd_cake is a CakePHP application and can be considered the engine room. Here the data is processed before being presented by the presentation layer.
    • rd_login_pages is a directory with various login pages which are centrally managed through the RADIUSdesk Dynamic login pages applet.
  • We will use SVN (subversion) to either check out the latest version (trunk) of RADIUSdesk; or check out one of the tagged releases

Install CakePHP

  • Make sure the following packages are installed:

    sudo apt-get install php5-cli php5-gd php5-curl
    
  • Install the available language packs (These are optional)

    sudo apt-get install language-pack-af language-pack-fa
    
  • Download the 2.x version of CakePHP (Version 2.3.5 as of this writing). https://github.com/cakephp/cakephp/tags

  • There are two formats to choose from when selecting to download, Zip or Tar.gz. Select Tar.gz.
  • Copy and extract it inside the directory that Nginx is serving its content from (/usr/share/nginx/www by default on Ubuntu)

    sudo cp cakephp-2.3.5.tar.gz /usr/share/nginx/www
    cd /usr/share/nginx/www
    sudo tar -xzvf cakephp-2.3.5.tar.gz 
    sudo ln -s ./cakephp-2.3.5 ./cake2
    
  • Reload php5-fpm

    sudo /etc/init.d/php5-fpm reload
    

Install RADIUSdesk CakePHP Application

  • Install subversion in order for you to check out the latest source for RADIUSdesk.

    sudo apt-get install subversion
    
  • Check out the rd_cake branch from trunk to /usr/share/nginx/www.

    cd /usr/share/nginx/www/cake2
    sudo svn checkout svn://dvdwalt@svn.code.sf.net/p/radiusdesk/code/trunk/rd_cake ./rd_cake
    
  • TIP: If you are following the development of RADIUSdesk and want to make sure you have the latest SVN you can simply cd to /usr/share/nginx/www/cake2/rd_cake and run svn update to fetch the latest changes.

  • Change the following directories to be writable by www-data:

    sudo chown -R www-data. /usr/share/nginx/www/cake2/rd_cake/tmp
    sudo chown -R www-data. /usr/share/nginx/www/cake2/rd_cake/Locale
    sudo chown -R www-data. /usr/share/nginx/www/cake2/rd_cake/webroot/img/flags
    sudo chown -R www-data. /usr/share/nginx/www/cake2/rd_cake/webroot/img/nas
    sudo chown -R www-data. /usr/share/nginx/www/cake2/rd_cake/webroot/img/realms
    sudo chown -R www-data. /usr/share/nginx/www/cake2/rd_cake/webroot/img/dynamic_details
    sudo chown -R www-data. /usr/share/nginx/www/cake2/rd_cake/webroot/img/dynamic_photos
    sudo chown -R www-data. /usr/share/nginx/www/cake2/rd_cake/webroot/files/imagecache
    

The Database

  • Create the following blank database:

    mysql -u root
    create database rd;
    GRANT ALL PRIVILEGES ON rd.* to 'rd'@'127.0.0.1' IDENTIFIED BY 'rd';
    GRANT ALL PRIVILEGES ON rd.* to 'rd'@'localhost' IDENTIFIED BY 'rd';
    exit;
    
  • Populate the database (trunk):

    mysql -u root rd < /usr/share/nginx/www/cake2/rd_cake/Setup/Db/rd.sql
    
  • If you use a tagged release:

    mysql -u root rd < /usr/share/nginx/www/cake2/rd_cake/Setup/Db/rd_<tagged_version>.sql
    
  • If you are upgrading from a tagged release:

    mysql -u root rd < /usr/share/nginx/www/cake2/rd_cake/Setup/Db/rd_beta<your_tagged_version>_to_beta<next_tagged_verion>.sql
    

Configure Nginx

  • Since CakePHP uses rewrite rules, we have to configure Nginx in such a way as to allow rewriting of the URL's that starts with /cake2/rd_cake.
  • Edit /etc/nginx/sites-enabled/default

    sudo vi /etc/nginx/sites-enabled/default
    
  • Add the following section inside the server section:

    location /cake2/rd_cake {
        rewrite ^/cake2/rd_cake/(.*)$ /cake2/rd_cake/webroot/$1 break;
        try_files $uri $uri/ /cake2/rd_cake/webroot/index.php?q=$uri&$args;
    }
    
  • Reload the Nginx web server:

    sudo /etc/init.d/nginx reload
    

Modify all the paths

  • Some paths were hard coded into the files of RADIUSdesk.
  • Since the files are no longer located under /var/www/ but rather located under /usr/share/nginx/www we need to change them.
  • Fortunately UNIX (including Linux) has some very powerful scripting tools available to do just that for us.
  • Run the following command to update the paths
    cd /usr/share/nginx/www
    sudo bash -c "grep -R --files-with-matches '/var/www' . | sort | uniq | xargs perl -p -i.bak -e 's/\/var\/www\//\/usr\/share\/nginx\/www\//g'"
    

Test things out

  • RADIUSdesk supports multiple languages which are sourced during loading. To confirm that the CakePHP application is working as intended, go to this URL:
    http://127.0.0.1/cake2/rd_cake/phrase_values/get_language_strings.json?_dc=1355816922405&language=
  • Your browser should show a JSON encrypted string:

    {"data":{"phrases":{"spclCountry":"United Kingdom","spclLanguage":"English","sUsername":"......,"success":true}
    
  • Congratulations you are almost there. Next we will install the viewer component


Viewer component

  • Check out the latest code of the viewer component under the /usr/share/nginx/www/ directory:

    cd /usr/share/nginx/www/
    sudo svn checkout svn://dvdwalt@svn.code.sf.net/p/radiusdesk/code/trunk/rd ./rd
    
  • TIP: If you are following the development of RADIUSdesk and want to make sure you have the latest SVN you can simply cd to /usr/share/nginx/www/rd and run svn update to fetch the latest changes.

  • For the viewer component you need the ExtJS toolkit. As of this writing the latest version of ExtJS is 4.2.0. The development however was done on 4.1.1. This version is not easy to find on Sencha's download page so we've added it to the SVN repository for easy download :-)

  • Checkout and unzip the GPL version under the /usr/share/nginx/www/rd directory. NOTE: This is a single big file which will take some time to download over slow connections.

    sudo apt-get install unzip
    cd /usr/share/nginx/www/
    sudo svn checkout svn://svn.code.sf.net/p/radiusdesk/code/extjs ./
    sudo mv ext-4.1.1a-gpl.zip ./rd
    cd /usr/share/nginx/www/rd
    sudo unzip ext-4.1.1a-gpl.zip
    
  • Rename the extracted directory to simply ext:

    cd /usr/share/nginx/www/rd
    sudo mv ext-4.1.1a ext
    
  • Copy the ux folder under examples to the src folder

    sudo cp -R /usr/share/nginx/www/rd/ext/examples/ux /usr/share/nginx/www/rd/ext/src
    
  • Now try to log in on the following URL with username root and password admin:
    http://127.0.0.1/rd


Dynamic Login Pages

  • Check out the latest code of the dynamic login pages under the /usr/share/nginx/www/ directory:

    cd /usr/share/nginx/www/
    sudo svn checkout svn://dvdwalt@svn.code.sf.net/p/radiusdesk/code/trunk/rd_login_pages ./rd_login_pages
    
  • TIP: If you are following the development of RADIUSdesk and want to make sure you have the latest SVN you can simply cd to /usr/share/nginx/www/rd_login_pages and run svn update to fetch the latest changes.


Speed things up

  • The Java Script code from SVN is not optimised. You can however make things a bit faster by the following tweaks.
  • Edit the /usr/share/nginx/www/rd/app/app.js file and modify the top part to force it to cache the files:

    Ext.Loader.setConfig({enabled:true,disableCaching: false});
    
  • Edit the /usr/share/nginx/www/rd/index.html file and change this part:

    <!-- <x-bootstrap> -->
        <script src="ext/ext-dev.js"></script>
        <script src="bootstrap.js"></script>
    <!-- </x-bootstrap> -->
    

Next steps


Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.