Missing POST and GET with Apache Alias - php

I have a Symfony project with the file index.php in /var/www/public.
I want to only allow access to the website using a virtual generate folder, so the site should always be accessed like this: http://localhost/generate.
http://localhost/ should just throw a 404.
I'm using the following Apache config, but the $_POST and $_GET globals are missing when I debug my script (I'm firing a POST request from Postman). It works when I reset my Apache config to just serve from the root without any alias..what is going on?
I'm not using any .htaccess files in my /var/www/public folder.
The Apache config:
<VirtualHost _default_:80>
ServerAdmin webmaster#localhost
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<FilesMatch ".+(\.php)$">
SetHandler "proxy:fcgi://127.0.0.1:9100/
</FilesMatch>
DocumentRoot "/var/www/public"
Alias /generate /var/www/public
<Location /generate>
AllowOverride All
</Location>
</VirtualHost>

Since you are using Symfony, you can simply define routes for /generate and nothing for /
routes.yaml
Generate:
path: /generate
controller: ControllerNamespaceAndName::MethodName
Because you don't use route definition for / when accessing http://localhost/, symfony's kernel will throw a 404

Related

Symfony 5: 404 page for /lucky/number application

I just started learning Symfony and try create 1th application by article https://symfony.com/doc/current/page_creation.html
WebServer: Apache 2.4.41
Server: Ubuntu 20
PHP: 7.4.3
I created domain on server symfony.
Installed in /var/www/html/symfony/ by command symfony new --full ./
Add required files (controller and write route to config/routes.yaml). Installed composer require symfony/apache-pack.
Execute $ php bin/console debug:router:
Name
Method
Scheme
Host
Path
...
app_lucky_number
ANY
ANY
ANY
/lucky/number
Opened in browser http://server.ip/symfony/lucky/number and get 404 Apache error.
Opened in browser http://server.ip/symfony/public/ and OK.
Opened in browser http://server.ip/symfony/public/index.php/lucky/number and OK.
Apache config in /etc/apache2/.../symfony.conf:
<VirtualHost *:80>
ServerName symfony
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html/symfony/public
<Directory /var/www/html/symfony/public>
Options FollowSymlinks
Require all granted
#AllowOverride All
Order Allow,Deny
Allow from All
#FallbackResource /index.php
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
mod_rewrite is enable.
Anybody have any ideas how to fix it?
You need to add .htaccess in your project.
If you will use only apache configuration, you will be enabled FallbackResource index.php in your Directory
Following this configuration : https://symfony.com/doc/current/setup/web_server_configuration.html
See section : Use the following optimized configuration to disable .htaccess support and increase web server performance:

Apache setHandler only only works on index

I am using docker for local development on a PHP powered craft website. My shared host requires a SetHandler php70-cgi directive, but the version of apache in the official PHP container does not have php-cgi and does not interpret PHP scripts. Because the container is development only, and not an attempt to 1:1 replicate production, I'm attempting to work around this with a custom apache config that sets an InDocker request variable:
<VirtualHost *:80>
ServerAdmin me#mydomain.com
DocumentRoot /var/www/craft/public
SetEnv InDocker true
<!-- ... more config -->
</VirtualHost>
And an .htaccess in my public folder that conditionally checks for the request env and only runs the:
Header set InDocker "%{InDocker}e"
<FilesMatch \.php$>
<If "!reqenv('InDocker') =~ /true/">
Header set IfMatch "We are not in docker"
SetHandler php70-cgi
</If>
Header set FilesMatch "yo"
</FilesMatch>
When I make a request to the root of my application, things render correctly and I see the following headers:
http -h :4020/
InDocker: true
FilesMatch: yo
However, if I make a request to another path, things do not render correctly and I can see the following headers are set:
http -h :4020/
InDocker: true
FilesMatch: yo
IfMatch: We are not in a container
Is there a way I can get this to work using my If or a better way to accomplish conditionally using SetHandler?
I could not get this to work using SetEnv and if reqenv so I had to fall back to using Define and IfDefine.
I added Define InDocker to my container's apache config:
<VirtualHost *:80>
ServerAdmin me#mydomain.com
DocumentRoot /var/www/public
Define InDocker
<Directory /var/www/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order deny,allow
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And then used IfDefine in my .htaccess file:
<IfDefine !InDocker>
<FilesMatch \.php$>
SetHandler php70-cgi
</FilesMatch>
</IfDefine>
This allows me to keep things as-is on my shared host and use the same .htacess for development.

How to configure Apache2 with Laravel Backend/React Frontend

I'm attempting to setup a server configuration where React is the frontend and Laravel is the API, but I'm unsure of how to setup Apache2 so that mydomain.com/api will serve Laravel and everything else will serve React
I tried setting up a separate site config file for Laravel at mydomain.com/api being routed to the Laravel public folder but it doesn't work
Any advice is appreciated
Thanks
Zach
I had this before and fixed it as below.
I did not create a new configuration file for the second site, just included it as Alias as below.
in your 000-default.conf
<VirtualHost *:80>
ServerAdmin root#example.com
ServerName example.com
ServerAlias www.example.com
Alias /api/ /var/www/html/api
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Alias defines your new site or application.
When you are setting up the Alias, if "/api/" does not work, try /api. either one of them should work.
And make sure, you set your document root configurations in the /etc/apache2/apache2.conf

Configure htaccess to symfony project

I have a hosting with my personal project in Symfony2 installed it inside a folder "public_html", then when I want to access to my project I have to write the next
url: "mydomian.net/myprojectSymfony/web/", instead of "mydomain.net".
If I just write mydomain.net the server shows me the directory "public_html" with all folders inside him. How can I write my .htaccess file to solve this problem?
I tried with Redirect 301 but I don't know if the right way.
In my opinion the best approach is to configure your apache vhost as explained there:
http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html
<VirtualHost *:80>
ServerName mydomain.net
ServerAlias www.mydomain.net
DocumentRoot /var/www/myprojectSymfony/web
<Directory /var/www/myprojectSymfony/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>

Apache alias directive

I am trying to set up simpleSAML.php but I am running into an issue with the Apache Alias directory. I have successfully used Alias so that when the user visits domain.com/auth then it uses the folder /var/saml/www.
However, for some reason it just lists the contents of the folder /var/saml/www rather than running the index.php file.
I have included the relevant virtual host below.
<VirtualHost *:80>
ServerAdmin helpdesk#domain.com
DocumentRoot /var/www/html/open
ServerName open.domain.com
ErrorLog logs/open.domain.com-error_log
CustomLog logs/open.domain.com-access_log common
Alias /auth/ /var/saml/www
Alias /auth /var/saml/www
<Directory /var/saml/www>
Options All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
You can add the following line to your config:
AddType application/x-httpd-php .php
This way apache knows it has to parse the files using php.
P.S. I think you may have to restart Apache rather than a reload.
Update
As Alfabravo commented:
Agree. He also needs to check the DirectoryIndex directive and add index.php to it

Categories