Web Server Setup

In order for the routing of the Phalcon application to work, you will need to set up your web server to process the redirects properly. Setup instructions for popular web servers are:

PHP-FPM

The PHP-FPM (FastCGI Process Manager) is usually used to allow the processing of PHP files. Nowadays, PHP-FPM is bundled with all Linux based PHP distributions.

On Windows PHP-FPM is in the PHP distribution archive through the file php-cgi.exe and you can start it with this script to help set options. Windows does not support unix sockets so this script will start fast-cgi in TCP mode on port 9000.

PHP Built-In Webserver (For Developers)

To speed up getting your Phalcon application running in development the easiest way is to use this built-in PHP server. Do not use this server in a production environment. The following configurations for Nginx and Apache are what you need.

Phalcon configuration

To enable dynamic URI rewrites, without Apache or Nginx, that Phalcon needs, you can use the following router file:
.htrouter.php

If you created your application with Phalcon-Devtools this file should already exist in the root directory of your project and you can start the server with the following command:

$(which php)-S localhost:8000 -t public .htrouter.php

The anatomy of the command above:

$(which php) - will insert the absolute path to your PHP binary

-S localhost:8000 - invokes server mode with the provided host:port

-t public - defines the servers root directory, necessary for php to route requests to assets like JS, CSS, and images in your public directory

.htrouter.php - the entry point that will be evaluated for each request

Then point your browser to http://localhost:8000/ to check if everything is working.

Nginx

Nginx is a free, open-source, high-performance HTTP server and reverse proxy, as well as an IMAP/POP3 proxy server. Unlike traditional servers, Nginx doesn’t rely on threads to handle requests. Instead it uses a much more scalable event-driven (asynchronous) architecture. This architecture uses small, but more importantly, predictable amounts of memory under load.

Phalcon with Nginx and PHP-FPM provide a powerful set of tools that offer maximum performance for your PHP applications.

Install Nginx

Phalcon configuration

You can use following potential configuration to setup Nginx with Phalcon:

server{# Port 80 will require Nginx to be started with root permissions
# Depending on how you install Nginx to use port 80 you will need
# to start the server with `sudo` ports about 1000 do not require
# root privileges
# listen 80;
listen8000;server_namedefault;##########################
# In production require SSL
# listen 443 ssl default_server;
# ssl on;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# These locations depend on where you store your certs
# ssl_certificate /var/nginx/certs/default.cert;
# ssl_certificate_key /var/nginx/certs/default.key;
##########################
# This is the folder that index.php is in
root/var/www/default/public;indexindex.phpindex.htmlindex.htm;charsetutf-8;client_max_body_size100M;fastcgi_read_timeout1800;# Represents the root of the domain
# http://localhost:8000/[index.php]
location/{# Matches URLS `$_GET['_url']`
try_files$uri$uri//index.php?_url=$uri&$args;}# When the HTTP request does not match the above
# and the file ends in .php
location~[^/]\.php(/|$){# try_files $uri =404;
# Ubuntu and PHP7.0-fpm in socket mode
# This path is dependent on the version of PHP install
fastcgi_passunix:/var/run/php/php7.0-fpm.sock;# Alternatively you use PHP-FPM in TCP mode (Required on Windows)
# You will need to configure FPM to listen on a standard port
# https://www.nginx.com/resources/wiki/start/topics/examples/phpfastcgionwindows/
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index/index.php;includefastcgi_params;fastcgi_split_path_info^(.+?\.php)(/.*)$;if(!-f$document_root$fastcgi_script_name){return404;}fastcgi_paramPATH_INFO$fastcgi_path_info;# fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
# and set php.ini cgi.fix_pathinfo=0
fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;}location~/\.ht{denyall;}location~*\.(js|css|png|jpg|jpeg|gif|ico)$ {expiresmax;log_not_foundoff;access_logoff;}}

Start Nginx

Usually start nginx from the command line but this depends on your installation method.

Apache

Apache is a popular and well known web server available on many platforms.

Phalcon configuration

The following are potential configurations you can use to setup Apache with Phalcon. These notes are primarily focused on the configuration of the mod_rewrite module allowing to use friendly URLs and the router component. Commonly an application has the following structure:

Document root

This being the most common case, the application is installed in any directory under the document root. In this case, we use two .htaccess files, the first one to hide the application code forwarding all requests to the application’s document root (public/).

Note that using .htaccess files requires your apache installation to have the AllowOverride All option set. ##### {.alert .alert-warning}

For users that are using the Persian letter ‘م’ (meem) in uri parameters, there is an issue with mod_rewrite. To allow the matching to work as it does with English characters, you will need to change your .htaccess file:

Cherokee

Cherokee is a high-performance web server. It is very fast, flexible and easy to configure.

Phalcon configuration

Cherokee provides a friendly graphical interface to configure almost every setting available in the web server.

Start the cherokee administrator by executing as root /path-to-cherokee/sbin/cherokee-admin

Create a new virtual host by clicking on vServers, then add a new virtual server:

The recently added virtual server must appear at the left bar of the screen. In the Behaviors tab you will see a set of default behaviors for this virtual server. Click the Rule Management button. Remove those labeled as Directory /cherokee_themes and Directory /icons:

Add the PHP Language behavior using the wizard. This behavior allows you to run PHP applications:

Normally this behavior does not require additional settings. Add another behavior, this time in the Manual Configuration section. In Rule Type choose File Exists, then make sure the option Match any file is enabled:

In the ‘Handler’ tab choose List & Send as handler:

Edit the Default behavior in order to enable the URL-rewrite engine. Change the handler to Redirection, then add the following regular expression to the engine ^(.*)$: