Redirect Symfony URL - apache

I have a Symfony3 installation on a server and I need to access the web files through the IP/game address.
So I created my "game" symfony folder with everything in it, and for now if I want to see my web content I need to add /web/app.php (or app_dev.php, nvm), which is logical.
So here is my question : How can I access my app.php (or app_dev.php) just by IP/game and not IP/game/web/app.php (or app_dev.php).
I can't configure any vhost to define a DocumentRoot so I think I have to use an htaccess but all my tests actually have failed :(
Thank you for your help and have a nice day !

The only way for this to work is always by accessing ( IP or hostname )/( app_dev.php for dev, nothing for prod )/routes
If you want to access dev environment you will need to use app_dev.php after hostname / IP. Everything after that is not related to file structure but with routing specified in configuration of Symfony application ( at least by default web server configuration which is provided on Symfony website ).

Related

Seamlessly hosting different parts of a website in different docker containers

I have a website that I am trying to convert to a docker-compose solution.
Part of the website is a CMS (WordPress, specifically), and there are also a few Java web applications that are currently handled by tomcat.
The current approach is that I have an official WordPress container and an official Tomcat container. I load my custom content in the WordPress (via a MySQL container which is not really a part of this question) and the WAR files into tomcat.
So the problem is this:
Parts of the WordPress website have links to Java applications. Previously, some of these links were relative links as simple as /Application1 and the user would be able to access the Java application.
Now, since they are hosted in separate containers, I cannot do this because there is nothing named "Application1" in the WordPress container. I have modified the htaccess file for the WordPress container with a RewriteRules like this:
# Tomcat exposes port 8085
RewriteRule ^.*Application1/(.*)$ http://localhost:8085/Application1/$1 [R,L]
But this means that if the user clicks the link for Application1 in wordpress, their browser will then redirect them to localhost:8085 - which works fine if you run this set of containers locally, but does not work for remote users.
I could change localhost to the actual name of the server this will run on. But then it would need to be manually updated for every server this stack will run on, or reset to localhost for developers.
I could combine WordPress and Tomcat into a single container, but we were hoping to keep them separate so that when we need to upgrade, we just change the version of the container we are pulling rather than having to rebuild a custom container from scratch.
Is there a more seamless way to do this, so that the URL in the user's browser never changes? Is there way to get this working so that remote users won't even know that the different parts of the site are hosted in different containers?
Assuming you do docker-compose in development and also in production, you replace localhost with servicename of the corresponding upstream.
Lets say, you have 3 containers: httpd, fpm ( wordpress) and java ( tomcat ). You configure httpd, to redirect to fpm:9000 if its domain.tld/php and redirect to java:8086 if the request is domain.tld/Application1.
All you need to understand is, that the actual service-name you have in docker-compose is available as a hostname in the container ( all containers ) - so containers can reach themselfs using the service-name of the other container - that makes inter-container communication very easy and pre-configureable, without knowing the actual container IPs ( which will change during development / production and host migrations )
I think I figured it out: I should have been using a proxy instead of a RewriteRule.
This seems to get me what I want:
ProxyPass "/Application1" "http://my-tomcat-server:8080/Application1"
ProxyPassReverse "/Application1" "http://my-tomcat-server:8080/Application1"

htaccess - simulating local page as web page

Is there a way to rewrite a URL in a local project to look like web page?
For example I have project with url
localhost/site
I'm tryin' to rewirite this to:
www.site.com
or
site.com
That project has subpages, and it would be good if it worked like
site.com/subpage.php
I'm trying for an hour but I'm really htaccess noob.
I work with VertrigoServ and mod_rewrite works fine with some examples which I tried in other projects.
The issue is not with rewrite. To make a local page appear to have a more conventional domain you need to tell your browser that the conventional domain is found on your local webserver.
The simplest way to do this is by editing the hosts file on your OS. On *nix based devices it's usually /etc/hosts, On windows it's usually C:\Windows\System32\drivers\etc\hosts. You will need elevated privileges to edit the file.
Add a line to the end of the file that maps the domain name to your local ip address like this:
127.0.0.1 www.site.com
Close an reopen your browser and visit www.site.com, you should see it is loading the page from your local webserver.
Chances are, you are still seeing the same page as if you view localhost site. To make it load your code you need to change the DocumentRoot in httpd.conf for your apache install to match the directory where your code is.
A preferable solution may be to use Name Based Virtual Hosting, this allows multiple web sites to share the same IP address. Searching 'apache VirtualHost examples' should give you plenty of resources for this. Make sure that NameVirtualHost *:80 is also enabled in order for this to work.

APACHE VirtualHosts configuration for Zend project and regular sites

I have such a problem:
In a local network I have a server which is called by its server_ip.
I have few sites located under /www/site_folder where index.php is located.
So whenever http://server_ip/site_folder is called index.php is called and selected site loads.
Now I put in /www/zend_site a Zend project.
It means two things:
1. index.php is located under /www/zend_site/public,
2. Zend requires rewriting rules set up so framework can understand controller/action extracted from an URL.
What I want, and can't succeed, is to have ability to call Zend project by typing http://server_ip/zend, and also to have rest of my simple sites working by calling them by http://server_ip/site_folder.
I got familiar with configuration for a Zend http://files.zend.com/help/previous-version/Zend-Server-5-Community-Edition/configuring_zend_framework.htm and tried to make use of virtual hosts by introducing new one called zend.
I see it's not possible to get such two existing options running. If zend is in virtual host and if I call only sever_ip it loads zend project instead of /www/index.php (a dummy site). If I remove virtual host after I call http://server_ip/zend it loads /www/zend/index.php instead of /www/zend/public/index.php and also site doesn't work (after I created symbolic link to proper index.php), because links are not correctly rewritten with controller/action extraction.
Or maybe I am thinking wrong about VirtualHosts idea? In server_ip/something <- something is not a domain which could be used in VirualHost in this case?....
Any idea if my configuration could work?
Regards

