Configuring symfony and apache - linux

I'm learning symfony right now. I want to use apache webserver instead of the shipped php. How can I add a virtualhost if I want to use also the default localhost? (localhost:80 for my notebook and I have a "personalhomepage")

Here is one of the vHosts I am currently using (cronolog is used for log rotation):
<VirtualHost *:80>
ServerName projectname.dev
DocumentRoot /var/www/projectname/html/web
<Directory /var/www/projectname/html/web>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog "|/usr/bin/cronolog /var/www/projectname/log/%Y-%m/error_%d_%m_%Y.log"
CustomLog "|/usr/bin/cronolog /var/www/projectname/log/%Y-%m/access_%d_%m_%Y.log" combined
</VirtualHost>
For testing and development purposes I usually create a local domain with the .dev or .local ending in the hosts file, e.g.:
127.0.0.1 projectname.dev
Make sure the domain name corresponds with the one in the vHost config. Oh, and don't dont forget to restart apache. Hope this helps.
Using these steps, you can use either projectname.dev, projectname.dev/app_dev.php or you set the vHost's DirectoryIndex directly, if you want to always use app_dev.php:
<Directory /var/www/projectname/html/web>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex app_dev.php
</Directory>

You would setup Apache in the same way as any other webservers that have more than one site on them - ideally, a named vhost. Symfony has an example Apache vhost config,
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
ServerAlias www.sitename.127.1.0.1.xip.name
DocumentRoot /var/www/project/web
<Directory /var/www/project/web>
AllowOverride None
Order Allow,Deny
Allow from All
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
# In local development, I default this to app_dev.php
</IfModule>
</Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
In this example, domain.tld (or www.) is the name you would use to reach the site. You would have to be able to reach those names via DNS though - on your local machine, 127.0.0.1. There are also some 'wildcard DNS' services that may help, such as http://xip.name/ With services such as these, you could put ServerName sitename.127.1.0.1.xip.name into the above configuration and then use that to reach your site.
I do something similar myself with a wildcard DNS sub-domain I own for a local machine. I have a number of such Apache Vhost configurations setup.

Related

Getting 404 on a specific path /pricing with Apache server [migrated]

