Cakephp is a great framework for creating fast and professional web sites. There are some tricks to configure correctly in Apache web server with php if it is wanted to be configured in a different directory (virtual directory) outside the main www area.
PROBLEM
Creating a virtual directory in Apache web server for a cakephp project.
DESCRIPTION
Cakephp uses mod_rewrite module, this is needed because cakephp uses friendly urls, example:
unfriendly url: http://www.blogger.com/post-create.g?blogID=3570594853912202313
friendly url: http://www.serviprod.com/users/add
Mod_rewrite module allows you to modify your URL being identified with a regular expression pattern and replaced with another url and reinjecting this new url. In other words it is going to identify your friendly url with a regular expression and then converting your url to an unfriendly url and send it to the web server to be processed.
Activating mod_rewrite is sometimes tricky in Apache web server, and sometimes you need to be aware of the new url created if it is correct, but the problem is that you cannot see this new url in your browser, sometimes you are guessing what your new url might be.
What I want to create is a web site outside of the web root of Apache web server, that it is a cakephp web site in a windows environment.
SOLUTION
- Copy your cakephp framework (cake_1.2.1.8004) to the desired directory in my case it would be C:\Almacen\003.DES\serviprod
- Rename the cakephp framework folder to your desired web site name as specified in cakephp documentation. In my case I am going to rename the folder "cake_1.2.1.8004" to "serviprod"
- Need to find the correct Apache's web server configuration file (httpd.conf), this is important because in linux there must be more than one httpd.conf file, in windows environment there is no such problem but just in case.
- Need to find the following line "LoadModule rewrite_module modules/mod_rewrite.so" it is usually commented with a "#", you need to uncomment this line taking the "#" out. This is going to activate the mod_rewrite module in your apache web server.
- Your webroot it is determined by the following line: DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs", it is important to know where is your webroot to see that where you put your site is outside this root.
- You need to create an alias to tell the apache web server where is your site. Alias: Maps web paths into filesystem paths and is used to access content that does not live under the DocumentRoot. In my case the Alias I created is: Alias /serviprod "C:/Almacen/003.DES/serviprod/serviprod" where /serviprod it is going to be de way the site is accessed and the rest is the physical path of the site.
- Now you need to give apache access to those files, so you need to create a directory segment with the appropiate security:
Note the FollowSymLinks option that it is going to let you rewrite urls specified in the .htaccess file in the main directory of your cakephp site. And AllowOverride All to let the .htaccess file change the urls.
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
- The .htaccess doesn't know that the site it is been accessed thru an alias, so when it needs to reinject the new url, it is going to be not with the alias name, so we need to add a line to a .htaccess file: rewritebase /youralias. In my case the .htaccess it is going to be like follows:
RewriteEngine on
RewriteBase /serviprod
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
with the RewriteBase /serviprod line added to the original file.
INTRODUCCIÓN
Cakephp es un excelente framework de php para crear sitios web rápidamente y profesionales. Existen algunos trucos para configurar correctamente el servidor de web Apache con php si se quiere configurar un sitio web en un directorio diferente (directorio virtual) fuera del area principal de www.
PROBLEMA
Crear un directorio virtual en el servidor web Apache para un proyecto del framework cakephp.
DESCRIPCIÓN
Cakephp usa el módulo mod_rewrite, esto es necesario porque cakephp usa urls amigables, ejemplo:
url no amigable: http://www.blogger.com/post-create.g?blogID=3570594853912202313
url amigable: http://www.serviprod.com/users/add
El módulo Mod_rewrite permite modificar los URL identficados con una expresión regular y reemplazados con otro url y reinyectándolo. En otras palabras va a identificar el url amigable con una expresión regular y luego convirtiendolo a un url no amiable y enviándolo al servidor web para ser procesado.
Activar mod_rewrite algunas veces es truculento en el servidor de web Apache, y algunas veces necesitará estar pendiente del url creado si es correcto, porque el problema es que no puede ver este nuevo url en su navegador, algunas veces se adivina cual es este nuevo url.
Lo que yo quiero crear es un sitio web fuera de la raiz web del servidor web Apache, que es un sitio web cakephp en un ambiente windows.
SOLUCIÓN
- Copie el framework cakephp (cake_1.2.1.8004) al directorio deseado en mi caso será: C:\Almacen\003.DES\serviprod
- Renombre la carpeta del framework cakephp al nombre deseado del sitio web como se especifica en la documentación. En mi caso voy a renombrar la carpeta "cake_1.2.1.8004" a "serviprod"
- Es necesario encontrar el archivo de configuración del servidor web Apache (httpd.conf), esto es importante porque en linux deben existir más de un archivo de estos httpd.conf , en el ambiente windows no hay tales problemas, pero solo por si acaso.
- En el archivo de configuración se necesita encontrar la siguiente linea "LoadModule rewrite_module modules/mod_rewrite.so" está usualmente comentada con un "#", es necesario quitarla de modo comentario quitando el "#". Esto va a activar el módulo mod_rewrite en el servidor web Apache.
- La raiz de sitios web (webroot) está determinado por la siguiente linea: DocumentRoot "C:/Program Files/Apache Software Foundation/Apache2.2/htdocs", es importante saber donde está su webroot para ver que se colocó el sitio fuera de esta raiz.
- Es necesario crear un "alias" para decirle al servidor web Apache donde se encuentra el sitio. Alias: Convierte caminos web (web paths) en caminos del sistema de archivos (filesystem paths) y es usado para acceder a contenido que no está bajo la raiz web (DocumentRoot). En mi caso el Alias creado es: Alias /serviprod "C:/Almacen/003.DES/serviprod/serviprod" donde /serviprod será la manera de que el sitio es accesado y el resto es el camino físico hacia el sitio.
- Ahora se necesita dar acceso al Apache a esos archivos, así que se necesita crear una sección Directory con la seguridad apropiada:
Note que la opción FollowSymLinks permitirá reescribir urls especificados en el archivo .htaccess en el directorio principal de su sitio cakephp. Y la opciópn AllowOverride All le permitirá al archivo .htaccess cambiar los urls.
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
- El archivo .htaccess no sabe que el sitio ha sido accesado a través de un Alias, así que cuando necesite reinyectar el nuevo url, no será con el nombre del alias, así que será necesario agreagar una linea al archivo .htaccess: RewriteBase /tualias. En mi caso el archivo .htaccess será como sigue:
RewriteEngine on
RewriteBase /serviprod
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
Con la linea RewriteBase /serviprod agregada con respecto al archivo original.
No comments:
Post a Comment