Install Symfony on remote server

I'm trying to evaluate Symfony 2 (2.1.7). I'm installing it following the download instructions on an EC2 instance that is already running PHP 5.3.20 on Apache.
I'm stuck on the second step of the README.md: "Access the config.php script from a browser". The readme assumes a local installation and provides a sample URL to the localhost: http://localhost/path/to/symfony/app/web/config.php.
Since I'm on a remote server, I try to access the config.php file using the relevant URL: http://mysite.com/Symfony/app/check.php, which returns this message:
Forbidden
You don't have permission to access /Symfony/app/check.php on this server.
I tried to apply the answer from How do I access to symfony config.php remotely? by adding what PHP reports back as my REMOTE_ADDR, but that doesn't change the message.
What do I do now?
In symfony, the web folder is supposed to be your webroot. So, if you want to access \project\web\config.php, you should point your browser to http://www.example.com/config.php.
If that doesnt work, apache is probably configured incorrectly. make sure it it is pointed at your web directory, not your project directory.
edit As you mention in your question, you will also need to edit the config.php file to allow remote access. You can comment those lines out, or add your IP to the whitelist.
edit2 Many webhosts don't allow you to specify your webroot. In that situation, you can put the Symfony files in a different directory and create a symlink between the Symfony web directory and your webroot.

How to set root directory to a subfolder (local php development environment)

I'm just starting to learn web dev, but got myself a bit confused when setting up my local development environment.
I had a php site on shared hosting for some time. I would edit local files and upload them, then refresh to ensure everything worked. A naughty practice I know!
So I have tried to set up a local dev environment and installed AMPPS for a local webserver (PHP5).
I have ambitiously set up the folder structure below so that I can code up a range of websites to try out different languages and frameworks whilst keeping them all seperate. (Square brackets denote folders). Note sure if this is the correct approach... (?)
-->[PHP]
---> [AMPPS]
----> [www]
-----> [PROJECTS]
------> [project_name]
-------> index.php
-------> .htaccess
-------> [images]
-------> [section1]
--------> page1.php
-------> [etc...]
------> [project_name]
------> [project_name]
--> [RUBY]
--> [PYTHON]
My first hurdle however is that all of my links were relative, and I had of course uploaded my site at root with my host. Root in my local dev enironment is now www, which as you can see is several folders up from where I have installed my site.
Links (e.g. Page 1) now display as: "localhost/section1/page1.php" when I believe they should in fact show as "localhost/PROJECTS/project_name/section1/page1.php".
I don't think that telling APACHE that my root directory is [project_name] is the correct approach, because then it wont work for other projects I create. I assume that I should be doing something on a project by project basis to specify it's own root directory.
I thought that I could specify root by adding an .htaccess file to the project_name folder:
From my online research I thought it would go something like:
RewriteEngine on
RewriteRule ^/$ /PROJECTS/project_name/
or perhaps
RewriteEngine on
RewriteRule ^/$ .;C/PHP/AMPSS/www/PROJECTS/project_name/
However I can't get these to work.
When the site was online, I had also coded <?php set_include_path($_SERVER['DOCUMENT_ROOT']); //Look for includes starting from ROOT location ?> at the top every page, thinking I was doing the right thing.
Now I am not so sure that I correctly understood what I was doing there. It doesn't seem to have any impact with or without it in my development environment. And I haven't been able to edit it in any way successfully to resolve my relative links issue.
Sorry for the long post.
I'm grateful for any and all feedback/assistance. Thanks in advance :)
[Resolved] - The advice below was perfect. Thank you all. :)
A tip for Chrome users:
If you are using a custom domain, like I did with .dev, either proceed the address with http://, or end it with a trailing /.
For example I now visit my dev site in Chrome by visiting projectname.dev/ (without the last / it tries to search).
At first I thought I had set up VirtualHost or hosts incorrectly, when in reality it was this Chrome behaviour that that was clouding the issue. At the time of writing, other browsers like Firefox don't share this concern.
Hope that helps someone else out.
You're correct that configuring Apache for each project is the way forward. It's best done with virtual hosts. Define a virtual host for each project and give it a domain name or subdomain for development. In my case I use test.com for all my local development and configure project1.test.com, project2.test.com etc so all my relative paths work as expected. For this I also edit my hosts file so the DNS lookup for test.com and sub projects resolve to my local machine.
Rewriting URLs is something I reserve for a project-by-project basis, without setting global rewrites for a specific server setup. That way my local development environment is close to the actual server environment and project-specific rewrites will work as expected.
Hope that helps
I recommend using method mentioned by #Tak, but I know there might be problems with pre-configured WAMP packs to add virtual hosts, so there is also a solution to use BASE tag in head section of your html:
<base href="http://localhost/PROJECTS/project_name/">