#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    205
    Rep Power
    17

    Problem with Apache and Virtual Hosts


    Server OS: CentOS v.7
    Web Server: Apache v.2.4.6

    I have made the following changes to the httpd.conf file:
    Code:
    ...
    
    ServerName www.mydomain.net:80
    
    ...
    
    DocumentRoot "/var/www/html"
    
    ...
    
    # For .htaccess files...
    AllowOverride All
    
    ...
    
    # Virtual Host Files
    IncludeOptional sites-enabled/*.conf
    So in this new Apache setup, all the virtual host stuff is located in the "sited-enabled" directory. And here's that file called "mydomain.net.conf" ...

    The path is /etc/httpd/sites-enabled/mydomain.net.conf

    Code:
    <VirtualHost *:80 *:443>
        ServerName www.mydomain.net
        ServerAlias mydomain.net
        DocumentRoot /var/www/html/mydomain.net/public_html
        ErrorLog /var/www/html/mydomain.net/error.log
        CustomLog /var/www/html/mydomain.net/requests.log combined
    </VirtualHost>
    So what's going on is that when I punch www.mydomain.net into the browser, Apache should load the index.html document that I have in here: /var/www/html/mydomain.net/public_html/index.html

    However, it does not. Instead it loads an index.html document that I have (to prevent directory listings, although there is a directive in httpd.conf that addresses directory listings as well): /var/www/html/index.html

    When Apache reads httpd.conf and then IncludeOptional sites-enabled/*.conf and then /etc/httpd/sites-enabled/mydomain.net.conf, it should be serving /var/www/html/mydomain.net/public_html/index.html

    Incidentally, I have an SSL cert (Let's Encrypt), and so everything is redirected from http to https.
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,333
    Rep Power
    9645
    Did you restart Apache?

    Originally Posted by Arty Zifferelli
    Incidentally, I have an SSL cert (Let's Encrypt), and so everything is redirected from http to https.
    ...if you explicitly set that up.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2016
    Location
    Lakewood, WA
    Posts
    205
    Rep Power
    17
    Originally Posted by requinix
    Did you restart Apache?
    Of course.
    ...if you explicitly set that up.
    I did.
  6. #4
  7. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,333
    Rep Power
    9645
    If you don't have a default virtualhost for /var/www/html that is accidentally covering mydomain.net then it sounds like there's a typo somewhere.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    937
    Rep Power
    279
    To my knowledge, you can't put ssl and non-ssl ports in same virtualhost declaration.



    Since you are using LetsEncrypt, I would assume that you want the non-ssl(http) traffic to be redirected to ssl(https).
    You need to setup 2 virtualhosts like so:

    Code:
    <VirtualHost *:80>
        ServerName www.mydomain.net
        ServerAlias mydomain.net
        
        Options FollowSymLinks
        RewriteEngine On
        RewriteCond %{SERVER_PORT} !^443$
        RewriteRule ^(.*) https://%{HTTP_HOST}/
    </VirtualHost>
    
    <VirtualHost *:443>
        ServerName www.mydomain.net
        ServerAlias mydomain.net
        DocumentRoot /var/www/html/mydomain.net/public_html
        ErrorLog /var/www/html/mydomain.net/error.log
        CustomLog /var/www/html/mydomain.net/requests.log combined
        
        <Directory "/var/www/html/mydomain.net/public_html">
            Options Indexes FollowSymLinks ExecCGI Includes
            AllowOverride All
            Require all granted
        </Directory>
        <FilesMatch "^\.ht">
            Require all denied
        </FilesMatch>
        DirectoryIndex index.html index.php index.htm
        
        <IfModule mod_headers.c>
          Header always set Strict-Transport-Security "max-age=15768000; includeSubDomains; preload"
        </IfModule>
    
    SSLEngine on
    SSLProxyEngine on
    SSLCertificateFile /path/to/letsencrypt/mydomain.net/cert.pem 
    SSLCertificateKeyFile /path/to/letsencrypt/mydomain.net/privkey.pem 
    SSLCertificateChainFile /path/to/letsencrypt/mydomain.net/chain.pem 
    </VirtualHost>
  10. #6
  11. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,079
    Rep Power
    4101
    The Standard HTTP vhost could be simplified to just:
    Code:
    <VirtualHost *:80>
            ServerName example.com
            ServerAlias www.example.com
    
    
            Redirect permanent / https://www.example.com/
    </VirtualHost>
    No need for mod_rewrite for such a simple redirect. There's no need to check the port number either as apache wouldn't be processing this vhost if it wasn't port 80.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2009
    Location
    Nebraska, USA
    Posts
    937
    Rep Power
    279
    thanks for that info, kicken.
    I don't know why I had never heard of that method before, but, it works a treat.

IMN logo majestic logo threadwatch logo seochat tools logo