Set different data/folders for different domains on apache - apache

So I have Ubuntu 16 installed with LAMP and couple of other things on it (like FTP server...).
IP of my VPS is 1.2.3.4.
I have 2 domains, example.com and mydomain.com - both of these domains have their A record pointed at 1.2.3.4
If I access 1.2.3.4 and example.com and mydomain.com in my browser, all those three are showing the SAME data, specifically the contents of /var/www/html - if I change content of this directory it affects example.com and mydomain.com and the 1.2.3.4.
Now how can I set additional folders for domains to read from? I want different data on example.com and on mydomain.com - I want to make them read from different folder on server. How can i setup this please?

What you want to achieve is technically defined as setting virtual hosts which could be set using the following steps:
Under your Apache configurations directory, usually at /etc/apache2/, you will find a directory named sites-enabled.
For each of your domains, you will need to configure a special configuration file in order to point them to the right direction. The name of the file is typically your-domain.conf.
Here's an example of the file:
<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 your_domain
ServerAlias www.your_domain
ServerAdmin webmaster#your_domain
DocumentRoot website_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
<Directory website_directory>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
After saving the file you will need to restart the Apache server
/etc/init.d/apache2 restart
Repeat the above steps for each of your domains.
If everything goes right, your site will show when you access your domain.

Related

IP Address showing instead of Domain apache server

I just purchased a domain from GoDaddy, let's call it example.com. I set up my A record and CNAME to properly point to my home IP address (see picture below) where I run an Apache2 server on my Raspberry Pi.
My 000-default.conf file is as follows:
<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
ServerAlias example.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ErrorDocument 404 /404.html
<Directory "/var/www/html">
AllowOverride All
Options All
Require all granted
# Any other directives
</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
I have searched the internet for hours and even asked another question on StackOverflow to where I thought the problem was resolved, however after about an hour of restarting the Apache2 service on the Raspberry Pi, it goes back to showing my home IP Address in the address bar instead of my newly purchased domain. I have tried restarting the service multiple times but I always end up with the same result. Flushing DNS on my devices does nothing, too. How can I make it so that it always shows my domain in the address bar?
Make sure that in your Domain provider (GoDaddy for example) that you aren't forwarding to your IP address. Instead, forward to your domain.

Apache2 open SSL certificates are shown on AWS Load balancer

I'm trying to configure SSL certificates on AWS ec2 Instance, I followed the following steps:
Created instance on ec2 with ubuntu OS.
Issued a certificate through certificate manager on my domains -> Ex: *.domains.com and domains.com
Created a Classic Load balancer to point these certificate and ec2 instance.
Configured A record in Route 53 to alias of load balancer
Configured my virtual host on apache2
$sudo openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache-selfsigned.key -out /etc/apache2/ssl/apache-selfsigned.crt
I obtained self signed certificates with all the necessary details which was asked.
And configured my virtual host something like this:
<VirtualHost *:443>
# 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 support#noeticitservices.com
ServerName ssltest.domains.com
ServerAlias ssltest.domains.com
DocumentRoot /var/www/html/****/public
ErrorLog /var/www/html/****/error.log
CustomLog /var/www/html/****/access.log combined
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache-selfsigned.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache-selfsigned.key
<Directory /var/www/html/****/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
Require all granted
</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>
<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 support#noeticitservices.com
ServerName ssltest.domains.com
ServerAlias ssltest.domains.com
DocumentRoot /var/www/html/****/public
ErrorLog /var/www/html/****/error.log
CustomLog /var/www/html/****/access.log combined
<Directory /var/www/html/****/public>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
Require all granted
</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>
When I access my domain it is not taking Amazon Issued Certificates. How can I point the certificates, I've gone through almost all the resources available on Internet. My default-ssl.conf is also enabled mod_ssl is also enabled. Help me out with this.
Configured A record in Route 53 to alias of load balancer
OK, here's where I think you messed up. dig ns biltrax.com gives me the following nameservers for your domain:
ns34.domaincontrol.com
ns33.domaincontrol.com
I believe these are GoDaddy's. Whatever they are, they're not Route53's nameservers. As such, the records you have in Route53 have no effect - they're ignored, as your domain's DNS is handled by a different service entirely.
If you want Route53 to be responsible for the domain, you'll have to point the domain at the Route53 nameservers at your registrar.

Apache does not pick up default index file when I enter domain name

I installed an ubuntu server 16.04 and LAMP, it works very well and I can load default web by using ip address.
Also I created a new virtual host by domain synappse.ir. If I enter synappse.ir/index.html, it loads the page and there is not any issue but when I enter just the name of the domain without index.php, it can not find or load the index.html file!
Here is my config file for virtual host :
<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 ServerNamei
# 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 nasser.man#gmail.com
ServerName synappse.ir
ServerAlias synappse.ir
DocumentRoot /var/www/synappse.ir
DirectoryIndex index.html
# 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
#<Directory /var/www/html>
# Options Indexes FollowSymLinks MultiViews
# AllowOverride All
# Require all granted
#</Directory>
# 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>
Is there any other configuration for enabling this feature?
Sorry, pages were cached, every thing is ok.

Apache vhost loads incorrect doc root

