6 Steps to Configure Virtual Host and CakePHP in Nginx Webserver

cakephp-on-nginx-solved

The use of shared hosting is limited designers and developers for creativity, time to move to a VPS or dedicated server. If you are a beginner, meaning that so far only use shared hosting, such as hostgator and bluehost, we recommend using the VPS. Technically, VPS is similar to a dedicated server, it’s just a different ability. If you want to learn, use the VPS.

I also am learning VPS. My goal is to use a web server Nginx + PHP FPM and MariaDB version 10. This is the system I wanted, because rarely shared hosting using Nginx web server. For the record, Nginx web server is a alternative web server for Apache and LiteSpeed  that have the ability to process requests faster and more memory efficient.

In this post I want to show you how to create a virtual web server hosting on Nginx. Maybe this is a personal record for me, so do not forget. You need to prepare is

  1. VPS hosting with OS Debian 6 or 7. I’m using version 7.
  2. Access as root
  3. And of course already installed Nginx, PHP FPM and MariaDB 10.x I will make this tutorial.

Ok, we started to just create a virtual host. In this experiment, I use a free domain from freenom. You can also get a free domain : cakephp.cf

1. Prepare Directory To Store Your Website Files

Sign in to SSH, and type this command

mkdir -p /srv/www/cakephp.cf/htdocs
mkdir -p /srv/www/cakephp.cf/logs

website files will be stored in the htdocs directory.

2. Creating Virtual Host in Nginx

cd /etc/nginx/sites-available/
touch cakephp.cf

3. Edit Your Virtual Host File

We use the nano editor to edit the configuration of the virtual host.

nano cakephp.cf

Then full fill of the configuration as below. For the case in this post, I want to use cakephp at this domain. Each virtual host configuration is different, depending on web technology you use.

server {
    listen   80;
    server_name www.cakephp.cf;
    rewrite ^(.*) http://cakephp.cf$1 permanent;
}

server {
    listen   80;
    server_name cakephp.cf;

 
	root /srv/www/cakephp.cf/htdocs/app/webroot/;
    index  index.php;

    access_log /srv/www/cakephp.cf/logs/access.log;
    error_log /srv/www/cakephp.cf/logs/error.log;

    # main cakephp rewrite rule
    location / {
        try_files $uri $uri/ /index.php?$uri&$args;
    }

        location ~ .php$ {
            root           /srv/www/cakephp.cf/htdocs/app/webroot/;
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 256 4k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
        }

}

If you want to use wordpress, please use the setting as below

server {
       listen 80;
       #listen [::]:80 ipv6only=on default_server;
       server_name cakephp.cf;
       root /srv/www/cakephp.cf/htdocs;
    access_log /srv/www/cakephp.cf/logs/access.log;
    error_log /srv/www/cakephp.cf/logs/error.log;
       if ($http_host != "www.cakephp.cf") {
                 rewrite ^ http://www.cakephp.cf$request_uri permanent;
       }
       index index.php index.html;
       location = /favicon.ico {
                log_not_found off;
                access_log off;
       }
       location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
       }
 
 # Use gzip compression
 # gzip_static on; # Uncomment if you compiled Nginx using --with-http_gzip_static_module
 gzip on;
 gzip_disable "msie6";
 gzip_vary on;
 gzip_proxied any;
 gzip_comp_level 5;
 gzip_buffers 16 8k;
 gzip_http_version 1.0;
 gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/png image/gif image/jpeg;
 
        location ~ ^/wp-content/cache/minify/[^/]+/(.*)$ {
                try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1;
        }
 
 #location ~ ^/wp-content/plugins/wp-minify/min/[^/]+/(.*)$ {
 # try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1;
 # wp-minify/cache
 #}
 
 # Don't cache uris containing the following segments
 if ($request_uri ~* "(\/wp-admin\/|\/xmlrpc.php|\/wp-(app|cron|login|register|mail)\.php|wp-.*\.php|index\.php|wp\-comments\-popup\.php|wp\-links\-opml\.php|wp\-locations\.php)") {
 set $cache_uri "no cache";
 }
 
 # Don't use the cache for logged in users or recent commenters
 if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp\-postpass|wordpress_logged_in") {
 set $cache_uri 'no cache';
 }
 
       
 
 # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
       location ~ /\. {
                deny all;
                access_log off;
                log_not_found off;
       }
       location / {
                try_files $uri $uri/ /index.php?$args;
       }
 
       # Add trailing slash to */wp-admin requests.
       rewrite /wp-admin$ $scheme:http://$host$uri/ permanent;
 
 # Cache static files for as long as possible - removed xml as an extension to avoid problems with Yoast WordPress SEO plugin which uses WP rewrite API.
 location ~* \.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
 try_files $uri =404;
 expires max;
 access_log off;
 }
 
 # Pass PHP scripts on to PHP-FPM
 location ~* \.php$ {
 fastcgi_split_path_info ^(.+\.php)(/.+)$;
 try_files $uri /index.php;
 fastcgi_index index.php;
 fastcgi_pass unix:/var/run/php5-fpm.sock;
 #fastcgi_pass 127.0.0.1:9000;
 include fastcgi_params;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 fastcgi_param SCRIPT_NAME $fastcgi_script_name;
   }
}

