Set a default Virtual Host in Apache using Debian 10 - apache

I have a little problem.
I have a VPS configured with Apache, I have 2 VH, one is mydomain.com and other is subdomain.mydomain.com
Problem is that whenever I type the VPS IP in the browser, it goes to the subdomain. Is there a way to default this to the main domain?
I've created a folder called sites-enabled which contains both configuration files, they look something like this.
example.net.conf:
<VirtualHost *:80>
ServerName example.net
ServerAlias www.example.net
DocumentRoot /var/www/example.net
<Directory /var/www/example.net>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
admin.example.net.conf:
<VirtualHost *:80>
ServerName admin.example.net
DocumentRoot /var/www/admin.example.net
DirectoryIndex index.html
ErrorLog ${APACHE_LOG_DIR}/admin.example.net.log
CustomLog ${APACHE_LOG_DIR}/admin.example.net.log combined
</VirtualHost>
How can I set the first one to be the default?

place the below code in the 000-default.conf file
<VirtualHost *:80>
DocumentRoot /var/www/example.net
<Directory /var/www/example.net>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
<FilesMatch \.php$>
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost"
</FilesMatch>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Related

Why isn't my apache httpd server serving content from my VirtualHost

I have an apache httpd server running on a linux server. The server's fqdn is www.example.com, but I have a dns alias of www.example2.com for the same server.
When I access the server using my browser and use http://www.example.com it serves the correct content, but if I use http://www.example2.com, I get the default content from my /var/www/html folder not the content from the /var/www/html2 folder.
A stripped-down copy of my httpd.conf file follows:
ServerName www.example.com:80
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
CustomLog "logs/access_log" combined
ErrorLog "logs/error_log"
LogLevel warn
<VirtualHost www.example2.com:80>
ServerName www.example2.com:80
DocumentRoot /var/www/html2
CustomLog "logs/access_log2" combined
ErrorLog "logs/error_log2"
<Directory "/var/www/html2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName www.example2.com
DocumentRoot /var/www/html2
CustomLog "logs/access_log2" combined
ErrorLog "logs/error_log2"
<Directory "/var/www/html2">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
The VirtualHost tag is for IP based filtering, to filter by host\server name use the ServerName directive.

Apache virtualhost servername by ip + documentroot

I am trying to get this virtualhost working on apache + ubuntu. I want it to work without a real domainname but by ip and the path. It is now not getting activated. Is it possible to use the serverName like this or are only domain names valid?
<VirtualHost *:80>
serverName 37.35.66.98/just-do-it/
DocumentRoot /var/www/html/just-do-it/web
<Directory /var/www/html/just-do-it/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Remove path portion from server name, then this should work
<VirtualHost *:80>
serverName 37.35.66.98
DocumentRoot /var/www/html/just-do-it/web
<Directory /var/www/html/just-do-it/web>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
do not forget to add your server name in /etc/hosts file

Multiple Websites on Apache2

I have my apache configured to have 2 websites setup. I have the following in my apache2.conf
Include /opt/bitnami/apps/www.website1.com/conf/app.conf
Include /opt/bitnami/apps/www.website2.com/conf/app.conf
Here are the app.conf for the 2 websites
Website1
<VirtualHost *>
DocumentRoot /opt/bitnami/apps/www.website1.com/htdocs
ServerName www.website1.com:80
ServerAlias website1.com
ErrorLog /opt/bitnami/apps/www.website1.com/log/error.log
CustomLog /opt/bitnami/apps/www.website1.com/log/access.log common
<Directory "/opt/bitnami/apps/www.website1.com/htdocs">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *>
DocumentRoot /opt/bitnami/apps/www.website1.com/htdocs
ServerName website1.uat.com:80
ServerAlias website1.uat.com
ErrorLog /opt/bitnami/apps/www.website1.com/log/error.log
CustomLog /opt/bitnami/apps/www.website1.com/log/access.log common
<Directory "/opt/bitnami/apps/www.website1.com/htdocs">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Website 2
<VirtualHost *>
DocumentRoot /opt/bitnami/apps/www.website2.com/htdocs
ServerName www.website2.com:80
ServerAlias www.website2.com
ErrorLog /opt/bitnami/apps/www.website2.com/log/error.log
CustomLog /opt/bitnami/apps/www.website2.com/log/access.log common
<Directory "/opt/bitnami/apps/www.website2.com/htdocs">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
<VirtualHost *>
DocumentRoot /opt/bitnami/apps/www.website2.com/htdocs
ServerName website2.com:80
ServerAlias website2.com
ErrorLog /opt/bitnami/apps/www.website2.com/log/error.log
CustomLog /opt/bitnami/apps/www.website2.com/log/access.log common
<Directory "/opt/bitnami/apps/www.website2.com/htdocs">
Options Indexes MultiViews
AllowOverride None
Order allow,deny
Allow from all
</Directory>
Im testing these locally so i have my host setup below
xx.xxx.xx.xxx www.website1.com
xx.xxx.xx.xxx www.website2.com
When i go to www.website2.com, apache2 page pops up
When i go to www.website1.com, apache2 page pops up
When i go to www.website2.com/htdocs, i see the correct website and it works. When i got to www.website1.com/htdocs, i see website2. I dont understand why i am seeing website 2 here.
My first guess would be that you should remove the port :80 from ServerName and also change
<VirtualHost *>
to
<VirtualHost *:80>
Like so:
<VirtualHost *:80>
DocumentRoot /opt/bitnami/apps/www.website1.com/htdocs
ServerName www.website1.com
...
As prerik says use "VirtualHost *:80"
Also if it is Apache HTTPD 2.2.x, it needs "NamedVirtualHosts *:80" defined "once" in the config when several virtualhosts are present using the same ip:port scheme, if you don't add this all your requests will land in the first defined virtualhost.

