I am rapidly coming to the conclusion that this is not feasible, although for some reason, people - most likely who have not implemented the 'advice' they give, seem to think this is possible.
The scenario is quite straightforward. I am developing symfony websites on my local dev machine, running Ubuntu 10.0.4 LTS. I want to be able to run and test multiple sites locally.
Assuming I have the following sites:
site1.localhost
site2.localhost
site3.localhost
Following the documentation here, here and here (none of which work for me), I have done the following:
A. I modified my /etc/hosts file with the first entry to be:
127.0.0.1 site1.localhost site2.localhost hpdtp-ubuntu910
localhost php.localhost
B. I modified my /etc/apache2/ports.conf file (first lines) as follows:
NameVirtualHost localhost:80 Listen 80
C. I have created configuration sites for each of the websites (site1.localhost and site2.localhost). Each configuration is a separate file in /etc/apache2/sites-available
One such configuration file (for site1.localhost) in /etc/apache2/sites-available/site1 looks like this:
<VirtualHost localhost:80>
ServerName site1.localhost
DocumentRoot "/home/morpheous/work/webdev/frameworks/symfony/sites/site1/web"
DirectoryIndex index.php
<Directory "/home/morpheous/work/webdev/frameworks/symfony/sites/site1/web">
AllowOverride All
Allow from All
</Directory>
Alias /sf /lib/vendor/symfony/symfony-1.3.6/data/web/sf
<Directory "/lib/vendor/symfony/symfony-1.3.6/data/web/sf">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
D. I have disabled the default apache site by using (it kept showing up instead)
E. Since I can't enable all the sites I enable to work (like the documentation links above
purport), I have settled for enabling one site at a time, so that Apache dosen't get its confused as to which site to run. When I need to run another site, I disable the current one and enable the one I want to. This is (obviously?) far from ideal - but even this setup is not working - for the reasons listed below.
i). When I restart Apache, I get the following warning:
Reloading web server config apache2
[Sun Jul 18 10:32:23 2010] [warn]
NameVirtualHost localhost:80 has no
VirtualHosts
ii). when I navigate to http://site1.localhost
I get the following error message in FF:
Oops! This link appears to be broken
iii). My apache related errors are appearing in /var/log/apachche2/other_vosts.log
morpheous#hpdtp-ubuntu910:~$ tail /var/log/apache2/other_vhosts_access.log
site1.localhost:80 127.0.0.1 - - [18/Jul/2010:10:08:38 +0100] "GET / HTTP/1.1" 404 506 "-" "Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6 GTB7.1"
site1.localhost:80 127.0.0.1 - - [18/Jul/2010:10:09:30 +0100] "GET / HTTP/1.1" 404 506 "-" "Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6 GTB7.1"
site1.localhost:80 127.0.0.1 - - [18/Jul/2010:10:09:31 +0100] "GET /favicon.ico HTTP/1.1" 404 505 "-" "Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6 GTB7.1"
site1.localhost:80 127.0.0.1 - - [18/Jul/2010:10:09:36 +0100] "GET /favicon.ico HTTP/1.1" 404 505 "-" "Mozilla/5.0 (X11; U; Linux i686; en-GB; rv:1.9.2.6) Gecko/20100628 Ubuntu/10.04 (lucid) Firefox/3.6.6 GTB7.1"
My questions are:
Can Apache be setup to handle multiple virtual hosts on localhost? Afterall, all examples I have seen so far involve setting Apache with public facing ip addresses
If Apache can indeed handle multiple sites on localhost, which of the steps above am I doing incorrectly?. AFAICT, I have followed the documentation to the letter.
Yes, you can have several VirtualHosts, on your local machine -- It's exactly the same as when working on a remote server, except the IP address and domain-name are not the same.
What I generally do is :
Edit the hosts file
To add the new domain-names that I want served from my local computer.
For example, I would add :
127.0.0.1 tests
127.0.0.1 blog
And so on, with one line for each domain-name I want.
Add new VirtualHosts to Apache's configuration
Then, I add new VirtualHosts to Apache's configuration.
First site : tests
For example, for my tests domain-name, I would use :
<VirtualHost *:80>
ServerName tests
DocumentRoot /home/squale/developpement/tests
<Directory /home/squale/developpement/tests>
AllowOverride All
Options Indexes FollowSymLinks MultiViews
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
</VirtualHost>
A couple of things to note here :
The VirtualHost is on *:80
So, it'll listen to any address
The mapping to the domain-name is made by the ServerName directive
Of course, up to you to make sure that this VirtualHost is seen by Apache -- either by placing it's configuration in a loaded file (not recommended), or by :
Placing that configuration in a new file in /etc/apache2/sites-available/
Using a2ensite to enable the site
Which will create a symbolic-link in /etc/apache2/sites-enabled/, pointing to your new configuration file.
Second site : blog
And here the configuration I'd use for a second domain-name, that correspond to blog :
<VirtualHost *:80>
ServerName blog
DocumentRoot /home/squale/developpement/blog.pascal-martin.fr/www
<Directory /home/squale/developpement/blog.pascal-martin.fr/www>
AllowOverride All
Options Indexes FollowSymLinks MultiViews
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
</VirtualHost>
It's basically exactly the same thing ; only two differences :
The ServerName directive correspond to my second domain-name -- of course
And the DocumentRoot is not the same
Other files ?
I don't really change anything else to Apache's default configuration (I'm on Ubuntu, if that matters).
For example, I don't change anything in the ports.conf file : I still have the default NameVirtualHost and Listen directives :
NameVirtualHost *:80
Listen 80
Only important modifications I do are to enable some modules, like rewrite, expires, ... But that's not much related to your VirtualHosts problem ;-)
Results ?
If I open my browser and go to http://tests/, I get the things I would expect from my tests domain -- a list of directories and files, here.
And if I open my browser and go to http://blog/, I get the development instance of my blog -- well, and error page, saying that I forgot to set up the database ^^
Did you create a link from /etc/apache2/sites-available/site1 to /etc/apache2/sites-enabled/site1 ?
I'm using nginx instead of apache but ubuntu has standarized way of configuration. Usually sites-available includes all your configurations which are not included by apache. It only looks for configurations linked in sites-enabled directory.
I also don't think you need to enable one site a time. All should work. All have different domain names.
Related
I am a neophyte on Apache, but I see some similar questions to this list and hope you can help.
I just want to set up a basic local LAMP system under my Ubuntu 18.04 LTS (Asus VivoBook S15) to test out a website that I have to modify before playing with the version on my commercial ISP host.
The basic installation went fine (Apache/2.4.29, PHP 7.2) and both html and php could be processed from the var/www/html folder through the url localhost. I then did the following to set up public_html access:
i) add in /etc/apache2/apache2.conf the line:
ServerName localhost:80
ii) activate the the UserDir module (sudo a2enmod userdir) and edit the file /etc/apache2/mods-enabled/userdir.conf as follows :
<IfModule mod_userdir.c>
UserDir public_html
UserDir disabled root
<Directory /home/*/public_html>
# AllowOverride FileInfo AuthConfig Limit Indexes
AllowOverride All
# Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Options ExecCGI Indexes MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec
Require method GET POST OPTIONS
</Directory>
</IfModule>
iii) activate default virtual user directory (sudo a2ensite 000-default.conf) and edit the file /etc/apache2/sites-enabled/000-default.conf as follows:
# Global configuration
ServerName localhost
# without port according to recommendation found on the web
and
#ServerAdmin webmaster#localhost
ServerAdmin john#john-VivoBook
#DocumentRoot /var/www/html
DocumentRoot /home/john/public_html
iv) created the public_html directory under my home directory (/home/john) with ownership john:john and permissions 755 (also tried with 777), and added my index.html and index.php files
v) restarted Apache (sudo apache2ctl restart)
vi) configured my Firefox browser not to add www to urls.
When I try to access the url "localhost" I get a blank screen, whereas with "localhost/public_html", "localhost/public_html/index.html" or "localhost/public_html/index.php" I get the message:
Not Found
The requested URL was not found on this server.
Apache/2.4.29 (Ubuntu) Server at localhost Port 80
No errors in the log file /var/log/apache2/error.log
The tail of the log file /var/log/apache2/access log says:
127.0.0.1 - - [05/Apr/2020:13:24:44 +0200] "GET /public_html HTTP/1.1" 404 488 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0"
127.0.0.1 - - [05/Apr/2020:13:26:41 +0200] "GET /public_html/index.html HTTP/1.1" 404 488 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0"
127.0.0.1 - - [05/Apr/2020:13:27:29 +0200] "GET /public_html/index.php HTTP/1.1" 404 488 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0"
The log file /var/log/apache2/other_vhosts_access.log does not exist.
What am I doing wrong?
Thanks and best regards, John
If you want to use 'userdir' feature, you should just install apache2 and enable 'userdir'. You don't have to change the DocumentRoot.
sudo apt install apache2
sudo a2enmod userdir
sudo systemctl restart apache2
Now, switch to the user (in your case, john)
mkdir /home/john/public_html
echo 'hello' >> index.html
And you can now access index.html using "http://localhost/~john/index.html"
If you change your DocumentRoot to /home/john/public_html, then you should access the html page through "http://localhost/index.html", because the directory(/home/john/public_html) is already the Root.
Update after your comment
If you want php work with apache2 user directory feature.
First, install php environment.
sudo apt install php7.3
sudo a2enmod php7.3
Second,
sudo vim /etc/apache2/mods-enabled/php7.3.conf
Comment below block to re-enable PHP in user directories.
#<IfModule mod_userdir.c>
# <Directory /home/*/public_html>
# php_admin_flag engine Off
# </Directory>
#</IfModule>
Third, restart apache2 service.
sudo systemctl restart apache2
You should see the page working perfect through http://localhost/~john/index.php
I am now not sure that PHP was partially OK under Opera, since my index.php code was very short and I may well have mistaken a listing for an output. At any rate I upgraded opera-stable to version 67.0.3575.137 (remember that I have Apache/2.4.29 running under Ubuntu 18.04 LTS and PHP version 7.2.24), and all php files including http://localhost then showed listings rather than executing.
[Sorry, I've struggled for a half hour trying without success to show "sharp" (comment) signs and initial triangular brackets in code, and so am abbreviating without some code.]
I then did the following two steps (both taken from Apache shows php code instead of executing):
edit /etc/apache2/mods-enabled/php7.2.conf to comment out five lines as per the instructions after the commented line "Running PHP scripts in user directories is disabled by default":
At this point http://localhost worked but direct calls to php files still yielded listings only.
edit /etc/apache2/mods-enabled/mime.conf and after the following two commented lines:
comment: AddType allows you to add to or override the MIME configuration
comment: file mime.types for specific file types.
add the following lines:
AddType application/x-httpd-php .php
AddHandler application/x-httpd-php .php
With these two changes, my local php-based website seems to be working fine with both Opera and with Firefox 75.0 64 bits (previously blank screens for http://localhost and for explicit calls to php files).
I'm trying to set up a virtual host for a local site - something I've done literally hundreds of times - but something's going wrong this time.
My files are in C:\wwwroot\TEMPLATESITE
I have the following in my hosts file (last in a list of similar hostnames that all work)
127.0.0.1 templatesite.localhost.com
I have the following VirtualHost set up in my httpd-vhosts.conf (again, alongside a handful of other, identically set up, working sites)
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.
DocumentRoot c:\wwwroot\TEMPLATESITE
ServerName templatesite.localhost.com
ErrorLog logs/templatesite.-error_log
CustomLog logs/templatesite.-access_log common
</VirtualHost>
(I've tried both with and without a trailing slash on the DocumentRoot)
Yet, no matter what I try, no matter how many times I restart Apache, or my machine, when I type in templatesite.localhost.com I end up at the root of c:\wwwroot which is my localhost root.
I'm guessing the problem is in my hosts file since the logs don't have any attempts to even access TEMPLATESITE so it seems like the hosts file is failing back to the generic localhost.
In that light, here's my entire hosts file:
127.0.0.1 lmm.localhost.com
127.0.0.1 lmm2.localhost.com
127.0.0.1 prairie.localhost.com
127.0.0.1 pinegrove.localhost.com
127.0.0.1 cas-pc.localhost.com
127.0.0.1 lksu.localhost.com
127.0.0.1 localhost
127.0.0.1 templatesite.localhost.com
Thoughts?
I can't seem to get the Apache module mod_dumpio to log anything.
I have tried the suggestions in this post
To be sure I have tried the following
Disabled all of my vhosts except the one for the site I am working on
Ran a2enmod dump_io, and restarted Apache, can see it in /etc/apache/mods-enabled/dump_io.load
Check that /etc/apache/apache.conf LogLevel is dumpio:trace7 too
I open the log tail -f n 20 /var/log/apache2/packager-access.log
Run the following command to send some post data --data "param1=value1¶m2=value2" http://packager.local/api/package/create
Can only see lines like 127.0.0.1 - - [12/Nov/2014:10:07:04 +0000] "POST /api/package/create HTTP/1.1" 500 294 "-" "curl/7.35.0"
The vhost conf is as below
<VirtualHost *:80>
DocumentRoot /var/www/packager/web
ServerName lms-packager.local
ErrorLog ${APACHE_LOG_DIR}/packager-error.log
CustomLog ${APACHE_LOG_DIR}/packager-access.log combined
DumpIOInput On
DumpIOOutput On
LogLevel dumpio:trace7
</VirtualHost>
Is there anything else that I could try
After reading this post, I tried to move the configuration to the server level.
As the post suggests the key point from the Apache manual is
server config This means that the directive may be used in the server
configuration files (e.g., httpd.conf), but not within any
or containers. It is not allowed in
.htaccess files at all.
The following is also important so only check your ErrorLog file (e.g. /var/log/apache2/error.log) rather than any vhost file which makes sense if this is a server config
to be logged (dumped) to the error.log file
I'm having issues loading VirtualHosts I've setup on my iMac, all that they appear to be returning are blank pages. I've tried multiple browsers, flushed my DNS cache, cleared browser cache etc but nothing appears to be fixing this.
Here is what my /etc/apache2/httpd-vhosts/conf file looks like:
NameVirtualHost *:80
<Directory "/Users/chris/Sites/test/">
Allow From All
AllowOverride All
</Directory>
<VirtualHost *:80>
ServerName "test.dev"
ServerAlias "test.dev.*.*.*.*.xip.io"
DocumentRoot "/Users/chris/Sites/test"
</VirtualHost>
Here is what my /etc/hosts file looks like:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost
# VHX START
127.0.0.1 test.dev
fe80::1%lo0 test.dev
# VHX STOP
In my /private/etc/apache2/httpd.conf file I've made sure to uncomment the following line:
Include /private/etc/apache2/extra/httpd-vhosts.conf
I'm using VirtualHostX to manage all of this but I've even resorted to deleting everything in these files and starting again following guides online – that didn't work either.
I've tried to tail the apache access log and whenever I try to access http://test.dev nothing shows up in that file or the apache error log – it's as though my machine has absolutely no idea that URL exists even though I believe everything is setup correctly.
Any ideas on how I can fix this?
Thanks,
Chris
This is what I have in my "etc/hosts" file:
127.0.0.1 localhost
127.0.1.1 gotqn-System-Product-Name
127.0.0.1 depot.yourhost.com
and this is what I have added in the apache2.conf file:
<VirtualHost *:80>
ServerName depot.yourhost.com
DocumentRoot "/home/gotqn/Aptana Projects/depot/public/"
<Directory "/home/gotqn/Aptana Projects/depot/public">
AllowOverride all
Options -MultiViews
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
When I type http://localhost/ my browser lists all my files in the "var\www" folder and when I type http://depot.yourhost.com/ it displays the following error:
We're sorry, but something went wrong.
I have checked the production.log file of my rails applications and it says:
Connecting to database specified by database.yml
Started GET "/" for 127.0.0.1 at 2013-01-13 20:32:41 +0200 Processing
by StoreController#index as HTML Completed 500 Internal Server Error
in 3ms
ActiveRecord::StatementInvalid (Could not find table 'carts'):
app/controllers/application_controller.rb:46:in rescue in
current_cart' app/controllers/application_controller.rb:44:in
current_cart' app/controllers/store_controller.rb:11:in `index'
I suppose that there is something wrong with my database configuration as the table exists but it is not found.
Anyway, my questions is how the hosts file knew that "depot.yourhost.com" means to open the rails applications and "localhost" do list my "var\www" folder - I have thought that RoR is using port 3000.
Why there is no conflict for port 80?
Rails applications run under port 3000 when running with the lightweight Webrick server. Since you are running Apache with mod_passenger, the application will be available at port 80 according to your Apache configuration.
The hosts file contains an entry 127.0.0.1 depot.yourhost.com which directs that domain name to your local adapter 127.0.0.1. The hosts file should always be checked before a nameserver needs to be consulted to retrieve a name's address.
Next, your Apache configuration includes a VirtualHost which is listening on all network adapters due to the * in
<VirtualHost *:80>
Your Apache configuration must be setup to use name-based virtual hosting, after which the ServerName variable is matched against the HTTP_HOST request header which was supplied by your request to depot.yourhost.com.
Therefore, you can have as many VirtualHosts as needed all using port 80 on the same IP address, and Apache will decide which to route the request to based on the HTTP_HOST header.
Finally, mod_passenger will detect if an application is a Ruby on Rails application by inspecting the directory contents around the DocumentRoot which you have specified in the VirtualHost configuration. If it finds the public directory as its DocumentRoot and a RoR application at a level higher, Passenger will attempt to start the Rails app.
Now, as to why you are getting a directory listing from localhost... Elsewhere in your Apache configuration you must have a VirtualHost which matches the ServerName localhost and sets its DocumentRoot to /vart/www.
<VirtualHost *:80>
ServerName localhost
# or...
ServerAlias localhost
</VirtualHost>
If you are seeing the contents of the directory listed out rather than a 403 Forbidden (absent an index file like index.html), it is because the Apache server config or VirtualHost config does not include the directive:
Options -Indexes
Add that in the global httpd.conf to prevent listing directories server-wide.