Apache VirtualHost and localhost

后端 未结 11 1023
广开言路
广开言路 2020-12-07 09:48

I\'m working with XAMPP on Mac OS X.

I\'m trying to run a Symfony website properly for a client, and I really don\'t know Symfony (yet). I just want to install and la

相关标签:
11条回答
  • 2020-12-07 10:27

    I had the same issue of accessing localhost while working with virtualHost. I resolved it by adding the name in the virtualHost listen code like below:

    In my hosts file, I have added the below code (C:\Windows\System32\drivers\etc\hosts) -

    127.0.0.1   main_live
    

    And in my httpd.conf I have added the below code:

    <VirtualHost main_live:80>
        DocumentRoot H:/wamp/www/raj/main_live/
        ServerName main_live
    </VirtualHost>
    

    That's it. It works, and I can use both localhost, phpmyadmin, as well as main_live (my virtual project) simultaneously.

    0 讨论(0)
  • 2020-12-07 10:36

    According to this documentation: Name-based Virtual Host Support

    You may be missing the following directive:

    NameVirtualHost *:80
    
    0 讨论(0)
  • 2020-12-07 10:37

    This worked for me!

    To run projects like http://localhost/projectName

    <VirtualHost localhost:80>
       ServerAdmin localhost
        DocumentRoot path/to/htdocs/
        ServerName localhost
    </VirtualHost>
    

    To run projects like http://somewebsite.com locally

    <VirtualHost somewebsite.com:80>
         ServerAdmin webmaster@example.com
         DocumentRoot /path/to/htdocs/somewebsiteFolder
         ServerName www.somewebsite.com
         ServerAlias somewebsite.com
    </VirtualHost>
    

    Same for other websites

    <VirtualHost anothersite.local:80>
         ServerAdmin webmaster@example.com
         DocumentRoot /path/to/htdocs/anotherSiteFolder
         ServerName www.anothersite.local
         ServerAlias anothersite.com
    </VirtualHost>
    
    0 讨论(0)
  • 2020-12-07 10:40

    I am running Ubuntu 16.04 (Xenial Xerus). This is what worked for me:

    1. Open up a terminal and cd to /etc/apache2/sites-available. There you will find a file called 000-default.conf.
    2. Copy that file: cp 000-default.conf example.local.conf
    3. Open that new file (I use Nano; use what you are comfortable with).
    4. You will see a lot of commented lines, which you can delete.
    5. Change <VirtualHost *:80> to <VirtualHost example.local:80>
    6. Change the document root to reflect the location of your files.
    7. Add the following line: ServerName example.local And if you need to, add this line: ServerAlias www.example.local
    8. Save the file and restart Apache: service Apache2 restart

    Open a browser and navigate to example.local. You should see your website.

    0 讨论(0)
  • 2020-12-07 10:41

    You may want to use this:

    <VirtualHost *:80>
        DocumentRoot "somepath\Apache2.2\htdocs"
        ServerName localhost
    </VirtualHost>
    <VirtualHost *:80>
    

    as your first virtual host (place it before another virtual hosts).

    0 讨论(0)
  • 2020-12-07 10:42

    localhost will always redirect to 127.0.0.1. You can trick this by naming your other VirtualHost to other local loop-back address, such as 127.0.0.2. Make sure you also change the corresponding hosts file to implement this.

    For example, my httpd-vhosts.conf looks like this:

    <VirtualHost 127.0.0.2:80>
        DocumentRoot "D:/6. App Data/XAMPP Shared/htdocs/intranet"
        ServerName intranet.dev
        ServerAlias www.intranet.dev
        ErrorLog "logs/intranet.dev-error.log"
        CustomLog "logs/intranet.dec-access.log" combined
    
        <Directory "D:/6. App Data/XAMPP Shared/htdocs/intranet">
            Options Indexes FollowSymLinks ExecCGI Includes
            Order allow,deny
            Allow from all
            AllowOverride All
            Require all granted
        </Directory>
    </VirtualHost>
    

    (Notice that in <VirtualHost> section I typed 127.0.0.2:80. It means that this block of VirtualHost will only affects requests to IP address 127.0.0.2 port 80, which is the default port for HTTP.

    To route the name intranet.dev properly, my hosts entry line is like this:

    127.0.0.2 intranet.dev
    

    This way, it will prevent you from creating another VirtualHost block for localhost, which is unnecessary.

    0 讨论(0)
提交回复
热议问题