Local site in apache just redirects to another localsite - apache

Using Ubuntu 18.04.01 and I set up multiple sites in apache so I can work on them locally. Here is my current set up:
/etc/hosts
127.0.0.1 localhost
127.0.1.1 site1.local
127.0.1.1 site2.local
/var/www
site1.com
site2.com
/etc/apache2/sites-available
000-default.conf
default-ssl.conf
site1.com.conf
site2.com.conf
site1.com.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/site1.com
ServerName site1.local
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
site2.com.conf looks the same as this but swap 'site1' with 'site2'
I also ran:
sudo a2ensite site1.com.conf
sudo a2ensite site2.com.conf
and the symlinks in in /sites-enabled are now there.
However the behavior I get is when I go to my browser and typle in http://site1.local it loads just fine. But when I enter in http://site2.local the browser just redirects back to site1.
Please advise.

Looks like it was just a caching issue with my browser

Related

Apache2 ServerName and ServerAlias not working

On a nearly fresh Ubuntu 20.04 LTS computer, I would like to set up a virtual host on my local machine. So I created a index.html under /var/www/test/ with the following content:
you have entered a test page
I have set up a test.conf file under /etc/apache2/sites-available/
with the following content:
<VirtualHost *:80>
ServerAdmin webmaster#zhihu.com
DocumentRoot /var/www/test/
ServerName zhihu.com
ServerAlias www.zhihu.com
<Directory /var/www/test/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
The localhost is working:
/etc/hosts config is probably working as indicated by ping
ping zhihu.com
PING zhihu.com (127.0.1.1) 56(84) bytes of data.
64 bytes from xxx (127.0.1.1): icmp_seq=1 ttl=64 time=0.045 ms
but browser cannot bring me to the domain which should now be hosted in /var/www/test/.
I have also a2ensite test.conf and a2dissite 000-default.conf and service apache2 reload
So I think the only possible place for error to occur is ServerName and ServerAlias. Why are they not working?
Could you please enable VirtualHost using a2ensite and access site in incognito mode.
Ensure that the ssl certificate for the site includes both example.com and www.example.com
and ServerAlias is set to www.example.com

Creating Virtual Hosts on ubuntu mint apache

I am working Linux Mint 17.3 and trying to create virtual hosts on Apache 2.4. I have followed the following procedure, but still can't browse the site:
Create two new virtual hosts
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/site1.com.conf
modify each host
sudo vim /etc/apache2/sites-available/site1.com.conf
ServerName site1.com
ServerAlias www.site1.com
ServerAdmin admin#site1.com
DocumentRoot /var/www/site1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
3.Enable the New Virtual Host Files
sudo a2ensite site1.com
// Disable original html host
sudo a2dissite 000-default.conf
sudo service apache2 reload
Add host info
sudo vim /etc/hosts
127.0.0.1:88 site1.com
127.0.0.1:89 site2.com
I have checked out everything online, that I could. Today is my second day. Thanks in advance for your help.
In your second step, you should declare that host as a VirtualHost in the conf file:
<VirtualHost *:80>
ServerName site1.com
ServerAlias www.site1.com
ServerAdmin admin#site1.com
DocumentRoot /var/www/site1.com/public_html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

What is wrong with this apache2 configuration?

I want to set up apache2 so that the wordpress website is served via port 80 and some other php website served via port 8080. This is on my local machine running Ubuntu 15.10.
The sites-available/000-default.conf contains:
Listen 80
Listen 8080
NameVirtualHost *:8080
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory /var/www/html/>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<VirtualHost *:8080>
DocumentRoot /var/www/php-website
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
When I enter localhost into the browser, I get the Wordpress website. When I enter localhost:8080, I get (redirected it seems to) localhost.
What could be wrong here? I though there might be something wrong with the second VirtualHost config so it's defaulting to the first, so I changed their order. Same thing happened however.
It could be a problem with wordpress configuration. I think wordpress uses wordpress adress (url) or site adress (url) to redirect if you are in another domain. So you should configure this adresses to localhost and localhost:8080.
It was a browser issue. Chrome and Firefox were automatically changing the URL to localhost. It works with curl and in Incognito/Private modes.

