PHP Namespace collision with Phalcon, Vagrant, Apache2 - php

I have this strange problem that I can best describe as "namespace leakage". I have setup a vagrant box running Apache2 with VHosts setup to replicate the production server in terms of domains & sub domains. I have edited my local machine's hosts file accordingly, and my VHosts work fine.
I have also created a skeleton/base app coded with PhalconPHP. It's a multi modular app with all the generic features I require to develop my apps (actually redevelop a load of very old, outdated apps). The skeleton app works fine.
The problem I have is that if I go to app1.dev in my browser, it works. Then if I go to app2.dev, app2 is clearly trying to load some components - views etc from app1 and is giving errors. However, close the browser and try again by going to app2.dev and it now works fine. Then go to app1.dev and that is now broken and trying to load components from app2! I managed to track this odd behaviour down to namespace collision.
It has to be namespace collision because my apps are all based on the skeleton app and use it's name spaces, which are obviously the same for generic parts of the app - modules such as App\backend, App\frontend etc. If on a broken app, I navigate in my browser to a part of that app that is unique, and therefore has a unique namespace, it works fine because there is no collision! Also a couple of apps are coded with Codeigniter3 which does not use namespaces, and those apps do not have this issue. It only effects the Phalcon apps with namespaces.
I will add that each app uses .htaccess to direct requests to the front controller located in public/ directory.
Options FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
I wondered if the .htaccess was the issue, but I think I've ruled that out. It is doing what it is supposed to do.
Here's an example of one of my VHost settups in apache - they all follow this pattern.
<VirtualHost *:80>
ServerName app1.dev
ServerAlias www.app1.dev
DocumentRoot /vagrant/www/app1
<Directory "/vagrant/www/app1">
Options Indexes Followsymlinks
AllowOverride All
Order allow,deny
Allow from All
Require all granted
</Directory>
</VirtualHost>
Changing all the namespaces throughout every app would be a pretty major job, and I don't think that should be necessary. I don't think this should be an issue on the production server as that uses CloudLinux/Centos & WHM, but it is a bit of a worry!
Clearly namespaces should not collide across different document routes and VHosts right? What am I missing?

I have solved similar problem in Apache using different local IP's for every app:
/etc/hosts
127.1.0.1 edu.l
127.1.1.1 tech.l
...
/etc/apache2/sites-available/001-blogs.conf
<VirtualHost edu.l:80>
ServerName edu.l
ServerAdmin webmaster#localhost
DocumentRoot /var/www/blog/edu
ErrorLog ${APACHE_LOG_DIR}/edu.error.log
CustomLog ${APACHE_LOG_DIR}/edu.access.log combined
</VirtualHost>
<VirtualHost tech.l:80>
ServerName tech.l
ServerAdmin webmaster#localhost
DocumentRoot /var/www/blog/tech
ErrorLog ${APACHE_LOG_DIR}/tech.error.log
CustomLog ${APACHE_LOG_DIR}/tech.access.log combined
</VirtualHost>
...
You can ommit configuring names in hosts ans use IP's in .conf tho.
It works because if you define few apps on same IP and port, Apache seems to remember directory it is working on and mismatching files.

Related

How do you configure Symfony 4 to route alongside other XAMPP (Apache) projects?

I'm brand new to Symfony, so this question will undoubtedly come across as noobish. How do you get Symfony to work with XAMPP? I've read the Symfony documentation for configuring Apache, but can't make heads or tails of it.
XAMPP projects are stored in a single /htdocs directory. Each project has its own subfolder and is accessible via the following URL: http://localhost/project_folder. At least, that's how I've been doing it.
My problem is this: Symfony is different in that its root directory is not front-facing. In other words, to actually access a Symfony 4 site, you need to navigate to the /public directory. However, as my local XAMPP (i.e. Apache) server is configured to use a single DocumentRoot (/xampp/htdocs), my Symfony project is inaccessible unless I manually append /public to the URL.
What I want: http://localhost/symfony_project_folder
What I have: http://localhost/symfony_project_folder/public
I do not want to set up an entirely separate local server for a single web application, nor do I want to deal with an entirely different URL structure than what is used by my other projects.
How do I configure Symfony 4 or Apache to properly route to /public in this environment?
Thank you for your help.
Hi there indeed when you create a new symfony project you need to change the document root of the domain in public folder. A good way to work with symfony would be to use docker which helps you out keep projects separated and you can use phpdocker.io to generate a docker configuration very fast to start working with docker.
Anyway here is an example from symfony manual on how to setup a vhost.
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/public
<Directory /var/www/project/public>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeeScript assets
# <Directory /var/www/project>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
You can read more about web server configuration here