My issue is that Apache randomly loads a non-default vhost instead of the domain I ask it to. I've tried just about every solution I found on this site and through searching but nothing seems to fix this problem.
Background:
This is a virtual server hosted on a popular cloud platform
The global apache conf file contains no VirtualHost definitions and the config file below is included properly (Include sites-enabled/*.conf)
Apache 2.4 on Ubuntu 16.04
I had each domain in their own conf file but that did not work either.
Apache has chosen domain3.com as the default for some reason.
When I execute curl localhost on the server, it returns the contents of domain3.com.
The only thing excluded from the vhost below are some SSL definitions that don't seem to be causing any problems.
ServerName is defined as localhost in apache.conf
The vhosts below are saved at /etc/apache2/sites-enabled/000-default.conf
Server weirdness?
I can't seem to actually stop apache. Executing service apache2 stop and then service apache2 status will identify the process as inactive, but I can still load pages under domain1/2/3.com. It's like nothing I do has any effect.
There's like 3 ways to access apache now. Some seem to work, some don't. /etc/init.d/apache2, service apache2, apache2ctl.
It only loads the wrong site about 50% of the time.
I'm completely out of ideas...
NameVirtualHost *:80
# domain1.com
<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.domain1.com
ServerAlias domain1.com
UseCanonicalName Off
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain1.com/
<Directory /var/www/domain1.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</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>
# domain3.com
<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.domain3.com
ServerAlias domain3.com
ServerAlias domain3.net
ServerAlias www.domain3.net
UseCanonicalName Off
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain3.com/
<Directory /var/www/domain3.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</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}/cq2_error.log
CustomLog ${APACHE_LOG_DIR}/cq2_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>
# domain2.com
<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.domain2.com
ServerAlias domain2.com
UseCanonicalName Off
ServerAdmin webmaster#localhost
DocumentRoot /var/www/domain2.com/
<Directory /var/www/domain2.com>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</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>
And my apache2ctl -S output:
AH00548: NameVirtualHost has no effect and will be removed in the next release /etc/apache2/sites-enabled/000-default.conf:1
VirtualHost configuration:
*:80 is a NameVirtualHost
default server www.domain1.com (/etc/apache2/sites-enabled/000-default.conf:4)
port 80 namevhost www.domain1.com (/etc/apache2/sites-enabled/000-default.conf:4)
alias domain1.com
port 80 namevhost www.domain3.com (/etc/apache2/sites-enabled/000-default.conf:54)
alias domain3.com
alias domain3.net
alias www.domain3.net
port 80 namevhost www.domain2.com (/etc/apache2/sites-enabled/000-default.conf:95)
alias domain2.com
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex rewrite-map: using_defaults
Mutex ssl-stapling-refresh: using_defaults
Mutex ssl-stapling: using_defaults
Mutex ssl-cache: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33 not_used
Group: name="www-data" id=33 not_used
As far as Apache choosing a random site:
When you do curl localhost, it is likely that it sending out an HTTP header that has no Host: defined, as it behaves similarly to when you type the IP of the server directly in the browser. The Host: header in the HTTP request is the field that Apache matches against the ServerName and ServerAlias trying to find a match to load up the corresponding DocumentRoot.
So, in that case, if you don't have a default host setup, and an empty Host: comes in, then Apache will randomly choose a site (I have recreated this behavior on my dev server, just to verify).
It is better to set up 000-default.conf as your catch-all and decide what to do when that happens, like redirecting to one of your sites or to a simple php file that just echos something bland. Then set up separate conf files for each site just to stay organized.
If you have DNS records pointing to your server correctly, then the default will only trigger when bots come around testing IPs.
On Ubuntu 16.04 you have systemd .. so the right way to interact with apache is using systemctl <command> apache2
I suggest to kill every apache process and then try to start it again using systemctl start apache2 ..
Moreover, to be sure my default domain is loaded at first, I usually rename its configuration file as 000-domain.conf .. then add other domains, one per file, with no numerical prefix at all
Your configuration looks alright, the weirdness you seem to describe seem to be related to a external issue, being it distro scripts, or whatever..., so try to use only apachectl only to stop/start, if the distro hasn't changed it much is the most "official" method you will find to manage your httpd server start/stop, etc, plus it is the only thing you have now giving you correct information
Also, In your configuration domain3.com would not ever be your default virtualhost, not even if you had defined your servernames incorrectly. So chances are you are seeing what other "service/server" is sending you.
Answer:
Use apachectl only until you fix your discro script issues

Apache works on localhost but not on server name

As my user id suggests, I am a novice programmer.
I am trying a simple Apache server configuration in ubuntu,
I have created below .conf file in /etc/apache2/sites-available/awesome.conf with below code. Also I have created a symbolic link in /etc/apache2/sites-enable and restarted the Apache.
<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.myawesome.org
ServerAdmin webmaster#localhost
DocumentRoot /var/www/awesome
DirectoryIndex hello.php
# 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>
when I access localhost/awesome/hello.php I get the desired text "Hello World".
But when I type www.myawesome.org it dispays below message:
"Server not found"
Any help/guidance is appreciated.
Regards....
You need to edit the hosts file and add the ServerName here pointing to localhost
Open hosts file with:
sudo nano /etc/hosts
...and at the end of this file add:
127.0.0.1 www.myawesome.org