Apache virtual host not working on Ubuntu 14.04 LTS

6,529

Solution 1

A slightly edited version of the Virtual Host definition in RiggsFolly's answer on stackoverflow worked for crm2plus.ddns.net

<VirtualHost *:80>

    ServerName crm2plus.ddns.net
        ServerAlias crm2plus.ddns.net
    ServerRoot /var/www/crm2plus.ddns.net/
    DocumentRoot /var/www/crm2plus.ddns.net/public_html

    <Directory "/var/www/crm2plus.ddns.net/public_html">
        Options +Indexes +FollowSymLinks
        Order allow,deny
        Allow from all
        AllowOverride All
    </Directory>


    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    ErrorLog ${APACHE_LOG_DIR}/crm2plus.ddns.net_error.log
    CustomLog ${APACHE_LOG_DIR}/crm2plus.ddns.net_access.log combined


</VirtualHost>

Solution 2

It looks like that the second .conf file has not been loaded. This can be done by including Include path/to/conf_files/*.conf in conf/apache2.conf and restarting apache.

Note that the access to crm2plus.ddns.net should be logged in the access log. Otherwise there is a misconfiguration.

VirtualHosts could be configured as follows.

  1. Ensure that both FQDNs are pointing to the IP of the Apache2 Server. This could be verified by pinging the FQDNs.
  2. Implement small changes and verify the outcome of each step.

This basic example has been implemented in apache2.conf:

NameVirtualHost *:80

<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/mydomain.com
    ServerName mydomain.com
    ErrorLog logs/mydomain.com-error_log
    CustomLog logs/mydomain.com-access_log common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/myseconddomain.com
    ServerName myseconddomain.com
    ErrorLog logs/myseconddomain.com-error_log
    CustomLog logs/myseconddomain.com-access_log common
</VirtualHost>

mkdir /var/www/mydomain.com && echo helloworld > /var/www/mydomain.com/index.html

mkdir /var/www/myseconddomain.com && echo helloworld2 > /var/www/myseconddomain.com/index.html

and it works, i.e. navigating to mydomain.com displays helloworld and myseconddomain.com results in helloworld2 once the service has been restarted.

The suggestion is to start from scratch and implement two basic VirtualHosts to ensure that the basics work. Once this has been implemented other changes could be made.

Share:
6,529

Related videos on Youtube

shhasan
Author by

shhasan

Updated on September 18, 2022

Comments

  • shhasan
    shhasan over 1 year

    I've been using this tutorial to set up an Apache virtual host on my Ubuntu pc. I've created a no-ip domain which I will be using as the hostname of my virtual host. I've followed every step of the tutorial but it doesn't work. Here's the virtual host file and it's named crm2plus.ddns.net.conf:

    <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 [email protected]
            ServerName  crm2plus.ddns.net
            ServerAlias crm2plus.ddns.net
            DocumentRoot /var/www/crm2plus.ddns.net/public_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
    
            # 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>
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    </Directory>
    <Directory /var/www>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order Allow,Deny
        Allow from all
    </Directory>
    # vim: syntax=apache ts=4 sw=4 sts=4 sr noet
    

    I've also added the ip and domain name pair to my local hosts file and here's how it looks:

    23.253.21.201   shhasan.ddns.net
    127.0.1.1       Ubuntu-Dev
    127.0.0.1       localhost
    99.250.71.177   crm2plus.ddns.net
    
    # The following lines are desirable for IPv6 capable hosts
    ::1     ip6-localhost ip6-loopback
    fe00::0 ip6-localnet
    ff00::0 ip6-mcastprefix
    ff02::1 ip6-allnodes
    ff02::2 ip6-allrouters
    

    When I navigate to crm2plus.ddns.net through chrome I get the web page not available page. shhasan.ddns.net is working fine. When I type ssh-keygen -H -F crm2plus.ddns.net into the terminal nothing shows up but when I type ssh-keygen -H -F shhasan.ddns.net this is what shows up:

    # Host shhasan.ddns.net found: line 14 type RSA
    

    followed by the RSA key.

    I've also looked at the Apache error and access logs. The error logs have a lot of repetition of such line of code:

    [Fri Nov 14 17:44:50.304782 2014] [mpm_prefork:notice] [pid 21927] AH00163: Apache/2.4.7 (Ubuntu) PHP/5.5.9-1ubuntu4.5 configured -- resuming normal operations
    [Fri Nov 14 17:44:50.304801 2014] [core:notice] [pid 21927] AH00094: Command line: '/usr/sbin/apache2'
    [Fri Nov 14 17:44:54.979832 2014] [mpm_prefork:notice] [pid 21927] AH00169: caught SIGTERM, shutting down
    

    I searched for caught SIGTERM, shutting down and I found that this was part of normal operation. I have no idea of where I'm going wrong. It had worked previously for shhasan.ddns.net but it doesn't work for crm2plus.ddns.net.

    All help and suggestions are greatly appreciated.


    Update:

    developer@Ubuntu-Dev:/etc/apache2$ ls apache2.conf apache2.conf.dpkg-dist conf-enabled httpd.conf magic mods-enabled ports.conf~ sites-enabled apache2.conf~ conf-available envvars httpd.conf~ mods-available ports.conf sites-available

    developer@Ubuntu-Dev:/etc/apache2/sites-available$ ls 000-default.conf 000-default.conf~ 000-default.conf.dpkg-dist crm2plus.ddns.net.conf crm2plus.ddns.net.conf~ default.save default-ssl.conf shhasan.ddns.net.conf

    developer@Ubuntu-Dev:/etc/apache2/sites-enabled$ ls crm2plus.ddns.net.conf shhasan.ddns.net.conf

    apache2.conf shortened version:

    Include /etc/phpmyadmin/apache.conf
    ServerName localhost
    
    Mutex file:${APACHE_LOCK_DIR} default
    
    PidFile ${APACHE_PID_FILE}
    
    Timeout 300
    
    KeepAlive On
    
    MaxKeepAliveRequests 100
    
    KeepAliveTimeout 5
    
    User ${APACHE_RUN_USER}
    Group ${APACHE_RUN_GROUP}
    
    HostnameLookups Off
    
    ErrorLog ${APACHE_LOG_DIR}/error.log
    
    LogLevel warn
    
    IncludeOptional mods-enabled/*.load
    IncludeOptional mods-enabled/*.conf
    
    Include ports.conf
    
    <Directory />
        Options FollowSymLinks
        AllowOverride All
    #    Order deny,allow
    #    Deny from all
    </Directory>
    
    <Directory /usr/share>
        AllowOverride None
        Require all granted
    </Directory>
    
    <Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
    
    #<Directory /srv/>
    #   Options Indexes FollowSymLinks
    #   AllowOverride None
    #   Require all granted
    #</Directory>
    
    AccessFileName .htaccess
    
    <FilesMatch "^\.ht">
        Require all denied
    </FilesMatch>
    
    
    LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
    LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %O" common
    LogFormat "%{Referer}i -> %U" referer
    LogFormat "%{User-agent}i" agent
    
    IncludeOptional conf-enabled/*.conf
    
    IncludeOptional sites-enabled/*.conf
    

    /etc/phpmyadmin/apache.conf file:

    # phpMyAdmin default Apache configuration
    
    Alias /phpmyadmin /usr/share/phpmyadmin
    
    <Directory /usr/share/phpmyadmin>
        Options FollowSymLinks
        DirectoryIndex index.php
    
        <IfModule mod_php5.c>
            AddType application/x-httpd-php .php
    
            php_flag magic_quotes_gpc Off
            php_flag track_vars On
            php_flag register_globals Off
            php_admin_flag allow_url_fopen Off
            php_value include_path .
            php_admin_value upload_tmp_dir /var/lib/phpmyadmin/tmp
            php_admin_value open_basedir /usr/share/phpmyadmin/:/etc/phpmyadmin/:/var/lib/phpmyadmin/:/usr/share/php/php-gettext/:/usr/share/javascript/
        </IfModule>
    
    </Directory>
    
    # Authorize for setup
    <Directory /usr/share/phpmyadmin/setup>
        <IfModule mod_authn_file.c>
        AuthType Basic
        AuthName "phpMyAdmin Setup"
        AuthUserFile /etc/phpmyadmin/htpasswd.setup
        </IfModule>
        Require valid-user
    </Directory>
    
    # Disallow web access to directories that don't need it
    <Directory /usr/share/phpmyadmin/libraries>
        Order Deny,Allow
        Deny from All
    </Directory>
    <Directory /usr/share/phpmyadmin/setup/lib>
        Order Deny,Allow
        Deny from All
    </Directory>
    
    • Eamonn Travers
      Eamonn Travers over 9 years
      Hi. Look in the log files & see if the Chrome machine is connecting with the server when you get the error message. You see it in ${APACHE_LOG_DIR}/access.log
    • 030
      030 over 9 years
      Are you using ubuntu desktop? Are you connecting to Apache2 from the same machine?
    • shhasan
      shhasan over 9 years
      Yes I'm using Ubuntu desktop and I'm connecting from the same machine.
  • shhasan
    shhasan over 9 years
    I'm not sure of what you mean by "IP of the Httpd server" but I've tried changing /etc/hosts file to this but it still didn't work as intended: ` 23.253.21.201 crm2plus.ddns.net` All else is the same as before in this file. @utretch
  • 030
    030 over 9 years
    @shhasan Did you deploy crm2plus.ddns.net.conf?
  • shhasan
    shhasan over 9 years
    sudo a2ensite crm2plus.ddns.net.conf --> Site crm2plus.ddns.net already enabled
  • 030
    030 over 9 years
    @shhasan does navigating to shhasan.ddns.net show the default apache page or the expected? Can one navigate to it as well or is it an internal site? RiskMP is your site?
  • shhasan
    shhasan over 9 years
    I see the expected page. You can navigate to shhasan.ddns.net and yes RiskMP is the site.
  • 030
    030 over 9 years
    Navigating to crm2plus.ddns.net causes an infinite loop. This should be seen in the log.
  • 030
    030 over 9 years
    It seems that the IP of both fqdns is 99.250.71.177
  • 030
    030 over 9 years