Setting up LAMP on VPS, virtual host not working - apache

Trying to set up a sample site on a VPS with LAMP.
Accessing the site via IP address loads it fine, but I'd like to make it work with the URL "mysite.dev". However when I go to http://mysite.dev in Chrome, I get "This site can’t be reached. mysite.dev refused to connect."
I followed instructions from this article and the configtest is OK: https://linuxize.com/post/how-to-set-up-apache-virtual-hosts-on-ubuntu-20-04/
Port 80 is open.
I did notice when I enter http://mysite.dev in Chrome, it replaces it with https and I can't get that to turn off. Not sure if that's the problem.
Here is the config setup:
/etc/apache2/sites-available/mysite.dev.conf:
<VirtualHost *:80>
ServerName mysite.dev
ServerAlias www.mysite.dev
ServerAdmin xxxx#gmail.com
DocumentRoot /var/www/mysite.dev/public_html
<Directory /var/www/mysite.dev/public_html>
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/mysite.dev-error.log
CustomLog ${APACHE_LOG_DIR}/mysite.dev-access.log combined
</VirtualHost>
My local /etc/hosts file:
123.123.123.123 mysite.dev
123.123.123.123 www.mysite.dev
Any help is appreciated.

Figured it out. Chrome forces all .dev domains to use HTTPS, which I hadn't configured. Changed to use a different TLD and all is good.

Related

Issues with Apache and Virtual Hosts

I'm, trying to run both MediaWiki and Wordpress on an Apache server. I've got the MediaWiki site set up with a couple of sub-domain Virtual Hosts, so everything is fine there. This is installed at /var/www/html/mediawiki.
I'm trying to install Wordpress on it's own directory, /var/www/wordpress.
In my config file for Wordpress' virtual host, I've got:
<VirtualHost *:80>
ServerName domain.com
ServerAdmin admin#domain.com
DocumentRoot /var/www/wordpress
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
<Directory /var/www/wordpress/> AllowOverride All</Directory>
Obviously domain.com is not the domain.
Whenever I visit domain.com, however, it seems to be loading the MediaWiki install from /var/www/html/mediawiki, which is obviously not what I want.
Anyone got any ideas what I'm doing wrong here?
Run apachectl to see all the servers/vhosts running on your server both port 80 and 443
apachectl -S
Also I believe that:
<Directory /var/www/wordpress/> AllowOverride All</Directory>
should be inside of your virtual host block

Xampp unable to load multiple sites

I am working on a project using xampp server. I have more than one project. So for this I have set my httpd-vhosts like below
<VirtualHost *:8080>
ServerName khpos.com
ServerAlias www.khpos.com
DocumentRoot "D:/xampp/htdocs/pos"
<Directory "D:/xampp/htdocs/pos">
Require all granted
</Directory>
</VirtualHost>
#my second site
<VirtualHost *:8080>
ServerName demopos.com
ServerAlias www.demopos.com
DocumentRoot "D:/xampp/htdocs/demopos"
<Directory "D:/xampp/htdocs/demopos">
Require all granted
</Directory>
</VirtualHost>
Whenever I try to hit localhost:8080/demopos it's redirecting towards .../pos
hosts file
127.0.0.1:8080 khpos.com
127.0.0.1:8080 demopos.com
How to set it
Any help would be highly appreciated
You should change the portnumbers of the second etc. sites. Now all port addresses are on port 8080, if you change those to an other port number you should be fine.
<VirtualHost *:8080> to <VirtualHost *:{port-number}>
Where port-number is an other port-number
127.0.0.1:8080 khpos.com
127.0.0.1:{port-number} demopos.com
you have 2 virtual hosts defined and also the names in hosts file. Please first remove the port numbers in the hosts file:
ServerName khpos.com
ServerName demopos.com
127.0.0.1 khpos.com
127.0.0.1 demopos.com
if you call khpos.com:8080 or demopo.com:8080 it should reach your correct virtual hosts.
But: if you call localhost:8080 it will match none of your virtual hosts.
In that case Apache always enters the first virtual host matching the port that you have in your conf file ignoring its server name. That is a strange behavior of Apache - in my opinion a bug. It is exactly what you observe.
For that reason I always place a dummy virtual server in front of all others that can catch all non fitting requests with a dummy message in a simple html file.
Just a hint: you have defined
DocumentRoot "D:/xampp/htdocs/pos"
so there currently is no khpos.com:8080/pos because pos is part of the root unless you create another folder pos below

