Menu

YfiTechNginx

Anonymous

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 YFi Hotspot Manager 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 a very simple really.
  • 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 rinning

    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 concious else simply hit the ESC key.

    sudo apt-get install mysql-server mysql-client php5-mysql
    

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/yfi_cake/webroot/(.*)$ /c2/yfi_cake/webroot/$1 break;
        rewrite ^/c2/yfi_cake/(.*)$ /c2/yfi_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 YFi Hotspot Manager

  • The first part prepares everything to install YFi Hotspot Manager. This part will go through the steps ti install the latest YFi Hotspot Manager.
  • YFi hotspot Manager constist of two components.
    • yfi directory with its contents contains all the HTML and JavaScript? code and is used as the presentation layer.
    • yfi_cake is a CakePHP application and can be considered the engine room. Here the data is processed before being presented by the presentation layer.

Install CakePHP

  • Make sure the following packages are installed:

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

    sudo apt-get install language-pack-af language-pack-fr language-pack-id language-pack-ms language-pack-nl language-pack-th language-pack-es language-pack-pt language-pack-de language-pack-da language-pack-it
    
  • Download the 1.2.x version of CakePHP (Version 1.2.11 as of this writing). Note: It is important to use the 1.2.x branch with YFi hotspot Manager. Get CakePHP

  • 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-cakephp-1.2.11-8-ga9b5b0c.zip /usr/share/nginx/www
    cd /usr/share/nginx/www
    sudo unzip cakephp-cakephp-1.2.11-8-ga9b5b0c.zip 
    sudo ln -s ./cakephp-cakephp-a9b5b0c ./c2
    
  • Ensure allow_call_time_pass_reference is set to On in '/etc/php5/fpm/php.ini' [Line Number: 334]

    allow_call_time_pass_reference = On
    
  • Reload php5-fpm

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

Install YFi CakePHP Application

  • Download the latest version of the YFi Cake application. Get YFi CakePHP

  • If you want to try the latest development source, follow this instructions: SVN Quick Help

  • Extract it inside the '/usr/share/nginx/www/c2' directory.

    sudo cp [ latest version of yfi_cake ].tar.gz /usr/share/nginx/www/c2
    cd /usr/share/nginx/www/c2
    sudo tar -xzvf [ latest version of yfi_cake ].tar.gz
    sudo chown -R www-data. /usr/share/nginx/www/c2/yfi_cake/tmp
    sudo chown -R www-data. /usr/share/nginx/www/c2/yfi_cake/webroot/img/graphics
    sudo chown -R www-data. /usr/share/nginx/www/c2/yfi_cake/webroot/files
    

Configure the database

  • You need to create a database called 'yfi' and allow access to it for a username / password combination.

  • It is good practice to change the default username / password combination.

  • This can be changed inside the '/usr/share/nginx/www/c2/yfi_cake/config/database.php' file.

  • These sample commands assume the defaults.

    mysql -u root
    create database yfi;
    GRANT ALL PRIVILEGES ON yfi.* to 'yfi'@'127.0.0.1' IDENTIFIED BY 'yfi';
    GRANT ALL PRIVILEGES ON yfi.* to 'yfi'@'localhost' IDENTIFIED BY 'yfi';
    exit;
    
  • Dump the sample database in the newly created yfi database.

    mysql -u root yfi < /usr/share/nginx/www/c2/yfi_cake/setup/db/yfi.sql
    
  • If you use the latest SVN code be sure to patch the SQL also to contain the latest changes.

    mysql -u root yfi < /usr/share/nginx/www/c2/yfi_cake/setup/db/beta-5_to_beta-6_patch.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 /c2/yfi_cake.
  • Edit /etc/nginx/sites-enabled/default

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

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

    sudo /etc/init.d/nginx reload
    

Install Presentation layer

  • Get the latest viewer component of YFi Hotspot Manager. Get YFi Hotspot Viewer Component

  • If you want to try the latest development source, follow this instructions: SVN Quick Help

  • Copy and extract it inside the directory that Nginx is serving its content from (/usr/share/nginx/www by default on Ubuntu)

    sudo cp [latest version of yfi].tar.gz /usr/share/nginx/www
    cd /usr/share/nginx/www
    sudo tar -xzvf [latest version of yfi].tar.gz
    

Modify all the paths

  • Some paths were hard coded into the files of YFi hHotspot Manager.
  • 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 powerfull scripting tools available to do just that for us.
  • Run the following command to update the paths

    sudo su
    
    cd /usr/share/nginx/www/yfi/js/dojo
    grep -R --files-with-matches '/c2/yfi_cake/files/image.php' . | sort | uniq | xargs perl -p -i.bak -e 's/\/c2\/yfi_cake\/files\/image\.php/\/c2\/yfi_cake\/webroot\/files\/image\.php/g'
    
    cd /usr/share/nginx/www/c2
    grep -R --files-with-matches '/var/www' . | sort | uniq | xargs perl -p -i.bak -e 's/\/var\/www\//\/usr\/share\/nginx\/www\//g'
    

Test the CakePHP application

  • Verify that everything works as intended by doing the following test.

  • Go to the followingURL and be sure that a login-page is displayed.

http://127.0.0.1/c2/yfi_cake/users/


Test the Presentation layer

  • Go to the following URL http://127.0.0.1/yfi (you may need to replace 127.0.0.1 with the IP address of the server you installed the YFi Hotspot Manager on)

  • You should get a splash page which, depending on the speed of your network connection, will change into a login page.

  • Log in either as the administrator or as an Access Provider or as a permanent user.

  • The following table displays the default username and password for each.

Role
Username
Password

Administrator
root
admin

Access Provider
ap
ap

Permanent User
dvdwalt@ri
dvdwalt@ri

Next steps


Related

Wiki: Home
Wiki: yfi_setup_FreeRADIUS
Wiki: yfi_setup_cron
Wiki: yfi_setup_pptpd
Wiki: yfi_setup_svn