This question was migrated from Stack Overflow because it can be answered on Server Fault.
Migrated 2 days ago.
I've set up an Apache server on Ubuntu 20.04
The site loads fine when I load the home page first (https://leadzilla.ai) and after that when I click on the pricing button and it takes me to https://leadzilla.ai/pricing and the that page loads fine as well.
But when I go directly to https://leadzilla.ai/pricing in the browser, I get a 404
Here is what I have in /etc/apache2/sites-available/leadzilla.ai.conf
<VirtualHost *:80>
DocumentRoot /var/www/leadzilla.ai
ServerName leadzilla.ai
ServerAlias www.leadzilla.ai
<Directory /var/www/leadzilla.ai>
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =leadzilla.ai [OR]
RewriteCond %{SERVER_NAME} =www.leadzilla.ai
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I have this config in /etc/apache2/sites-available/leadzilla.ai-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /var/www/leadzilla.ai
ServerName leadzilla.ai
ServerAlias www.leadzilla.ai
<Directory /var/www/leadzilla.ai>
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
#Deny from all
#Allow from 127.0.0.1
#Allow from ::1
</Directory>
<Directory /var/www/leadzilla.ai/blog>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/leadzilla.ai/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/leadzilla.ai/privkey.pem
</VirtualHost>
</IfModule>
I have Wordpress on https://leadzilla.ai/blog so that has to be taken care of as well.
Here is what I have tried:
RewriteRule ^pricing$ pricing.html [NC]
I added it before the other rewrite rule but that doesn't seem to be working. Any ideas?
[EDIT]
This is solved now. The issue was a Next.js config, not an Apache config.
I put in exportTrailingSlash: true in my module.exports and it worked
I find this to be curious behavior. But if you have a RewriteRule in the <VirtualHost *:443>, then you should also have RewriteEngine On.
Are there any symbolic links in your directory at all. Anything like foo -> foo.html?
Are there any directories like /var/www/html/pricing/ in your directory structure?
Also, remember, that all of your traffic ends up on HTTPS, which means that only the <VirtualHost *:443> is in play. The other virtual host entry only is used long enough to redirect from HTTP to HTTPS. Any rewrite rules for the :80 VirtualHost do not apply on HTTPS.

Changing the ServerAlias gives me an Index Of Page

I'm a bit of a newbie when it comes to Server related stuffs, but basically I'm having the most frustrating problem right now...
My configuration file before being changed:
<VirtualHost *:80>
ServerAdmin xyz#gmail.com
DocumentRoot /var/www/xyz.net/public_html
ServerName www.xyz.net
ServerAlias xyz.net
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<Directory "/var/www">
AllowOverride All
Order allow,deny
allow from all
</Directory>
Which is great, and it sort of worked. Although, going to www.xyz.net would load and you can see the content, however, going to xyz.net would show me an Index Of page. So I changed the config file, just switching the name and alias:
<VirtualHost *:80>
ServerAdmin xyz#gmail.com
DocumentRoot /var/www/xyz.net/public_html
ServerName xyz.net
ServerAlias www.xyz.net
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<Directory "/var/www">
AllowOverride All
Order allow,deny
allow from all
</Directory>
However, this doesn't seem to work, how come I'm still getting an Index Of page? Why is it that www.xyz.net has all the page content, yet xyz.net doesn't?! I'm really confused.
I've been trying to get www to redirect to non-www, at first I presumed it was an error with my .htaccess file, but after trying various methods of redirecting and even doing it in PHP, I presumed it was Apache screwing me over. So here's the code I use for the htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [L,R=301]
edit:
Thanks for all the help, but everything loads fine on www.xyz.net, however on xyz.net an Index Of page is shown, and there are no indexed items in the list, so presumably it's just an empty web page.
Check if there is somewhere another virtualhost with ServerName: xyz.net. In such case, that other virtualhost can take precedence over yours virtualhost.
Under Linux based systems, there is a default config file which can contain this ServerName. By default, it's location is /etc/apache2/sites-available/default. The file can be also named 000-default.conf

AllowOverride all causes error 403

I recently created a virtual host with WampServer (Version 2.4) so I can access my local website with the url http://yannbergonzat.local :
<VirtualHost *>
ServerName yannbergonzat.local
DocumentRoot "F:\Projets\Web\yann"
DirectoryIndex index.php
</VirtualHost>
And here is the Directory tag from httpd.conf :
<Directory />
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
This website is made with Symfony2, so yannbergonzat.local leads to a folder. If you want to access the actual website, you have to go to yannbergonzat.local/web/app.php .
I created a .htaccess file so the website can be seen with yannbergonzat.local :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ web/$1 [QSA,L]
</IfModule>
And now is the problem. Depending on the value of AllowOverride, there are errors.
When AllowOverride is set on "all"
The .htaccess file is read, the site can be accessed on yannbergonzat.local, but the resources (stylesheets, javascripts) which are in another folder cannot be accessed (error 403)
When AllowOverride is set on "none"
The .htaccess file is not read, the site can not be accessed on yannbergonzat.local (it shows the content of the folder), you have to write the entire url (yannbergonzat.local/web/app.php), but the resources (stylesheets, javascripts) which are in another folder can be accessed.
What should I do to have the website on yannbergonzat.local with the resources from other folders working fine ?
This change to httpd.conf is VERY DANGEROUS
<Directory />
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
as it allows all access to the whole of the drive you installed WampServer( Apache ) onto, if thats the C:\ drive you are making a hackers life very easy!
You should change httpd.conf that back to
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
And add the directory specification to your VHOST definition like this
<VirtualHost *:80>
ServerName yannbergonzat.local
DocumentRoot "F:\Projets\Web\yann"
DirectoryIndex index.php
<Directory "F:\Projets\Web\yann">
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
</VirtualHost>
AllowOverride none is an instruction to Apache to ignore any .htaccess files, so that explains what you have described.
When setting up a Symfony2 site this is the recommended config for a Virtual Host setup. You specify the /web folder as the DocumentRoot
See documentation
So I am assuming (you dont specify the actual folder structure) you would need something like this:
<VirtualHost *:80>
ServerName yannbergonzat.local
DocumentRoot "F:/Projets/Web/yann/web"
DirectoryIndex index.php
<Directory "F:/Projets/Web/yann/web">
# enable the .htaccess rewrites
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
This will almost definitely fix your 403 error as you are now granting access Require all granted to the folder that the site actually exists in.
This code worked for me:
sudo chmod -R 755 /var/www/***
Makes sure SELinux is not in not blocking access to your web directory:
chcon -R -u system_u -t httpd_sys_content_t /home/user/www

Setting IP in Apache vhost as testing domain

I was wondering if I can set the server external IP as a domain for testing.
On my Amazon server setup it works because all the domains point to the same folder.
domain1.com - /var/www/domain1
domain2.com - /var/www/domain2
ex.ter.nal.ip - /var/www/
So I can use ex.ter.nal.ip/test for a testing website.
On my Linode setup I moved the folders inside the user directories
domain1.com - /home/user1/public/domain1
domain2.com - /home/user2/public/domain2
ex.ter.nal.ip goes to domain2.com (I think it points to the last enabled site in Apache -a2ensite)
Is there a way to make it work?
I tried adding a vhost with with the /home/test DocumentRoot, but then all the websites point to the test directory.
My domain vhost file looks like this:
<VirtualHost *:80>
ServerName www.domain1.com
ServerAlias domain1.com
DirectoryIndex index.html index.php
DocumentRoot /home/user1/public/domain1.com/public
<Directory /home/user1/public/domain1.com/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
My testing one looks lithe this, but when enabled all the domains point to the test folder
<VirtualHost ex.ter.nal.ip>
ServerName ex.ter.nal.ip
DirectoryIndex index.html index.php
DocumentRoot /home/test/public
<Directory /home/test/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>

Mod-Rewrite loading files behind the DocumentRoot

I'm using .htaccess and mod_rewrite to point to files that reside behind the DocumentRoot. My folder structure looks like this:
home/
webroot/
other_files/
I have a .htaccess file in webroot with the following content:
RewriteEngine on
RewriteRule ^(.*)$ /home/other_files/$1
If I try to access http://example.com/file.html I receive the following error:
The requested URL /home/other_files/file.html was not found on this server.
Is it even possible to load files that are behind the DocumentRoot? If so, can someone point me in the right direction?
I believe you need to add a section with
<Directory "/home/other_files">
(options)
</Directory>
to your server configuration before apache will be able to serve anything from it. For an example, my DocumentRoot is /var/www but there is this section in the default available site:
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
You could then rewrite a URL to go to /doc/ and the server would know where to get the files from.
Just so you know why that rule doesn't work:
The reason that it isn't able to rewrite to /home/other_files/file.html is that mod_rewrite is parsing the path as /home/webroot/home/other_files/file.html since from mod_rewrite's point of view the preceding slash is equivalent to your document root of /home/webroot.
Ryan Ahearn's suggestion is a decent one, and is likely the route you want to go.
The credit goes to Ryan Aheam, but I'm going to spell it out. I'm a beginner and even with Ryan's answer I had to experiment with a few things to get the syntax right.
I wanted my DocumentRoot to be my cakephp directory. But then I had a Mantis Bug tracker that was just regular PHP and so not in the cakephp directory. The the files below I have the following working.
http://www.example.com : served by /var/www/cakephp
http://www.example.com/mantisbt : served by /var/www/html/mantisbt
File /etc/httpd/conf/httpd.conf
Alias /mantisbt/ "/var/www/html/mantisbt/"
<Directory "/var/www/html/">
AllowOverride All
</Directory>
<VirtualHost *:80>
ServerAdmin me#my_email.com
DocumentRoot /var/www/cakephp
ServerName my_website.com
<Directory /var/www/cakephp/>
AllowOverride All
</Directory>
</VirtualHost>
File /var/www/cakephp/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^mantisbt/?$ /mantisbt/ [NC,L]
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>

Resources