Webserver Setup


Webserver Setup - 图1

Overview

In order for the routing for a Phalcon application to work, you will need to set up your web server in a way that it will process redirects properly. Below are instructions for popular web servers:

PHP Built-in

The PHP built in web server is not recommended for production applications. You can use it though very easily for development purposes. The syntax is:

  1. $(which php) -S <host>:<port> -t <directory> <setup file>

If your application has its entry point in /public/index.php or your project has been created by the Phalcon Devtools, then you can start the web server with the following command:

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

The above command does:

  • $(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 requestThe .htrouter.php file must contain:
  1. <?php
  2. declare(strict_types=1);
  3. $uri = urldecode(
  4. parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
  5. );
  6. if ($uri !== '/' && file_exists(__DIR__ . '/public' . $uri)) {
  7. return false;
  8. }
  9. $_GET['_url'] = $_SERVER['REQUEST_URI'];
  10. require_once __DIR__ . '/public/index.php';

If your entry point is not public/index.php, then adjust the .htrouter.php file accordingly (last line) as well as the script call. You can also change the port if you like as well as the network interface that it binds to.

After executing the command above, navigating to http://localhost:8000/ will show your your site.

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.

Create the file php-fcgi.bat with the following contents:

  1. @ECHO OFF
  2. ECHO Starting PHP FastCGI...
  3. set PATH=C:\PHP;%PATH%
  4. c:\bin\RunHiddenConsole.exe C:\PHP\php-cgi.exe -b 127.0.0.1:9000

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

NginX Offical Site

Phalcon configuration

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

  1. server {
  2. # Port 80 will require Nginx to be started with root permissions
  3. # Depending on how you install Nginx to use port 80 you will need
  4. # to start the server with `sudo` ports about 1000 do not require
  5. # root privileges
  6. # listen 80;
  7. listen 8000;
  8. server_name default;
  9. ##########################
  10. # In production require SSL
  11. # listen 443 ssl default_server;
  12. # ssl on;
  13. # ssl_session_timeout 5m;
  14. # ssl_protocols SSLv2 SSLv3 TLSv1;
  15. # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  16. # ssl_prefer_server_ciphers on;
  17. # These locations depend on where you store your certs
  18. # ssl_certificate /var/nginx/certs/default.cert;
  19. # ssl_certificate_key /var/nginx/certs/default.key;
  20. ##########################
  21. # This is the folder that index.php is in
  22. root /var/www/default/public;
  23. index index.php index.html index.htm;
  24. charset utf-8;
  25. client_max_body_size 100M;
  26. fastcgi_read_timeout 1800;
  27. # Represents the root of the domain
  28. # https://localhost:8000/[index.php]
  29. location / {
  30. # Matches URLS `$_GET['_url']`
  31. try_files $uri $uri/ /index.php?_url=$uri&$args;
  32. }
  33. # When the HTTP request does not match the above
  34. # and the file ends in .php
  35. location ~ [^/]\.php(/|$) {
  36. # try_files $uri =404;
  37. # Ubuntu and PHP7.0-fpm in socket mode
  38. # This path is dependent on the version of PHP install
  39. fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
  40. # Alternatively you use PHP-FPM in TCP mode (Required on Windows)
  41. # You will need to configure FPM to listen on a standard port
  42. # https://www.nginx.com/resources/wiki/start/topics/examples/phpfastcgionwindows/
  43. # fastcgi_pass 127.0.0.1:9000;
  44. fastcgi_index /index.php;
  45. include fastcgi_params;
  46. fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  47. if (!-f $document_root$fastcgi_script_name) {
  48. return 404;
  49. }
  50. fastcgi_param PATH_INFO $fastcgi_path_info;
  51. # fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
  52. # and set php.ini cgi.fix_pathinfo=0
  53. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  54. }
  55. location ~ /\.ht {
  56. deny all;
  57. }
  58. location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
  59. expires max;
  60. log_not_found off;
  61. access_log off;
  62. }
  63. }

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:

  1. test/
  2. app/
  3. controllers/
  4. models/
  5. views/
  6. public/
  7. css/
  8. img/
  9. js/
  10. index.php

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.

  1. # test/.htaccess
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine on
  4. RewriteRule ^$ public/ [L]
  5. RewriteRule ((?s).*) public/$1 [L]
  6. </IfModule>

A second .htaccess file is located in the public/ directory, this re-writes all the URIs to the public/index.php file:

  1. # test/public/.htaccess
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine On
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
  7. </IfModule>

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:

  1. # test/public/.htaccess
  2. <IfModule mod_rewrite.c>
  3. RewriteEngine On
  4. RewriteCond %{REQUEST_FILENAME} !-d
  5. RewriteCond %{REQUEST_FILENAME} !-f
  6. RewriteRule ^([0-9A-Za-z\x7f-\xff]*)$ index.php?params=$1 [L]
  7. </IfModule>

If your uri contains characters other than English, you might need to resort to the above change to allow mod_rewrite to accurately match your route.