4. Download and Install CakePHP

cd /srv/www/cakephp.cf/htdocs
wget https://github.com/cakephp/cakephp/zipball/2.6.2
unzip 2.6.2
cp -rvf cakephp-cakephp-ec00c2d/* .
rm -R cakephp-cakephp-ec00c2d
rm 2.6.2

5. Creating Symbolic Link to Nginx

ln -s /etc/nginx/sites-available/cakephp.cf /etc/nginx/sites-enabled/cakephp.cf

Symbolic links useful for Nginx know new virtual host.

6. Provide Access Rights to the World

chown -R www-data:www-data /srv/www/cakephp.cf/htdocs

Now, look at cakephp.cf, runs perfectly.

Note: To follow this tutorial, you’ve directed your DNS to cakephp.cf domain. I use cloudflare for free and fast.

Dont forget to restart nginx with command like this.

service nginx restart

 

 

Useful CakePHP Libraries for Image Manipulation, Cropping, Resize and Thumbnail

CakePHP is a framework that is widely used by developers, including me. I was cakephp user since 2008, and makes CakePHP as the main foundation of web applications. By using the framework, web applications can be finished quickly and safely.

One of the problems using CakePHP is when dealing with the image. CakePHP able to  image processing such as image resizing, image cropping, image manipulation and others. I often do is upload the image. It seems very simple, just upload a file, but with CakePHP you can select any image file that can be uploaded.

Every work on projects related to the image, I keep the library, components, plugins associated with the image, and this time I wanted to share it all. Might be useful for you.

[code tipe=”336″]

Image Tool Component

image tool component

ImageTool is php class to perform various tasks with images. Every function accepts GD resource as input and can output it as well (just leave ‘output’ option empty).

This component has these function :

  • autorotate – autorotate JPG images (by exif data)
  • averageColor – get image’s average color
  • dominatingColor – get image’s dominating color
  • flip – flip image
  • grayscale – desaturate image
  • meshify – add mesh (grid of dots) over image
  • pixelate – pixelate image
  • resize – resize image
  • rotate – rotate image (only degrees divisible by 90)
  • unsharpMask – sharpen image
  • watermark – add watermark

Download

QImage Component

qimage component for cakephp

Quick Image Component (Qimage) is a component for CakePHP to facilitate image manipulation and upload. Tested on CakePHP 2.x

  1. copy -> Copy uploaded images.
  2. resize -> Resize an image.
  3. watermark -> Add watermark in an image.
  4. crop -> Crop an image.
  5. getErrors -> Get all errors that occurred in Qimage.
  6. _verifyMime -> Verify the MIME type of a file based on file extension.
  7. _getCreateFunction -> Get image creation function based on image mime type.
  8. _getFinishFunction -> Get image finish function based on image mime type.

[code tipe=”link”]

Download

CakeImageCropResize Plugin for CakePHP

cakephp image crop plugin

A small CakePHP plugin for resizing and cropping of images. Includes a Helper and Component.

[code tipe=”336″]

The Helper and/or Component gives you the ability to leave your original images in tact and resize/crop them for View only. Download

Image Soup Cakephp Component

image soup cakephp component

CakePHP component for image upload/checking, resizing and thumbnail generation (regular and square/cropped thumbnails). This component using GD library.

Download

Image Thumbnail Helpers for Cakephp

thumbnail image helper for cakephp

Helper to generate thumbnail images dynamically by saving them to the cache. Alternative to phpthumb.

Download

CakePHP Image Uploader

cakephp image uploader

Is a component for uploading images to the server. This component also comes with a thumbnail creation function, so you can easily create a thumbnail version

[code tipe=”link”]

Download

CakePHP 3.0 Image Upload Behavior

CakePHP 3.0 Image upload behavior

If you are using CakePHP 3.0, you can use this component to upload image.

Download

TinyMCE Image Upload for CakePHP

tinymce image upload for cakephp

Integration of Elfinder file manager(2.0-rc1) with tinymce(4.0.10) for Cakephp 2.x. Allows to upload images(and other files) in tinymce. Can be a free alternative for Tinymce MoxieManager image upload plugin

Download

MySQL Image Storage

mysql image storage

Is a plugin for cakephp, so that your application has a function of inserting images into mySQL database. Download

Youtube Image Helpers

youtube image helpers

A youtube thumbnail generator class for CakePHP

Download

CKEditor Image Upload for CakePHP

Ckeditor With Image Upload In Cakephp

Ckeditor With Image Upload In Cakephp

[code tipe=”336″]

Download

 

 

Snippet : How to Build Better URL CakePHP Pagination Just Like on WordPress

pagination on cakephp

Here is best cakephp snippet to build and rewrite pagination URL. You can build url like this (such as wordpress pagination)

example.com/page/1

example.com/page/2

example.com/page/3

Here is the superb snippet for it

Add this on AppController.php

function beforeFilter(){
		if (isset($this->request->params['page'])) {
			$this->request->params['named']['page'] = $this->request->params['page'];
		}
	}

Add this code to route.php

Router::connect('/page/:page', 
    array('controller' => 'controller', 'action' => 'index'),
    array(
        'pass' => 'page',
        'page' => '[0-9]+'
        )
	);

Newbie Tips : How to Use Bootstrap with CakePHP

formated text

CakePHP is a popular framework that is widely used by web developers and has an active community, which is constantly developing. Until now, CakePHP has reached version 3.0 dev3. I prefer to use the stable version, which is version 2.4. In version 3.0, reportedly many added features and a completely new, so we as developers are very easy to create sophisticated web. Check our snippet code for cakephp

CakePHP is a machine, you must creating a style manually. For that, I usually combine CakePHP with Bootstrap. Bootstrap is a CSS framework issued by twitter. Well, this post will explain how to combining Cakephp with bootstrap.

This is actually very simple, just add a CSS and javascript files. But for beginners, may find it difficult. Read more

Tutorial : How to Running CakePHP on Easyengine VPS

CakePHP is my fav. framework, because of the ease and speed it offers, build web applications more quickly and easily, besides that, you are also more secure. The codes in CakePHP already in the test and get updated regularly. This is the advantage of making the web with the framework.

CakePHP using .htaccess to rewrite URLs in. Of course there must be a deal with the web server. Web servers that support the full CakePHP and easy to use is apache, because apache support .htaccess and mod_rewrite. Without these two elements, cakephp is not running perfectly.

Even so, cakephp give tips or special code that can run on all web servers. Including the nginx web server. I’ve run the cakephp on nginx, but it must make complex configuration.

But, with easyengine, you can run cakephp very easily and quickly. I was also surprised by this.

Easyengine is a python script created by rtcamp, a group of developers from India which allows us to install Nginx + PHP + MySQL and other easily and quickly. And everything is configured perfectly.

To install wordpress on easyengine, very easy and this article does not menjelasnnya. We will explains  how to install cakephp on easyengine.

Say you’ve installed easyengine and ready to action. I use ubuntu14.04 64bit with 3GB of RAM. Now you’re ready.

1. Create new site with  PHP+Mysql support

ee create site cakephp.cf --mysql

2. Download CakePHP latest version

wget https://github.com/cakephp/cakephp/archive/3.0.2.zip
unzip 3.0.2.zip
cp -rvf cakephp-3.0.2/* .

3. Update Permisssion

chmod -R 777 /var/www/cakephp.cf/htdocs/app/webroot/
chmod -R 777 /var/www/cakephp.cf/htdocs/app/tmp/

4. Update VHost file

nano /etc/nginx/sites-available/cakephp.cf

edit root line becoms like this below

root /var/www/cakephp.cf/htdocs/app/webroot/;

5. Restart your Nginx web server

And look at your domain. In this tutorial, I use cakephp.cf

This Script Will Simplify Running CakePHP on EasyEngine (Nginx)

cakephp on easy engine

cakephp on easy engine

A few months ago I wrote about Cakephp and nginx running on easyengine. Easyengine is a collection of scripts to facilitate the VPS users to install wordpress and other scripts on a web server nginx. Unfortunately for running Cakephp, it takes one line of the script so that all are running perfectly.

[code tipe=”336″]

Here are the steps to run CakePHP.

  1. Create site with mysql support (ee site create example.com –mysql)
  2. Upload your cakephp website. Or download it with wget command.
  3. Modify cakephp temporary and web root directory in order
    chmod -R 777 /var/www/yoursite.com/htdocs/app/webroot/
    chmod -R 777 /var/www/yoursite.com/htdocs/app/tmp/
  4. This is one line code that very crusial. Open website nginx config file with nano /etc/nginx/sites-available/yoursite.com
  5. Find and change
    root /var/www/yoursite.com/htdocs;
    to
    root /var/www/yoursite.com/htdocs/app/webroot/;
  6. Then restart your nginx server with command
    service nginx restart

Please check your site. This tutorial tested work on cakephp 2.x, I have not tried to version 3.x

[code tipe=”336″]

Please share this article, it will help many developer

[Solved] Warning 512 Invalid Salt on CakePHP when Use Blowfish Encryption

cakephp-blowfish-error

CakePHP has a complete feature to create a simple authentication or system login / logout. Although simple, but the quality of security sufficient for modern websites. If you want to learn to make the authentication system, please follow the tutorials available on this page. I also followed him, and succeeded perfectly.

However, there is a bit of a problem, that is, when you use a hash or encryption method different password other than blowfish. Yes, on the tuturial, cakephp using blowfish hash method. If you fill in the password by methods other than blowfish, then you will get an error like this.

cakephp blowfish error

Dont worry, the problem is not your system or cakephp or PHP version. However, for the PHP version, make sure you are using version 5.3 and above. I am using PHP version 5.5.3 and no problems.

The problem is, you use a different password hash method with blowfish, for example md5. I’m using MD5 and there was an error as above.