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.
Related
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
I inherited a php/Laravel app that was running on an Apache server that I don't have access to. My task is to get it running on another Apache server. I'm pretty good with php but relatively new to Laravel and very new to Apache configuration.
I have figured out how to get the Laravel app running on Apache that is running on an Ubuntu VM (VirtualBox.) I can access the Laravel app in a browser on the Ubuntu VM via http://localhost. I can also access the Laravel app in a browser from the Internet via http://appname.com/public. However, if I just use http://appname.com, then I just get a folder listing of /var/www/appname.
I have tried several modifications to the /etc/apache2/available-sites/appname.conf file but haven't quite got it right yet, apparently. I have also read a number of posts around the nets about making modifications to various other config files including php config files and Apache config files. It seems like these other mods (while they may be workable) shouldn't be necessary.
Here is my current /etc/apache2/available-sites/appname.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName appname.com
ServiceAlias www.appname.com
DocumentRoot /var/www/appname/public
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Any advise is appreciated.
Bob
You need to allow the mod_rewrite in the apache server and allowSymLinks.
Source
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName appname.com
ServiceAlias www.appname.com
DocumentRoot /var/www/appname/public
<Directory "/var/www/appname/public">
Options FollowSymLinks
ReWriteEngine On
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
in the DocumentRoot Directory i would also allow MultiViews
<Directory "/var/www/appname/public">
Options FollowSymLinks MultiViews
ReWriteEngine On
</Directory>
You may need to also do
sudo a2enmod rewrite
to enable module rewrite.
Edit 1:
In my .conf files i got them with the quotes and they are working.
Did you enable the modudle rewrite?
Besides some options i also have the "/" folder with the next config.
<Directory "/">
Options FollowSymLinks
AllowOverride All
ReWriteEngine On
</Directory>
and here i'll write my full code of public directory
<Directory "/var/www/appname/public">
Options FollowSymLinks MultiViews
Order Allow,Deny
Allow from all
ReWriteEngine On
</Directory>
Try it and see if it works, after delete the options that you don't like to use.
Follow the steps and all will be good and easy,
1). Type following command in terminal
cd /etc/apache2/sites-available
2). Make a new config file
sudo cp 000-default.conf appname.dev.conf
3. Open the new config file and paste the following code
<VirtualHost *:80>
# The ServerName directive sets the request scheme, hostname and port that
# the server uses to identify itself. This is used when creating
# redirection URLs. In the context of virtual hosts, the ServerName
# specifies what hostname must appear in the request's Host: header to
# match this virtual host. For the default virtual host (this file) this
# value is not decisive as it is used as a last resort host regardless.
# However, you must set it for any further virtual host explicitly.
#ServerName www.example.com
ServerAdmin yourmail#example.com
ServerAlias appname.dev
DocumentRoot /var/www/html/appname/public
<Directory /var/www/html/appname/public>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
<FilesMatch \.php$>
#Change this "proxy:unix:/path/to/fpm.socket"
#if using a Unix socket
#SetHandler "proxy:fcgi://127.0.0.1:9000"
</FilesMatch>
</Directory>
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible to configure the loglevel for particular
# modules, e.g.
#LogLevel info ssl:warn
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
# For most configuration files from conf-available/, which are
# enabled or disabled at a global level, it is possible to
# include a line for only one particular virtual host. For example the
# following line enables the CGI configuration for this host only
# after it has been globally disabled with "a2disconf".
#Include conf-available/serve-cgi-bin.conf
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
4). CTRL+x, then press y then press enter and run following command in terminal
sudo a2ensite appname.dev.conf
5). Type following command and edit the /etc/hosts file
sudo nano /etc/hosts
127.0.0.1 appname.dev
press CTRL x then press Enter and type following command
sudo service apache2 restart
6). Now your app will execute on appname.dev successfully.
Lets imagine we have two different apache virtual hosts and we want one host to be php web-site and the other one - .net core. The simple question - how to configure apache (httpd.conf/php.conf) to make this happen?
For php we already have directives:
AddHandler php5-script .php and AddType text/html .php, for the php virtual host we have settings:
<VirtualHost 10.10.20.22:80>
ServerName phpsite.ru
ServerAlias www.phpsite.ru
DocumentRoot /var/www/html/phpsite.ru/public
<Directory /var/www/html/phpsite.ru/public >
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog /var/www/html/phpsite.ru/public/logs/error.log
CustomLog /var/www/html/phpsite.ru/public/logs/access.log common
</VirtualHost>
What should we do to make a second virtual host handle through .net core?
I have deployed my web.py application using apache2 and the configuration looks as follows.
<VirtualHost _default_ *:80>
ServerAdmin admin#project.com
DocumentRoot /var/www/html
ErrorLog /var/www/Engine/log/error.log
CustomLog /var/www/Engine/log/access.log combined
WSGIScriptAlias / /var/www/Engine/Engine.py
Alias /static /var/www/html
AddType text/html .py
WSGIDaemonProcess www-data threads=15
WSGIProcessGroup www-data
WSGIApplicationGroup %{GLOBAL}
</VirtualHost>
<Directory /var/www/html>
allow from all
</Directory>
Problem:
I have some php services hosted inside /var/www/html/apis . Those used to work before configuring with mod_wsgi, but when i request now, it says 404 error.
Question:
How to configure both Python and php services work together?
Try adding:
Alias /apis/ /var/www/html/apis/
Also see:
http://blog.dscpl.com.au/2014/09/hosting-php-web-applications-in.html
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