I run a php-based web site (shopping mall) on nginx web server. recently I heard that phalcon is very fast and reliable framework, helping developer speed up the dev process.
So I wanted to implement it to my site, starting as a submodule for specific services for it's hard to start from the scratch.
I want to create a subfolder (let's say "phalcontest") under the /var/www .
I googled about this topic, but all(maybe?) the contents I found on the net were to set nginx server as a dedicated site for phalcon project (I mean, make "the project" folder as a web root), nothing about making phalcon project coexist with old non-phalcon pages.
I tried nginx configuration something like this, hoping it's ok:
server {
listen 80;
root /var/www/;
index index.html index.htm index.php;
server_name localhost;
access_log /var/log/nginx/access.log main buffer=16k;
error_log /var/log/nginx/error.log;
location @rewrite {
rewrite ^(.*)$ phalcontest/index.php?_url=$1;
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
}
location /phalcontest {
try_files $uri $uri/ @rewrite;
}
location ~* ^/(css|img|js|flv|swf|download)/(.+)$ {
root /var/www/;
}
}
to make "phalcontest" folder to be a phalcon-enabled subfolder under webroot.
After that I created phalcontest with phalcon devtools,
and accessed http://localhost/phalcontest/public, it shows:
Congratulations! You're now flying with Phalcon. Great things are about to happen! This page is located at views/index/index.phtml
but when I accessed http://localhost/phalcontest, it shows:
Mod-Rewrite is not enabled
Please enable rewrite module on your web server to continue
this means that I've done something wrong and I can only access via public folders, not the folder itself.
Please help me how to let phalcon live with existing php-based site!
The way that Phalcon is supposed to work is that it rewrites all traffic aimed at your web root /
to the /public/
directory. The point of this is that it enables you to essentially hide the /app/
directory from web view since the user's request would first be rewritten to /public/app/
thus encountering a 404 which gets rewritten to /public/index.php
where it would look for the controller "App". If you're going to run Phalcon from a subdirectory, you'll need to change all /
to /phalcontest/
.
I see you're using root /var/www/;
.
So in your case, you'll want a directory structure like so:
/www/phalcontest/public/index.php
/www/phalcontest/public/css/bootstrap.css
/www/phalcontest/public/js/jquery.js
/www/phalcontest/app/config/config.php
/www/phalcontest/app/controllers/IndexController.php
/www/phalcontest/app/models/
/www/phalcontest/app/views/index.phtml
/www/phalcontest/app/views/index/index.phtml
Make sure to rewrite all requests aimed at phalcontest(/.*)?
to phalcontest/public/$1
then from there rewrite them to index.php
if the requested file or directory doesn't exist.
Here's a tool for converting .htaccess to nginx if it's any help: http://winginx.com/en/htaccess
Honestly, you don't need to follow their directory structure so perfectly. You only really need the rewrite logic to rewrite all traffic to your index.php file. The parts regarding Phalcon's proposed /public/
directory, you don't need a /public/
directory if you simply move your /app/
directory below the web root, then reference its location properly from your index.php
file.
Phalcon gives you a lot of information regarding how to setup the rewriting for nginx over here: https://docs.phalconphp.com/en/latest/reference/nginx.html
But since you're working from your localhost, I'd suggest creating a subdomain on the localhost using your hosts file. Then you can work from the /
web root and access your project from http://phalcontest.localhost/
. Then you'd use server_name phalcontest.localhost;
in your nginx configuration rather than server_name localhost;
and aim its root at root /var/www/phalcontest;
For your hosts file you'd add a line: phalcontest.localhost 127.0.0.1
. That will save you from a DNS look up so the subdomain gets tied to your localhost. See nginx subdomain configuration and How to test nginx subdomains on localhost.