Development mode on Symfony2 virtual hosts

I was boring about my large url http://localhost:8888/site/web/app_dev.php, so, I created apache virtual hosts. When I write http://site.dev in my browser, everything is working, my website appears.
The problem is that my Symfony2 website seems to be in dev / prod mode whereas I point to app_dev.php (in the .htaccess).
I have the debug bottom toolbar (it shows me "dev environment"), I can do dumps, but when I modify a file (JS, Twig, and so on), I have to execute, each time, php app/console assetic:dump (I hadn't before).
My virtual host :
<VirtualHost *:80>
ServerName site.dev
ServerAlias www.site.dev
DocumentRoot /Applications/MAMP/htdocs/bo/web
<Directory /Applications/MAMP/htdocs/bo/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
</VirtualHost>
EDIT : I noticed that the website is so much faster like that (site.dev) than before (with the localhost URL). Don't know why, and still don't know how to fix the php app/console assetic:dump problem...
You need add this line DirectoryIndex app.php
<VirtualHost *:80>
ServerName site.dev
ServerAlias www.site.dev
DocumentRoot /Applications/MAMP/htdocs/bo/web
DirectoryIndex app.php
<Directory /Applications/MAMP/htdocs/bo/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
</VirtualHost>
Don't you have any other problems but worrying about the lengths of the URL? :)
But seriously, try starting the internal PHP/Symfony server with the following command:
$ app/console server:start
The development version of your app will be at http://localhost:8000. Nice and short.
Based on your comment, it turns out that the actual problem you have is hardcoding URLs in your JS. The solution for that is https://github.com/FriendsOfSymfony/FOSJsRoutingBundle.
Basically, that bundle lets you expose routes to your JS so that you avoid hardcoding URLs there.

How can you configure Apache to run CakePhP and raw PHP Apps as separate virtual hosts running side-by-side on the same server?

On my CentOS Linux VPS I have full LAMP installed. I have a CakePHP website (and intend to add a few more) as a virtual host. I also have a web based bug tracker called MantisBT that is implemented in PHP. I'd like to set up my Apache configuration file (/etc/httpd/conf/httpd.conf) to index both side-by-side. My websites are not in DNS. I access them using the raw IP number.
Here's my directory structure:
/var/www/
MyCakePhPApp/
html/
mantisbt/
Here's what I have in my httpd.conf file:
# This makes my MantisBT work under http:/<my_ip>/mantisbt
DirectoryIndex index.php index.html
# This makes my CakePhP work under http:/<my_ip>
<VirtualHost *:80>
ServerAdmin webmaster#tecadmin.net
ServerName example.com
DocumentRoot /var/www/MyCakePhPApp
<Directory /var/www/MyCakePhPApp>
Allowoverride All
</Directory>
</VirtualHost>
With this my CakePhP is accessible as "http:/my_ip" but my mantisbt site is not accessible. "http:/my_ip/mantisbt" goes to CakePhP and says that controller is missing. If I comment out the entire VirtualHost, then Mantis is indeed accessible as: "http:/my_ip/mantisbt", but of course then my CakePHP site is not accessible.
Does anyone know how to get both these websites working side-by-side?

Putting PHP and Rails on the same server

I am currently working on a website where the core of the website is in PHP. I now want to write a bunch of applications on top of that core, and was hoping to do it in Rails. I saw a couple things online where you could set single folders to be handled by PHP, (example: http://macdiggs.com/2007/06/29/using-php-inside-rails-structure-on-apache/) but I am hoping to do the opposite, have single folders that are handled by Rails, and then the rest is handled by PHP. For example, having ourwebsite.com/blog as a Rails app, but ourwebsite.com and ourwebsite.com/internal are all in PHP. What kind of Apache configurations would let this happen?
(As a bonus, my server is managed by Plesk, so I am concerned about making straight changes to the apache configuration. I have root access, so I can do it, but I am worried that Plesk might get mad)
EDIT: I should also mention, I am using Subdomains as part of my application, so I would really prefer to have something like ourwebsite.com/rails_app. If that is the only option, I can go that route, but I would prefer not to.
If you want the PHP app to be default application and only use Rails for a subdirectory, this Apache configuration should work for you:
DocumentRoot "/path/to/your/php/app/html"
ProxyPass /some_resource http://127.0.0.1:3000/some_resource
Note that your rails application would be running on port 3000 and you will need the ProxyPass Apache module installed.
I am working on a project and its having some blog in php ie wordpress and application in rails. Just configured it an hour before. Might help you.
<VirtualHost *:80>
ServerName abc.com
DocumentRoot /home/me/apps/my_rails_app/current/public
</VirtualHost>
<VirtualHost *:80>
ServerName blog.abc.com
DocumentRoot /home/me/apps/abc/wordpress
<Directory "/home/me/apps/abc/wordpress">
Options +Indexes FollowSymLinks
AllowOverride All
Allow from all
Order allow,deny
</Directory>
</VirtualHost>

Acessing other webapps while one is in document root

I have a Zend Framework project on a local machine, and as recommended its /public subfolder is made a DocumentRoot in httpd.conf. So I access this app simply with http://localhost.
This new requirement makes me unable to access other
web apps in a former webserver root (which contains a few regular php apps and a couple of zend framework apps).
What configuration should I choose to be able to simultaneously access both ZF root-based apps and other apps like PHPMYADMIN?
You'll probably need to use some kind of VirtualHost
You have at least two solutions :
VirtualHosts based on diferent port numbers
For instance, you'd have one site on http://localhost/ (ie, default port = 80)
And another one on http://localhost:8001/
And another one on http://localhost:8802/
And so on
VirtualHosts based on different domain-names
For instance, you'd have on site on http://localhost/
And another one on http://mytestwebsite/
In the second case (the solution I always use), you will have to edit your "hosts" file, so "mytestwebsite" is an alias to your local machine -- which IP address is 127.0.0.1
Under windows, this file is located in C:\WINDOWS\system32\drivers\etc
On Linux, it's in /etc/
You'd have to add a line like these ones :
127.0.0.1 localhost
127.0.0.1 mytestwebsite
Then, in the Apache configuration, you need to create one VirtualHost per site. Something like this, I suppose :
<VirtualHost *>
ServerName mytestwebsite
DocumentRoot /home/squale/developpement/tests
<Directory /home/squale/developpement/tests>
Options Indexes FollowSymLinks MultiViews +SymLinksIfOwnerMatch
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *>
ServerName myothertestwebsite
DocumentRoot /.../myothertestwebsite
<Directory /.../myothertestwebsite>
Options Indexes FollowSymLinks MultiViews +SymLinksIfOwnerMatch
AllowOverride All
</Directory>
</VirtualHost>
(Needs tunning / configuration, of course)
And you'll also probably need some directive like this one :
NameVirtualHost *
Well, this might not be the entire solution (that one depends on your server / applications) ; but I hope these few pointers will help you get to it !
If you need more help, the keyword is "VirtualHost" ;-)
Have fun !
Zend Framework probably uses a .htaccess file. If so, you might use that to add rules to leave the other apps alone. Another option is to use subdomain (eg phpmyadmin.localhost)

Categories