Apache2 subdomain go to different folder - linux

I want to subdomain use different folder. For example: example.com use /var/www/example and blog.example.com use /var/www/exampleblog.
Right now if I go to example.com or www.example.com the website loads correctly, but if I go to blog.example.com I got DNS_PROBE_FINISHED_NXDOMAIN.
The virtualhost files:
example.conf
<VirtualHost *:80>
DocumentRoot /var/www/example/
ServerName example.com
ServerAlias www.example.com
<Directory "/var/www/example/">
AllowOverride All
Options FollowSymLinks
</Directory>
ErrorLog /var/log/apache2/example-error
</VirtualHost>
And:
exampleblog.conf
<VirtualHost *:80>
DocumentRoot /var/www/exampleblog
ServerName blog.example.com
<Directory "/var/www/exampleblog/">
AllowOverride All
Options FollowSymLinks
</Directory>
ErrorLog /var/log/apache2/exampleblog-error
</VirtualHost>
What did I incorrectly?

The error was: The domain registrar isn't sent out the new settings when I wrote the question. They wrote, that it will be about 15-20 minutes, but somewhy it took more time.

Related

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/

Httpd VHost not using correct DocumentRoot

I am having trouble getting a virtual host file to work correctly.
Here is my virtualhost file
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
DocumentRoot "/var/www/example.com/public_html"
SSLEngine on
SSLCertificateFile /etc/ssl/example.com.crt
SSLCertificateKeyFile /etc/ssl/example.com.key
# SSLCertificateChainFile /etc/ssl/origin_ca_rsa_root.crt
</VirtualHost>
<Directory "/var/ww/example.com/public_html">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
The issue is that when accessing the domain is pulling from the default path of /var/www/html instead of the documentroot specified in the conf file.
Within the httpd.conf file I have the IncludeOptional /sites-enabled/example.com.conf and it works correctly, including SSL however the DocumentRoot is not being pulled correctly.
Here is my httpd.conf file: https://pastebin.com/2cuqDwPY
System OS: AlmaLinux8

Htaccess doesn't work on MAMP with multiple sites

I have 3 sites on MAMP local server but in 2 of 3, the htaccess doesnt work.
I add this code in httpd.conf (MAMP->apache):
NameVirtualHost *
<VirtualHost *>
DocumentRoot "c:/MAMP/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *>
DocumentRoot "C:\codes\Bedloop"
ServerName local.bed.com
</VirtualHost>
<VirtualHost *>
DocumentRoot "C:\codes\apart"
ServerName local.apart.com
</VirtualHost>
And this lines on windows hosts file:
127.0.0.1 local.bed.com
127.0.0.1 local.apart.com
I put on 3 pages, the same files (same page), but htacces only work on the localhost page, not in other 2. The main page work fine, but urls with rewrite rules fails.
I need to config something more?
Thx!
NameVirtualHost is deprecated. Can you try these directives in your Apache vhost file and restart apache:
<VirtualHost *:80>
ServerName localhost
DocumentRoot "c:\MAMP\htdocs"
<Directory "c:\MAMP\htdocs">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName local.bed.com
DocumentRoot "C:\codes\Bedloop"
<Directory "C:\codes\Bedloop">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName local.apart.com
DocumentRoot "C:\codes\apart"
<Directory "C:\codes\apart">
Options Indexes FollowSymLinks MultiViews ExecCGI
AllowOverride All
</Directory>
</VirtualHost>

Rewrite not working, even though AllowOverride is set to All

I've installed LAMP stack on a digitalocean droplet running on Ubuntu. I have a file called "/restful/post.php".
using "https://url/restful/post.php" works just fine, however, I want to make it SEO friendly, sth like "/restful/post".
my htaccess is in the root folder "html/var/www" and has the following:
RewriteEngine on
RewriteRule ^/restful/post(.*)$ /restful/post.php?p=$1
I have got a2enmode enabled, AllowOverride is set to All in "/etc/sites-available/*".
for the 000-default.conf, I've got:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
Redirect / https:/url
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
and for the 000-default-le-ssl.conf, I've got:
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
All the answers to the questions I've seen didn't fix my issue and I'm not really sure what I'm missing here.

htaccess with virtualhost not working

I have a local dev Debx64 machine with a number of virtualhosts configured.
the primary URL is set as
<VirtualHost *:80>
ServerAdmin webmaster#blah.com
ServerName blah.com
ServerAlias blah.com
DocumentRoot /home/blah/v1.blah.com
<Directory /home/blah/v1.blah.com/>
AllowOverride All
Order allow,deny
allow from all
</Directory>
AccessFileName .htaccess
ErrorLog /home/blah/blah_logs/v1.blah.com.in-error_log
CustomLog /home/blah/blah_logs/v1.blah.com.in-access_log common
</VirtualHost>
and that redirects to the primary operating VH
<VirtualHost *:80>
ServerAdmin webmaster#blah.com
ServerName v1.blah.com
ServerAlias v1.blah.com
DocumentRoot /home/blah/v1.blah.com
<Directory /home/blah/v1.blah.com/>
AllowOverride All
Order allow,deny
allow from all
</Directory>
AccessFileName .htaccess
ErrorLog /home/blah/blah_logs/v1.blah.com.in-error_log
CustomLog /home/blah/blah_logs/v1.blah.com.in-access_log common
</VirtualHost>
I have a .htaccess set up on v1.blah.com to parse .html as .php
Options +ExecCGI
AddHandler php-fcgi .php .html
Action php-cgi /home/php5-fcgi
<FilesMatch "^php5?\.(ini|cgi)$">
Order Deny,Allow
Deny from All
Allow from env=REDIRECT_STATUS
</FilesMatch>
This works fine if I access the URL as v1.blah.com, however if I access it as blah.com the .htaccess is not invoked and the .html is served as normal.
What Am I missing? is there something in php.ini that needs to be changed?
Is there a particular reason why you need to have two separate entries for your hosts? Seeing as they both use the same Log Files, and DocumentRoot, could you not add blah.com to the list of ServerAlias' ?
So you would end up with the following configuration below:
<VirtualHost *:80>
ServerAdmin webmaster#blah.com
ServerName v1.blah.com
ServerAlias v1.blah.com blah.com
DocumentRoot /home/blah/v1.blah.com
<Directory /home/blah/v1.blah.com/>
AllowOverride All
Order allow,deny
allow from all
</Directory>
AccessFileName .htaccess
ErrorLog /home/blah/blah_logs/v1.blah.com.in-error_log
CustomLog /home/blah/blah_logs/v1.blah.com.in-access_log common
</VirtualHost>

Resources