简体   繁体   中英

Apache: Virtual Host configuration

As i tried to configure my virtual host in apache. I put something like this,

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot /xampp/htdocs/gift
   ServerName gift.loc  
</VirtualHost>

And in my hosts file i put something like this,

127.0.0.1       localhost
127.0.0.1       gift.loc

And i run it on the browser,

http://gift.loc - is fine

But when i tried using this,

http://localhost/othersite - can't found

Do i missed somehting to configure? ANy ideas...

Thanks in advance,

You need a VirtualHost entry for every host you want apache to handle. The first one in the config file will be used as the default if no other VirtualHosts match the request.

For example if we have:

<VirtualHost *:80>
   DocumentRoot /xampp/htdocs/gift
   ServerName gift.loc  
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot /example/htdocs/gift
   ServerName example.com  
</VirtualHost>

A request for foobar.org will get handled by the gift.loc virtual host.

you need to put localhost in the vhosts.conf

    NameVirtualHost *:80

    <VirtualHost *:80>
       DocumentRoot /xampp/htdocs/
       ServerName localhost
    </VirtualHost>

    <VirtualHost *:80>
       DocumentRoot /xampp/htdocs/gift
       ServerName gift.loc  
    </VirtualHost>

This works fine (Make sure you restart apache). If you need to check your configuration you can (on linux at least) run httpd -S.

There are few steps you need to follow to setup the virtual host on ubuntu : Let say that your project folder name is myProject

Step 1:Place your folder inside /var/www/html

sudo mv ~/myProject /var/www/html/

Step 2: Give ownership of project folder to www-data

sudo chown -R www-data:www-data /var/www/html/myProject

Step 3:Create new site inside Sites available:

cd /etc/apache2/sites-available/ 
ls

Here you will see existing 000-default.conf and default-ssl.conf .Copy the content of both file into one file and replace your folder name or copy this one into new file named myProject.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 webmaster@localhost
    DocumentRoot /var/www/html/myProject/
        ServerName project.com
        ServerAlias www.project.com

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>

<VirtualHost *:443>
        ServerAdmin webmaster@localhost

        DocumentRoot /var/www/html/myProject/
        ServerName project.com
        ServerAlias www.project.com          

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        SSLEngine on

        SSLCertificateFile  /etc/ssl/certs/mobidev_cert.pem
        SSLCertificateKeyFile /etc/ssl/certs/mobidev_key.pem


        <FilesMatch "\.(cgi|shtml|phtml|php)$">
                SSLOptions +StdEnvVars
        </FilesMatch>
        <Directory /usr/lib/cgi-bin>
                SSLOptions +StdEnvVars
        </Directory>

</VirtualHost>

Include the path of self signed certificate also in this as shown ssl key and ssl certificate that can be downloaded easily.

Step 4:Add your project into apache configuration file.

sudo vi /etc/apache2/apache2.conf

put this lines in the file:

DocumentRoot "/var/www/html/myProject"
<Directory /var/www/html/myProject/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

Step 5:Add your virtual server name (specified in myProject.conf) into host file.add that line:

sudo gedit /etc/hosts
127.0.1.1   project.com

Step 6:Now all set ,enable site,restart apache

sudo a2ensite /etc/apache2/sites-availabl/myProject.conf
sudo systemctl reload apache2
sudo update-rc.d apache2 defaults
sudo update-rc.d mysql defaults
sudo a2enmod ssl
sudo a2ensite default-ssl

Just hit project.com in your browser.

From the docs , it looks like we need to create a block for each different host that you would like to serve .

Further in the same doc, If you are adding virtual hosts to an existing web server, you must also create a block for the existing host.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM