How to make a service on apache2 respond to request on multiple ports - bash

I have a website that I have hosted in /var/www/html. I was able to access it using my.ip.add.res. Now I want to be able to access it from multiple ports. I mean all three URLs, my.ip.add.res:80, my.ip.add.res:8000 and my.ip.add.res:8950, in the browser should lead to the same website.
What I tried:
Step 1: In /etc/apache2/ports.conf file, Listen 80 was already there. I added Listen 8000 and Listen 8950. The file looks like:
Listen 80
Listen 8000
Listen 8950
<IfModule ssl_module>
Listen 443
</IfModule>
<IfModule mod_gnutls.c>
Listen 443
</IfModule>
Step 2: In /etc/apache2/sites-available directory, there was a file 000-default.conf. I copied it to two files: myservice1.conf and myservice2.conf. I changed the first statement, VirtualHost *:80> to VirtualHost *:8000> and VirtualHost *:8950> in myservice1.conf and myservice2.conf files, respectively.
Step 3: I established symbolic links of these files to the corresponding files in /etc/apache2/sites-enabled. This is what I mean:
root#virtual-machine:/etc/apache2/sites-enabled# ls -l
total 0
lrwxrwxrwx 1 root root 35 Jul 7 09:18 000-default.conf -> ../sites-available/000-default.conf
lrwxrwxrwx 1 root root 46 Aug 22 11:45 loginservice.conf -> /etc/apache2/sites-available/loginservice.conf
lrwxrwxrwx 1 root root 44 Aug 22 11:44 scfservice.conf -> /etc/apache2/sites-available/scfservice.conf
Step 4: Then I restarted the Apache2 server
root#virtual-machine:/etc/apache2/sites-enabled# /etc/init.d/apache2 restart
[ ok ] Restarting apache2 (via systemctl): apache2.service.
The result is:
my.ip.add.res : leads to correct website
my.ip.add.res:80 : leads to correct website
my.ip.add.res:8000 : Unable to Connect
my.ip.add.res:8950 : No such resource (even if I stop the server ... surprising)
What is it that I am doing wrong or missing?
Edit1: As suggested by jedifans, I tried apachectl -S. below is the output.
root#virtual-machine:/etc/apache2/sites-available# apachectl -S
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
*:80 127.0.1.1 (/etc/apache2/sites-enabled/000-default.conf:1)
*:8950 127.0.1.1 (/etc/apache2/sites-enabled/loginservice.conf:1)
*:8000 127.0.1.1 (/etc/apache2/sites-enabled/scfservice.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex watchdog-callback: using_defaults
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex mpm-accept: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
Edit 2:
root#virtual-machine:/etc/apache2/sites-available# telnet 127.0.0.1 8000
Trying 127.0.0.1...
telnet: Unable to connect to remote host: Connection refused
root#virtual-machine:/etc/apache2/sites-available# telnet 127.0.0.1 8950
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
# gets stuck here. I have to ctrl + C to come out.

Listen 80
Listen 8000
Listen 8950
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot "/var/www/html"
</VirtualHost>
<VirtualHost *:8000>
ServerName www.example.com
DocumentRoot "/var/www/html"
</VirtualHost>
<VirtualHost *:8950>
ServerName www.example.org
DocumentRoot "/var/www/html"
</VirtualHost>
Make sure to use Document root to /var/www/html and also if you are adding this line at end of http.conf then their is no need to add in two separate files if you wish to keep in two separate files see to it that you have a
Include sites-available/*.conf
The above Include is true if you have ServerRoot as /etc/apache2/ inn your case
However a simpler way would be just add above configuration to end of http.conf file

The figured out what the problem was. All my steps were absolutely correct. However, there were already two services running on ports 8000 and 8950. I was able to get every port other than these two working.

Related

Unable to find a virtual host listening on port 80.... Please add a virtual host for port 80

My web server is set up like this:
AWS EC2 Linux AMI
Apache 2.4
PHP 7
MySQL
Certbot is giving me an error like this when I try to run it:
Unable to find a virtual host listening on port 80 which is currently needed for Certbot to prove to the CA that you control your domain. Please add a virtual host for port 80.
I've looked at other answers people posted on their blogs etc... but they were were not specifically for the EC2 Linux AMI or were made more complicated than they need be.
Most of them seem to have something to do with /sites-available or enabled... but the main .conf file already has a line in it that points to additional .conf files. No need to add a line there.
This all assumes that you have Apache 2.4 installed and are trying to install Certbot.
Make sure A record is set to your IP address in DNS.
cd /etc/httpd/conf.d
sudo nano yourDomainName.conf
Paste, edit, and save the following:
<VirtualHost *:80>
ServerName yourDomainName.com
DocumentRoot /var/www/html
ServerAlias www.yourDomainName.com
ErrorLog /var/www/error.log
CustomLog /var/www/requests.log combined
</VirtualHost>
.
sudo service httpd restart
And with this you should see the virtual host:
httpd -D DUMP_VHOSTS
To install certbot:
cd ~/downloads
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
sudo certbot-auto --apache --debug
Try option 1, "spin up a temporary web server"
I had running nginx on port 80, even setup xamp on port 80, curl on port 80 worked, but it could not find server at localhost
A probable issue with certbot trying to setup and authenticate host with existing servers.
Use of temporary webserver (option 1) worked, ensure no service is running on port 80
I have no httpd service, so I have know such directory and file.
But thanks for the advice before I found the file mydomain.conf in the directory
(in your case you should replace mydomain
/etc/apache2/sites-available and I modified it as it was advised above
sudo nano /etc/apache2/sites-available/mydomain.conf
<VirtualHost *:80> DocumentRoot /var/www/mydomain ServerName mydomain </VirtualHost>
then check for correct
sudo apache2ctl configtest
then restart apache
systemctl reload apache2
and then run certbot
certbot --apache -d mydomain

How to set up virtual hosts when changed the default port(80) to 8080?

I'm using MAMP on windows 10 and I couldn't start Apache server because of my default port(80) is used by System. So I changed the default port to 8080. But now I can't access my previously set up virtual host sites. Please help me
I already tried this :
1) MAMP\bin\apache\conf\httpd.conf
Listen 8080
2) MAMP\bin\apache\conf\extra\httpd-ssl.conf
<VirtualHost _default_:8080>
Listen 8080
3)hosts file
127.0.0.1:8080 mygym.test
4) MAMP\bin\apache\conf\extra\httpd-vhosts.conf
NameVirtualHost *:8080
<VirtualHost *:8080>
DocumentRoot "E:/MAMP/htdocs/mygym/system"
ServerName mygym.test
</VirtualHost>
now when I typed as mygym.test in the URL tab It comes a 404 error
No webpage was found for the web address: http://mygym.test/
You are doing it wrong.
3)hosts file
127.0.0.1:8080 mygym.test
The hosts file is for host name resolution only. You cannot put port numbers in there. the browser is what selects the port to choose.
Instead try 127.0.0.1 mygym.test in hosts file and try http://mygym.test:8080 in browser.

El Capitan upgrade and Apache VHost problems

So this caught me out. I've figured to diff between /etc/httpd.conf and /etc/httpd.conf.pre-update and correct default LoadModules and Includes which the upgrade applied, but I have a number of vhosts which are not playing nicely. All were fine prior to upgrade and are failing along the lines of:
http://localhost (DocumentRoot /www works fine)
http://test (DocumentRoot /www/test serves "It works!" from the standard DocumentRoot and nothing shows in /var/log/apache2/access_log)
http://test/index.php (works)
http://localhost/test (works)
httpd.conf extract
DocumentRoot "/www"
<Directory "/www">
Options FollowSymLinks Multiviews
MultiviewsMatch Any
AllowOverride All
Require all granted
</Directory>
<IfModule dir_module>
DirectoryIndex index.html
</IfModule>
httpd-vhosts.conf extract
<VirtualHost *:80>
DocumentRoot "/www/test"
ServerName test
</VirtualHost>
The ServerName is set in /etc/hosts and pings correctly, and it works when explicitly specifying the file so this looks good. Also tried with test.local (and test as ServerAlias) which made no difference.
DirectoryIndex has been set as "index.php index.html" which made no difference, and as http://localhost/test" correctly serves index.php, that looks as it should be.
And when http://test is accessed, it returns the standard "It works!" file from /Library/WebServer/Documents/index.html.en. This directory is only referenced from /etc/apache2/original/httpd.conf and /etc/apache2/extra/httpd-ssl.conf.
httpd -V shows correct conf is in use
Server version: Apache/2.4.16 (Unix)
Server built: Aug 22 2015 16:51:57
Server's Module Magic Number: 20120211:47
Server loaded: APR 1.4.8, APR-UTIL 1.5.2
Compiled using: APR 1.4.8, APR-UTIL 1.5.2
Architecture: 64-bit
Server MPM: prefork
threaded: no
forked: yes (variable process count)
Server compiled with....
-D APR_HAS_SENDFILE
-D APR_HAS_MMAP
-D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
-D APR_USE_FLOCK_SERIALIZE
-D APR_USE_PTHREAD_SERIALIZE
-D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
-D APR_HAS_OTHER_CHILD
-D AP_HAVE_RELIABLE_PIPED_LOGS
-D DYNAMIC_MODULE_LIMIT=256
-D HTTPD_ROOT="/usr"
-D SUEXEC_BIN="/usr/bin/suexec"
-D DEFAULT_PIDLOG="/private/var/run/httpd.pid"
-D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
-D DEFAULT_ERRORLOG="logs/error_log"
-D AP_TYPES_CONFIG_FILE="/private/etc/apache2/mime.types"
-D SERVER_CONFIG_FILE="/private/etc/apache2/httpd.conf"
httpd -S shows sites are loading correctly
VirtualHost configuration:
*:80 is a NameVirtualHost
default server localhost (/private/etc/apache2/extra/httpd-vhosts.conf:24)
port 80 namevhost localhost (/private/etc/apache2/extra/httpd-vhosts.conf:24)
port 80 namevhost phpmyadmin (/private/etc/apache2/extra/httpd-vhosts.conf:29)
port 80 namevhost site1.local (/private/etc/apache2/extra/httpd-vhosts.conf:34)
port 80 namevhost ideas.local (/private/etc/apache2/extra/httpd-vhosts.conf:39)
port 80 namevhost site2.local (/private/etc/apache2/extra/httpd-vhosts.conf:44)
port 80 namevhost site3.local (/private/etc/apache2/extra/httpd-vhosts.conf:49)
port 80 namevhost site4 (/private/etc/apache2/extra/httpd-vhosts.conf:54)
port 80 namevhost site5 (/private/etc/apache2/extra/httpd-vhosts.conf:59)
port 80 namevhost site6 (/private/etc/apache2/extra/httpd-vhosts.conf:64)
port 80 namevhost site7 (/private/etc/apache2/extra/httpd-vhosts.conf:69)
port 80 namevhost site8 (/private/etc/apache2/extra/httpd-vhosts.conf:74)
port 80 namevhost test (/private/etc/apache2/extra/httpd-vhosts.conf:79)
ServerRoot: "/usr"
Main DocumentRoot: "/www"
Main ErrorLog: "/private/var/log/apache2/error_log"
Mutex default: dir="/private/var/run/" mechanism=default
Mutex mpm-accept: using_defaults
Mutex proxy-balancer-shm: using_defaults
Mutex rewrite-map: using_defaults
Mutex proxy: using_defaults
PidFile: "/private/var/run/httpd.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="_www" id=70 not_used
Group: name="_www" id=70 not_used
The *.local sites load correctly, though their DocumentRoots are like:
/www/site1/public
I've been looking at it so long, I'm missing something really obvious aren't I. Put me out of my misery please.
Thanks.
So, end result appears to be that either I'm very, very stupid, or Safari's caching system is very, very stupid.
I'll get my coat...
(I remain shocked by how badly El Capitan has messed up my system - I clearly still have much to learn about OSX)

VirtualHosts does not work on Mac OS 10.7

I want to create VirtualHosts on Mac OS 10.7 and therefore I edited the /etc/apache2/httpd.conf. I uncommented the line "Include /private/etc/apache2/extra/httpd-vhosts.conf" to include the virtual hosts. In the file /private/etc/apache2/extra/httpd-vhosts.conf I wrote the following:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "/var/www"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/someFolder"
ServerName myApplication.dev
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/var/www/someOhterFolder"
ServerName myApplication2.dev
</VirtualHost>
There were two example virtual hosts before which I deleted. In my /etc/hosts file I added the following:
127.0.0.1 myApplication.dev
127.0.0.1 myApplication2.dev
I restarted my Apache and typed myApplication.dev and myApplication2.dev in the browser but I get an error "server not found" and it makes www.myApplication.dev in the browser (the same for myApplication2.dev).
Did I forget something to configure? I activated PHP in httpd.conf, mysql is installed also, but that has nothing to do with virtual hosts, I think.
Thanks for your help!
apachectl has an option -S to check vhost.conf file syntax. You can find these lines in vhosts.conf file.
> # You may use the command line option '-S' to verify your virtual host
> # configuration.
So, when you run
sh-3.2# apachectl -S
if you get Syntax OK result it means that there is no problem in your vhosts.conf file.
httpd: VirtualHost configuration:
wildcard NameVirtualHosts and _default_ servers:
*:80 is a NameVirtualHost
default server zz.xxxx.com (/private/etc/apache2/extra/httpd-vhosts.conf:27)
port 80 namevhost zz.xxxx.com (/private/etc/apache2/extra/httpd-vhosts.conf:27)
port 80 namevhost yy.xxxx.com (/private/etc/apache2/extra/httpd-vhosts.conf:35)
Syntax OK
If conf file has any problem it will tell you error line(s) like
sh-3.2# apachectl -S
Syntax error on line 33 of /private/etc/apache2/extra/httpd-vhosts.conf:
CustomLog takes two or three arguments, a file name, a custom log format string or format name, and an optional "env=" clause (see docs)
make sure that your vhosts.conf file has true configuration.
I had the exact same problem using OS X Lion. I fixed it by adding "::1 myhost.dev" to /etc/hosts:
127.0.0.1 myhost.dev
::1 myhost.dev
Incidentally, the ::1 also fixes a bug that makes page loading very slow on virtual hosts served from the Mac.
Are you using an HTTP proxy? If so, make an exception for myApplication.dev and myApplication2.dev.
What I meant was that the problem "server mot found" means that your browser cannot find the ip adresses of the hosts "myapplication.dev". This may be because you're using an http proxy, possibly one configured by your hosting company. In any case, you don't even reach the server, so you never get to try the virtual host configuration at all.
To just try the virtual host configuration, you can use telnet in a Terminal window and talk HTTP directly to the server, like this:
yourmacbox:~ yourname$ telnet 127.0.0.1 80
You should see the following text:
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Then you type
GET / HTTP/1.0
Host: myApplication.dev
Now, hopefully you should see some response from your web server. This shows that once you can connect to it, the virtual hosts things works.
I had the same problem, and noticed that the ServerRoot "/usr" was set as shown and incorrectly after the 10.7 upgrade. The httpd.conf file was still under /etc/apache2, but this setting in it was pointing to the wrong place. Once I had fixed that by changing to ServerRoot "/etc/apache2", all my previous virtual host configuration got picked up properly.
I also had to re-enable virtual hosts by uncommenting line 477 as mentioned here http://brettterpstra.com/fixing-virtual-hosts-and-web-sharing-in-mountain-lion/ That didn't quite kick in until I had fixed the path issue above.

Error: "VirtualHost *:80 -- mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results"

I'm running WAMP v2.0 on WindowsXP and I've got a bunch of virtual hosts setup in the http-vhosts.conf file.
This was working, but in the last week whenever I try & start WAMP I get this error in the event logs:
VirtualHost *:80 -- mixing * ports and
non-* ports with a NameVirtualHost
address is not supported, proceeding
with undefined results.
and the server won't start. I can't think of what's changed.
I've copied the conf file below.
NameVirtualHost *
<VirtualHost *:80>
ServerName dev.blog.slaven.net.au
ServerAlias dev.blog.slaven.net.au
ServerAdmin user#host.com
DocumentRoot "c:/Project Data/OtherProjects/slaven.net.au/blog/"
ErrorLog "logs/blog.slaven.localhost-error.log"
CustomLog "logs/blog.slaven.localhost-access.log" common
<Directory "c:/Project Data/OtherProjects/slaven.net.au/blog/">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
EDIT: I meant to add, if I change the NameVirtualHosts directive to specify a port, i.e
NameVirtualHost *:80
I get this error:
Only one usage of each socket address (protocol/network address/port) is normally permitted. : make_sock: could not bind to address 0.0.0.0:80
NameVirtualHost *:80
I get this error:
Only one usage of each socket address (protocol/network address/port) is normally >permitted. : make_sock: could not bind to address 0.0.0.0:80
I think this might be because you have somthing else listening to port 80. Do you have any other servers (or for example Skype) running?
(If it was Skype: untick "Tools > Options > Advanced > Connection > Use port 80 and 443 as alternatives for incoming connections")
Well, it seems the problem there is the way (and order) in which you assign the ports.
Basically, *:80 means "use port 80 for all hosts in this configuration". When you do this, Apache tries to bind that host to 0.0.0.0:80, which means that host will receive every single packet coming to the machine through port 80, regardless of what virtual host it was intended to go to. That's something you should use only once, and only if you have one host in that configuration.
Thus, if you have the same *:80 directive on two hosts in the configuration file, the server won't load because it will try to bind 0.0.0.0:80 twice, failing on the second try. (which explains the "Only one usage of each socket address (protocol/network address/port) is normally permitted. : make_sock: could not bind to address 0.0.0.0:80" message).

Resources