简体   繁体   中英

How can I serve multiple websites from specific paths on Apache?

I have two projects - site_a and site_b - that I would like to host on an Ubuntu server running Apache. I want these two projects to be accessed at paths {domain}/site_a and {domain}/site_b , respectively; I am not interested in having separate domains for them.

With the exception of each project's public/ directory, each project contains files that I do not want to be publicly accessible. So, I have stored the projects in /home instead of in /var/www .

I have tried specifying ServerName in both projects' .conf files and <Directory> in apache2.conf , but am unsure on how to make this work. At the moment, whenever I access {domain}/ in a browser, I get shown the contents of site_a/public .

Here is what I tried so far:

site_a.conf:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName localhost/site_a
    DocumentRoot /home/site_a/public

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

site_b.conf:

<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName localhost/site_b
    DocumentRoot /home/site_b/public

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

apache2.conf:

<Directory /home/site_a>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>
<Directory /home/site_b>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

Since you want both sites to respond on the same domain, you can only have 1 VirtualHost. Apache has no way to know what is site A and site B based on the domain if you only have 1.


One domain, 2 sites

<VirtualHost *:80>
    ServerName example.com
    ServerAlias www.example.com
    ServerAdmin admin@example.com

    documentRoot "YOUR_DIRECTORY_PATH/apache/htdocs"
    DirectoryIndex index.html

    ErrorLog "logs/80_error_log"
    CustomLog "logs/80_access_log" combined
</VirtualHost>

Setup your files like this:

  • under YOUR_DIRECTORY_PATH/apache/htdocs
  • mkdir site_a : put your files for site a here
  • mkdir site_b : put your files for site b here

One domain, 2 ports

<VirtualHost *:80>
    SITE A CONFIG
</VirtualHost>

<VirtulaHost *:81>
    SITE B CONFIG
</VirtualHost>

To see site a: http://example.com/
To see site b: http://example.com:81/

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