Apache configuration

If you do not want to use .htaccess files you can move these configurations to the apache’s main configuration file:

  1. <IfModule mod_rewrite.c>
  2. <Directory "/var/www/test">
  3. RewriteEngine on
  4. RewriteRule ^$ public/ [L]
  5. RewriteRule ((?s).*) public/$1 [L]
  6. </Directory>
  7. <Directory "/var/www/test/public">
  8. RewriteEngine On
  9. RewriteCond %{REQUEST_FILENAME} !-d
  10. RewriteCond %{REQUEST_FILENAME} !-f
  11. RewriteRule ^((?s).*)$ index.php?_url=/$1 [QSA,L]
  12. </Directory>
  13. </IfModule>

Virtual Hosts

And this second configuration allows you to install a Phalcon application in a virtual host:

  1. <VirtualHost *:80>
  2. ServerAdmin [email protected]
  3. DocumentRoot "/var/vhosts/test/public"
  4. DirectoryIndex index.php
  5. ServerName example.host
  6. ServerAlias www.example.host
  7. <Directory "/var/vhosts/test/public">
  8. Options All
  9. AllowOverride All
  10. Require all granted
  11. </Directory>
  12. </VirtualHost>

WAMP

WampServer is a Windows web development environment. It allows you to create web applications with Apache2, PHP and a MySQL database. Below are detailed instructions on how to install Phalcon on WampServer for Windows. Using the latest WampServer version is highly recommended.

Download Phalcon

WAMP has both 32 and 64 bit versions. From the download section, you can download the Phalcon DLL that suits your WAMPP installation.

After downloading the Phalcon library you will have a zip file like the one shown below:

Webserver Setup - 图2

Extract the library from the archive to get the Phalcon DLL:

Webserver Setup - 图3

Copy the file php_phalcon.dll to the PHP extensions folder. If WAMP is installed in the C:\wamp folder, the extension needs to be in C:\wamp\bin\php\php5.5.12\ext (assuming your WAMP installation installed PHP 5.5.12).

Webserver Setup - 图4

Edit the php.ini file, it is located at C:\wamp\bin\php\php5.5.12\php.ini. It can be edited with Notepad or a similar program. We recommend Notepad++ to avoid issues with line endings. Append at the end of the file:

  1. extension=php_phalcon.dll

and save it.

Webserver Setup - 图5

Also edit the php.ini file, which is located at C:\wamp\bin\apache\apache2.4.9\bin\php.ini. Append at the end of the file:

  1. extension=php_phalcon.dll

and save it.

Restart the Apache Web Server. Do a single click on the WampServer icon at system tray. Choose Restart All Services from the pop-up menu. Check out that tray icon will become green again.

Webserver Setup - 图6

Open your browser to navigate to https://localhost. The WAMP welcome page will appear. Check the section extensions loaded to ensure that phalcon was loaded.

Webserver Setup - 图7

Congratulations! You are now phlying with Phalcon.

XAMPP

XAMPP is an easy to install Apache distribution containing MySQL, PHP and Perl. Once you download XAMPP, all you have to do is extract it and start using it. Below are detailed instructions on how to install Phalcon on XAMPP for Windows. Using the latest XAMPP version is highly recommended.

Download Phalcon

XAMPP is always releasing 32 bit versions of Apache and PHP. You will need to download the x86 version of Phalcon for Windows from the download section.

After downloading the Phalcon library you will have a zip file like the one shown below:

Webserver Setup - 图8

Extract the library from the archive to get the Phalcon DLL:

Webserver Setup - 图9

Copy the file php_phalcon.dll to the PHP extensions directory. If you have installed XAMPP in the C:\xampp folder, the extension needs to be in C:\xampp\php\ext

Webserver Setup - 图10

Edit the php.ini file, it is located at C:\xampp\php\php.ini. It can be edited with Notepad or a similar program. We recommend Notepad++ to avoid issues with line endings. Append at the end of the file:

  1. extension=php_phalcon.dll

and save it.

Webserver Setup - 图11

Restart the Apache Web Server from the XAMPP Control Center. This will load the new PHP configuration.

Webserver Setup - 图12

Open your browser to navigate to https://localhost. The XAMPP welcome page will appear. Click on the link phpinfo().

Webserver Setup - 图13

phpinfo will output a significant amount of information on screen about the current state of PHP. Scroll down to check if the phalcon extension has been loaded correctly.

Webserver Setup - 图14

If you can see the phalcon version in the phpinfo() output, congratulations!, You are now phlying with Phalcon.

Screencast

The following screencast is a step by step guide to install Phalcon on Windows:

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

Webserver Setup - 图15

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

Webserver Setup - 图16

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:

Webserver Setup - 图17

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

Webserver Setup - 图18

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:

Webserver Setup - 图19

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

Webserver Setup - 图20

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 ^(.*)$:

Webserver Setup - 图21

Finally, make sure the behaviors have the following order:

Webserver Setup - 图22

Execute the application in a browser:

Webserver Setup - 图23