Creating Virtual Hosts on ubuntu mint apache - 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>

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

Local site in apache just redirects to another localsite

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

Setup vhost ubuntu

I am trying to setup a vhost in ubuntu, my file is like
/var/www/vhost/domain1.com/app
/var/www/vhost/domain1.com/public
/var/www/vhost/domain2.com/app
/var/www/vhost/domain2.com/public
the vhost in /etc/apache2/site-avalable/domain1.com.conf is :
<virtualhost *:80>
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
ServerAdmin webmaster#domain1.com
ServerName domain1.com
ServerAlias *.domain1.com
DirectoryIndex index.php
DocumentRoot /var/www/vhosts/domain1.com/public
LogLevel warn
ErrorLog /var/www/vhosts/domain1.com/log/error.log
CustomLog /var/www/vhosts/domain1.com/log/access.log combined
</virtualhost>
problem
the vhost folder and all the files after that (the children) are accessible in the web browser. I know I have to put some sort of htaccess but i don't know how and where I have to put it.
If you use apache 2.4 and you develop on a local machine:
Create a new configuration file for your website:
$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/domain1.com.conf
Edit the file like so:
<VirtualHost *:80>
ServerName domain1.com
ServerAlias *.domain1.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/vhost/domain1.com/public
ErrorLog /var/www/vhosts/domain1.com/log/error.log
CustomLog /var/www/vhosts/domain1.com/log/access.log combined
</VirtualHost>
Enable the website
$ sudo a2ensite domain1.com
Restart apache
$ sudo service apache2 restart
Add the following line to your /etc/hosts file
127.0.1.1 domain1.com
Now it should work, go to http://domain1.com and check it out.

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: 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.