Virtual Host on CentOS5

I'm trying to add a new virtual host to a CentOS 5 box.
I've added a new section in virtualhost.conf matching existing entries:
<VirtualHost ip address:80>
ServerAdmin email#email.com
DocumentRoot /home/stuff/public_html
ServerName stuff.stuff.com
ErrorLog /var/log/httpd/stuff.stuff.com-error_log
CustomLog /var/log/httpd/stuff.stuff.com-access_log combined
<Directory /home/stuff/public_html>
Options All
AllowOverride All
</Directory>
<IfModule mpm_itk_module>
AssignUserId stuff stuff
</IfModule>
</VirtualHost>
I then do a reload using the command: /etc/init.d/httpd reload
I've put a placeholder index.php in the directory, and would expect to see an "it works" when visiting the domain, but instead Chrome shows "ERR_NAME_NOT_RESOLVED"
What steps am I missing here? (I'm used to working in a windows environment, this is my first real encounter with CentOS)
Resolved this as a DNS issue, resolved locally editing hosts to match the IP.

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.

Apache VirtualHost and localhost

I'm working with XAMPP on Mac OS X.
I'm trying to run a Symfony website properly for a client, and I really don't know Symfony (yet). I just want to install and launch it.
I've changed my /etc/hosts file this way:
127.0.0.1 www.mysite.local
And the httpd.conf file this way:
<VirtualHost *:80>
ServerName www.mysite.local
DocumentRoot /Applications/MAMP/htdocs/mysite/web
DirectoryIndex index.php
<Directory /Applications/MAMP/htdocs/mysite/web>
AllowOverride All
Allow from All
</Directory>
Alias /sf /Applications/MAMP/htdocs/mysite/lib/vendor/symfony/data/web/sf
<Directory "/Applications/MAMP/htdocs/mysite/lib/vendor/symfony/data/web/sf">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
Now, the site is working (yay!), but I can't access any more any of my other local sites because localhost is rendered as www.mysite.local.
Where am I wrong?
This worked for me!
To run projects like http://localhost/projectName:
<VirtualHost localhost:80>
ServerAdmin localhost
DocumentRoot path/to/htdocs/
ServerName localhost
</VirtualHost>
To run projects like http://somewebsite.com locally:
<VirtualHost somewebsite.com:80>
ServerAdmin webmaster#example.com
DocumentRoot /path/to/htdocs/somewebsiteFolder
ServerName www.somewebsite.com
ServerAlias somewebsite.com
</VirtualHost>
The same for other websites:
<VirtualHost anothersite.local:80>
ServerAdmin webmaster#example.com
DocumentRoot /path/to/htdocs/anotherSiteFolder
ServerName www.anothersite.local
ServerAlias anothersite.com
</VirtualHost>
localhost will always redirect to 127.0.0.1. You can trick this by naming your other VirtualHost to other local loop-back address, such as 127.0.0.2. Make sure you also change the corresponding hosts file to implement this.
For example, my httpd-vhosts.conf looks like this:
<VirtualHost 127.0.0.2:80>
DocumentRoot "D:/6. App Data/XAMPP Shared/htdocs/intranet"
ServerName intranet.dev
ServerAlias www.intranet.dev
ErrorLog "logs/intranet.dev-error.log"
CustomLog "logs/intranet.dec-access.log" combined
<Directory "D:/6. App Data/XAMPP Shared/htdocs/intranet">
Options Indexes FollowSymLinks ExecCGI Includes
Order allow,deny
Allow from all
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
(Notice that in <VirtualHost> section I typed 127.0.0.2:80. It means that this block of VirtualHost will only affects requests to IP address 127.0.0.2 port 80, which is the default port for HTTP.
To route the name intranet.dev properly, my hosts entry line is like this:
127.0.0.2 intranet.dev
This way, it will prevent you from creating another VirtualHost block for localhost, which is unnecessary.
This is normal if you see it. Since it is the first virtual host entry, it will show local host.
Let’s say for example you didn't want that page to show. All you want to show is the "Apache, it works" page, so you would make a vhost entry before mysite.local as local host and point it to the "it works" page.
But this is normal. I had this problem before, so don't worry!
You may want to use this:
<VirtualHost *:80>
DocumentRoot "somepath\Apache2.2\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
as your first virtual host (place it before other virtual hosts).
I had the same issue of accessing localhost while working with virtualHost. I resolved it by adding the name in the virtualHost listen code like below:
In my hosts file, I have added the below code (C:\Windows\System32\drivers\etc\hosts) -
127.0.0.1 main_live
And in my httpd.conf I have added the below code:
<VirtualHost main_live:80>
DocumentRoot H:/wamp/www/raj/main_live/
ServerName main_live
</VirtualHost>
That's it. It works, and I can use both localhost, phpmyadmin, as well as main_live (my virtual project) simultaneously.
Additional description for John Smith's answer from the official documentation. To understand why it is.
Main host goes away
If you are adding virtual hosts to an existing web server, you must
also create a block for the existing host. The
ServerName and DocumentRoot included in this virtual host should be
the same as the global ServerName and DocumentRoot. List this virtual
host first in the configuration file so that it will act as the
default host.
For example, to work properly with XAMPP, to prevent VirtualHost overriding the main host, add the follow lines into file httpd-vhosts.conf:
# Main host
<VirtualHost *:80>
ServerName localhost
DocumentRoot "/xampp/htdocs"
</VirtualHost>
# Additional host
<VirtualHost *:80>
# Over directives there
</VirtualHost>
For someone doing everything described here and still can't access:
XAMPP with Apache 2.4:
In file httpd-vhost.conf:
<VirtualHost *>
DocumentRoot "D:/xampp/htdocs/dir"
ServerName something.dev
<Directory "D:/xampp/htdocs/dir">
Require all granted #apache v 2.4.4 uses just this
</Directory>
</VirtualHost>
There isn't any need for a port, or an IP address here. Apache configures it on its own files. There isn't any need for NameVirtualHost *:80; it's deprecated. You can use it, but it doesn't make any difference.
Then to edit hosts, you must run Notepad as administrator (described below). If you were editing the file without doing this, you are editing a pseudo file, not the original (yes, it saves, etc., but it's not the real file)
In Windows:
Find the Notepad icon, right click, run as administrator, open file, go to C:/WINDOWS/system32/driver/etc/hosts, check "See all files", and open hosts.
If you were editing it before, probably you will see it's not the file you were previously editing when not running as administrator.
Then to check if Apache is reading your httpd-vhost.conf, go to folder xampFolder/apache/bin, Shift + right click, open a terminal command here, open XAMPP (as you usually do), start Apache, and then on the command line, type httpd -S. You will see a list of the virtual hosts. Just check if your something.dev is there.
According to this documentation: Name-based Virtual Host Support
You may be missing the following directive:
NameVirtualHost *:80
Just change <VirtualHost *:80> to <VirtualHost 127.0.0.1:80>.
Then the default DocumentRoot will serve for all domains or IP addresses that point to your server and specified VirtualHost will work.
It may be because your web folder (as mentioned "/Applications/MAMP/htdocs/mysite/web") is empty.
My suggestion is first to make your project and then work on making the virtual host.
I went with a similar situation. I was using an empty folder in the DocumentRoot in httpd-vhosts.confiz and I couldn't access my shahg101.com site.
I am running Ubuntu 16.04 (Xenial Xerus). This is what worked for me:
Open up a terminal and cd to /etc/apache2/sites-available. There
you will find a file called 000-default.conf.
Copy that file: cp 000-default.conf example.local.conf
Open that new file (I use Nano; use what you are comfortable with).
You will see a lot of commented lines, which you can delete.
Change <VirtualHost *:80> to <VirtualHost example.local:80>
Change the document root to reflect the location of your files.
Add the following line: ServerName example.local And if you need to, add this line: ServerAlias www.example.local
Save the file and restart Apache: service Apache2 restart
Open a browser and navigate to example.local. You should see your website.
For anyone using Windows and the Bitnami WAMP Stack Manager Tool this virtual host configuration should go into Bitnami\apache2\conf\bitnami\bitnami.conf
Note: Some settings in Directory section is not a must.
For example, my virtual host configuration for site.com would be as follows:
<VirtualHost site.com:80>
DocumentRoot "C:/Bitnami/apache2/htdocs/site/docroot"
<Directory "C:/Bitnami/apache2/htdocs/site/docroot">
Options Indexes FollowSymLinks
AllowOverride All
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3 >
Require all granted
</IfVersion>
</Directory>
</VirtualHost>
Remember that configuration for vhost as mentioned, by other friends, sagits's answer is needed.