Subdomain answer to specific folder in Apache

I want to configure an Apache vhost. Practically Basically I want by typing the address files.yyyyyy.ltd answer me the folder destination /var/www/html/alpha/files
This is apache configuration:
<VirtualHost *:80>
DocumentRoot /var/www/html/alpha/files
ServerName files.yyyyyy.ltd
ServerAlias files.yyyyyy.ltd
<Directory /var/www/html/alpha/files >
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
# Other directives here
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
DNS configuration:
yyyyyy.ltd. IN A XXX.XXX.XXX.XXX
www IN CNAME yyyyyy.ltd.
yyyyyy.ltd. IN CNAME files.yyyyyy.ltd.
What is wrong? Do you recommend some other method to achieve the same result?
Thk for help
DNS configuration
you need to change
yyyyyy.ltd. IN CNAME files.yyyyyy.ltd.
to
files IN CNAME yyyyyy.ltd.
you can also set all as A record like
yyyyyy.ltd. IN A XXX.XXX.XXX.XXX
www.yyyyyy.ltd. IN A XXX.XXX.XXX.XXX
files.yyyyyy.ltd. IN A XXX.XXX.XXX.XXX
What i would do to have a faster name resolution.
Apache configuration
Your Apache configuration is correct, but some stuffs are useless:
<VirtualHost *:80>
DocumentRoot /var/www/html/alpha/files
ServerName files.yyyyyy.ltd
<Directory /var/www/html/alpha/files >
Options Indexes FollowSymLinks MultiViews
AllowOverride all
</Directory>
# Other directives here
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
AllowOverride all is there to allow .htaccess, if you don't want .htaccess files, you can remove this command from your configuration.

Apache Virtual Host with path

I am deploying a Rails app with Phusion passenger and Apache HTTPD, to do that I have to create a virtual host in Apache to associate the root of my app. And this process is successful, effectively my app is running, but my problem is that I have to do this run in a path of my domain, such as "mydomain.com/railsapp".
I tried this configurations and all failed.**
Configuration 1
ServerAdmin admin#mydomain.com
ServerName mydomain.com/railsapp
RailsEnv production
RailsBaseURI /app
DocumentRoot /var/www/rails/app/public
<Directory /var/www/rails/app/public>
AllowOverride all
Options -MultiViews
#Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Configuration 2
ServerAdmin admin#mydomain.com
ServerName mydomain.com
ServerPath /railsappp
RailsEnv production
RailsBaseURI /app
DocumentRoot /var/www/rails/app/public
<Directory /var/www/rails/app/public>
AllowOverride all
Options -MultiViews
#Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Configuration 3
ServerAdmin admin#mydomain.com
ServerName mydomain.com
ServerAlias mydomain.com/railsappp
RailsEnv production
RailsBaseURI /app
DocumentRoot /var/www/rails/app/public
<Directory /var/www/rails/app/public>
AllowOverride all
Options -MultiViews
#Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Does Anyone have idea how to do that?
Use ServerName as my domain.com and make use of ProxyPass and ProxyPassReverse directive to redirect /railsapp to desired location.... Read more about reverse proxy..