Document Root doesn't work with Virtual Host setting for Apache

I set VirtualHost in httpd.conf like this:
<VirtualHost xxx.255.118.79:80>
ServerAdmin hoge#hoge.foo.com
DocumentRoot /var/www/html
ServerName main.foo.com
ErrorLog logs/main.foo.com-error_log
TransferLog logs/main.foo.com-access_log
</VirtualHost>
<VirtualHost xxx.255.118.79:8080>
ServerAdmin hoge#hoge.foo.com
DocumentRoot /opt/another_www/
ServerName anotherhost.foo.com
ErrorLog logs/host.foo.com-error_log
TransferLog logs/host.foo.com-access_log
</VirtualHost>
And it looks like ok with httpd -S.
[iron#birdwatch html]$ sudo httpd -S
VirtualHost configuration:
xxx.255.118.79:80 main.foo.com (/etc/httpd/conf/httpd.conf:1012)
xxx.255.118.79:8080 anotherhost.foo.com (/etc/httpd/conf/httpd.conf:1020)
Syntax OK
But when I access to http://xxx.255.118.79:8080, it still access to /var/www/html.
Could you kindly tell me how I can make apache2 serve /opt/another_www for port 8080 ?
Thanks!
I realized that the domain name in has to be discoverable by the local machine that runs Apache.
It means it has to be local IP address or *.
<VirtualHost *:8080>
ServerAdmin hoge#hoge.foo.com
DocumentRoot /opt/another_www/
After I changed it to *, it started to serve documents under /opt/another/www.

Apache IP Virtual Hosts

Server has two IPs, fresh centos min install. Apache is working, both ips load Apache test page. both www.domain.com and domain.com resolve to second IP.
I'd like for the first IP (192.168.0.1) to load Apache test page, this is working fine
I want the second IP (192.168.0.2) to load a website in /home/site/www
Currently when we goto domain.com or www.domain.com or 2nd IP it loads apache test page instead of the site, here's our config. Also I have the IPs listed as 192 instead of the real ips. What am I missing? Why isn't 192.168.0.2 loading /home/site/www instead of the Apache test page?
ServerRoot "/etc/httpd"
Listen 80
ServerName 192.168.0.1:80
DocumentRoot "/var/www/html"
<Directory "/var/www/html">
NameVirtualHost 192.168.0.2:80
<VirtualHost 192.168.0.2:80>
DocumentRoot /home/site/www
ServerName mydomain.com
ServerAlias *.mydomain.com
ErrorLog logs/mydomain.com-error_log
CustomLog logs/mydomain.com-access_log common
</VirtualHost>
Update
The Fix
chcon -R --reference=/var/www /home/site/www
SELinux needed the correct permissions set on it, using the reference it copies the same permissions to my new folder
Try this:
ServerRoot "/etc/httpd"
Listen 80
ServerName 192.168.0.1:80
NameVirtualHost 192.168.0.1:80
NameVirtualHost 192.168.0.2:80
<VirtualHost 192.168.0.1:80>
DocumentRoot /var/www/html
ServerName mydomain.com #change accordingly
ServerAlias *.mydomain.com
ErrorLog logs/mydomain.com-error_log
CustomLog logs/mydomain.com-access_log common
</VirtualHost>
<VirtualHost 192.168.0.2:80>
DocumentRoot /home/site/www
ServerName mydomain2.com
ServerAlias *.mydomain2.com
ErrorLog logs/mydomain2.com-error_log
CustomLog logs/mydomain2.com-access_log common
</VirtualHost>
Don't forget to apply the changes on apache.
service httpd reload or similar command.
Also, make sure the directory /var/www/html has, at least, reading permissions for the apache user.
You are missing the NameVirtualHost directive.
NameVirtualHost 192.168.0.2:80
I would also highly suggest putting in Directory directives in as well.