I am trying to call shell script from 000-default.conf inside /etc/httpd/sites-available.
The 000-default.conf file content is :
<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/html/
CustomLog "| /bin/sh /scratch/user/test.sh" custom_user_tracking
</VirtualHost>
My /etc/httpd/conf/httpd.conf has below content added:
ServerName localhost
LogFormat "%t [%h] [%m] [%U] [%B] [%b] [%D] [%q] [%s] [%{Referer}i] [%{User-Agent}i]" custom_user_tracking
IncludeOptional sites-enabled/*.conf
IncludeOptional sites-available/*.conf
I have kept a dummy html file inside /var/www/html/ Content of index.html:
<!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
When ever I hit http://localhost:80 the shell script is called. The shell script is executable and it just prints "Hello World".
But the script is auto called in 5s interval though the url http://localhost:80 is not triggered from the browser.
Can any one help here how can I disable auto refresh in apache httpd?
Related
I am trying to call shell script from 000-default.conf inside /etc/httpd/sites-available.
The 000-default.conf file content is :
<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/html/
CustomLog "| sh /scratch/user/test.sh" custom_user_tracking
</VirtualHost>
My /etc/httpd/conf/httpd.conf has below content added:
ServerName localhost
LogFormat "%t [%h] [%m] [%U] [%B] [%b] [%D] [%q] [%s] [%{Referer}i] [%{User-Agent}i]" custom_user_tracking
IncludeOptional sites-enabled/*.conf
IncludeOptional sites-available/*.conf
I have kept a dummy html file inside /var/www/html/
Content of index.html:
<!DOCTYPE html>
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
When ever I hit http://localhost:80 the shell script is not called at all. The shell script is executable and it just prints "Hello World".
But when I call apache kafka binary from 000-default.conf file then it works properly.
Modified 000-default.conf file:
<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/html/
CustomLog "| [HOME_DIR_KAFKA]/bin/kafka-console-producer.sh --topic access-log --broker- list <Remote_host_ip>:9092" custom_user_tracking
</VirtualHost>
Now when I click on http://localhost:80 then message is send in remote kafka server.
Can any one help here how can I call shell script from apache httpd?
Consider making the test.sh script executable using "shebang" ($!), instead of using sh.
000-default.conf :
CustomLog "| /scratch/user/test.sh" custom_user_tracking
/scratch/user/test.sh
#! /bin/sh
...
Alternatively run 'sh' using full path
CustomLog "| /bin/sh /scratch/user/test.sh" custom_user_tracking
Also, double check the execute permission on /scratch/user/test.sh, and the permission on the folder. https is usually running as non-privileged account.
Here is the content of send_message.sh:
#! /bin/sh
PRIVATE_KEY=$1
HOST=$2
TOPIC_NAME=$3
MESSAGE=$4
echo "OCI VM IP : $HOST"
echo "PRIVATE KEY : $PRIVATE_KEY"
echo "TOPIC-NAME: $TOPIC_NAME"
echo "MESSAGE: $MESSAGE"
## Run Cmd in vm
ssh -o "StrictHostKeyChecking no" -i $PRIVATE_KEY opc#$HOST /bin/bash << EOF
sudo su
echo $MESSAGE | [HOME_DIR]/bin/kafka-console-producer.sh --broker-list [VM_IP]:9092 --topic $TOPIC_NAME
EOF
And content of /etc/httpd/sites-available/000-dafult.conf
<VirtualHost *:80>
ServerName localhost
DocumentRoot /var/www/html/
CustomLog "| /home/opc/send_message.sh /home/opc/dev2oci.key 100.111.69.61 access-log apache-httpd" custom_user_tracking
</VirtualHost>
The message is sent in topi access_log for every 5s interval. Though the http://localhost:80 is not triggered from browser.
If I replace the CustomLog with below entry:
CustomLog "| [HOME_DIR]/bin/kafka-console-producer.sh --topic access-log --broker-list [VM_IP]:9092" custom_user_tracking
Then message is sent on triggering http://localhost:80 from browser.
Can anyone let me know what I am missing here.
I'm trying just to allow access to a web server from the localhost only. This is a fresh CentOS 7 installation with apache 2.4.6.
I created a basic web:
[root#server2 ~]# cat /var/www/html/secret/index.html
my password
[root#server2 ~]#
Then, the virtualhost and directory as official documentation for apache 2.4+ (192.168.1.10 it's the server IP and I have 192.168.1.10 serverX.example.com in the /etc/hosts):
[root#server2 ~]# cat /etc/httpd/conf.d/varios.conf
<VirtualHost *:80>
DocumentRoot /var/www/html/secret
ServerName serverX.example.com
</VirtualHost>
<Directory "/var/www/html/secret">
AllowOverride None
Options None
Require ip 127.0.0.1 192.168.1.10
</Directory>
[root#server2 ~]#
Everything should work, but:
[root#server2 ~]# curl serverX.example.com/secret
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>403 Forbidden</title>
</head><body>
<h1>Forbidden</h1>
<p>You don't have permission to access /secret
on this server.</p>
</body></html>
[root#server2 ~]#
Any idea?
To make the virtual host accessible to localhost only, you can bind it to the ip address 127.0.0.1.
<VirtualHost 127.0.0.1:80>
DocumentRoot /var/www/html/secret
ServerName serverX.example.com
</VirtualHost>
In /etc/hosts you can use 127.0.0.1 serverX.example.com instead.
It was necesary to add one / at the end:
curl serverX.example.com/secret/
With firefox works fine, but with curl or elinks, no.
Best regards.
-- Original post ---
I'd like to setup a dev env to play with some apache features. I'm running httpd on fedora.
I added to hosts local redirects
# cat /etc/hosts
127.0.0.1 example1.com
127.0.0.1 example2.com
# cmkdir /var/www/example1; echo "Hello from /var/www/example1/index.html" > /var/www/example1/index.html
# cmkdir /var/www/example2; echo "Hello from /var/www/example2/index.html" > /var/www/example2/index.html
# cmkdir /var/www/example2/sub ; echo "Hello from /var/www/example2/sub/index.html" > /var/www/example2/sub/index.html
# cvi /etc/httpd/conf/httpd.conf
<VirtualHost _default_:80>
DocumentRoot "/var//www/html"
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example1"
ServerName example1.com
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example2"
ServerName example2.com
</VirtualHost>
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example2/sub"
ServerName sub.example2.com
ServerPath "/sub/"
RewriteEngine On
RewriteRule "^(/sub/.*)" "/var/www/example2$1"
</VirtualHost>
# capachectl -t ; apachectl restart
# curl localhost
Hello from /var/www/html/index.html
# curl example1.com
Hello from /var/www/example1/index.html
# curl example2.com
Hello from /var/www/example2/index.html
# curl sub.example2.com
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
... ( lots of stuff different from the one i echoed) ...
If I do the same thing in local firefox - localhost works as expected, example1.com works as expected, but sub.example2.com redirects me to example2.com.
Can you please help me to figure out how to configure a local sub-domain? What is missing? Based on https://httpd.apache.org/docs/2.4/vhosts/examples.html I believe what I did is correct.
-- Edit / Update ---
If I follow the advice below from Newbie and change only the rewrite rule, without making any other changes from the setup above:
<VirtualHost 127.0.0.1:80>
DocumentRoot "/var/www/example2/sub"
ServerName sub.example2.com
ServerPath "/sub/"
# RewriteEngine On
# RewriteRule "^(/sub/.*)" "/var/www/example2$1"
</VirtualHost>
I get :
# curl sub.example2.com
curl: (6) Could not resolve host: sub.example2.com
If I
# cat /etc/hosts | grep sub
127.0.0.1 example2.com sub.example2.com
It works as expected
#curl example2.com
Hello from /var/www/example2/index.html
# curl sub.example2.com
Hello from /var/www/example2/sub/index.html
Still this seems to be strange setup. I don't want to create /etc/hosts record for each sub-domain... Shouldn't it be possible to handle this situation only via the httpd VirtualHost setting, without changing the DNS settings locally or even worse - adding C records in the DNS of a domain(if not localhost)? Any hint what I'm doing wrong? How can I get sub.example1.com to work without modifying the dns settings?
Regards,
Pavel
I believe I found out the answer to my question ...
** How to run locally sub-domains?**
/etc/hosts does not support wild cards(*.example2.com) and one needs to setup a local dns proxy server like dnsmasq for example. Otherwise for dev purposes you have to list (and then maintain :/ ) the sub-domains one-by-one in the /etc/hosts for local dev purposes.
How to run the sub-domains via official DNS records for domain ?
Seems laziest approach is to setup DNS settings having:
example2.com A the-server-IP
example2.com MX 0 example2.com
*.example2.com CNAME example2.com
Looking forward to your comments, if there is smarter approach.
If you agree this is the way to go - please accept the answer, so other members know it is the way to go.
Regards, Pavel
Remove the rewrite rule, it is used for redirecting so you have a loop.
<VirtualHost 127.0.0.1:80> DocumentRoot "/var/www/example2/sub" ServerName sub.example2.com ServerPath "/sub/"
Let me know if you run into problems.This dns would be able to work with your regular browsing since for all names it can't resolve it would check with googles dns and save it into its file.
install bind
apt-get install bind9 -y
cd /etc/bind
vim named.conf.options
And uncomment forwarders and two rows bellow and instead of 0.0.0.0 enter google's dns IP (8.8.8.8).
service bind9 restart
vim named.conf.options
zone "YOURDOMAIN NAME" {
type master;
file "db.site.com";
notify yes;
};
cp db.local /var/cache/bind/db.site.com
cd /var/cache/bind/
vim db.site.com
$TTL 604800
# IN SOA admin. admin.itlink.edu. (
2 ; Serial
604800 ; Refresh
86400 ; Retry
2419200 ; Expire
604800 ) ; Negative Cache TTL
;
IN NS ns.YOURDOMAINNAMEHERE.
IN A 192.168.1.10 replace this with the IP of your PC that has apache installed
ns A 192.168.1.10 replace this with the IP of your PC that has apache installed
www A 192.168.1.10 replace this with the IP of your PC that has
service bind9 restart
I'm trying to run two different domains on one and the same Docker container and port.
The Docker container runs CentOS. docker-compose.yml looks like so:
web:
image: fab/centos
ports:
- "80:80"
volumes:
- ./src/httpd.conf:/etc/httpd/conf/httpd.conf
- ./src:/var/www/html
- ./src/hosts:/etc/hosts
environment:
- VIRTUAL_HOST=dummy.dev,tests.dev
I also declared both .dev domain names inside of /etc/hosts on the host computer (OS X.)
It's been a while since I configured virtual hosts. My understanding was that I just needed to declare them and that Apache would automatically serve the proper files depending on the HTTP HOST being requested.
This is what I have, added at the end of httpd.conf:
<VirtualHost *:80> # first host = default host
DocumentRoot /var/www/html/default
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/dummy
ServerName dummy.dev
ServerAdmin webmaster#dummy.dev
ErrorLog logs/dummy.dev-error_log
CustomLog logs/dummy.dev-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/tests
ServerName tests.dev
ServerAdmin webmaster#tests.dev
ErrorLog logs/tests.dev-error_log
CustomLog logs/tests.dev-access_log common
</VirtualHost>
However, in practice, visiting either dummy.dev or tests.dev actually serves /var/www/html/default. This is as if Apache didn't realize which host is being called (though a dump of $_SERVER in PHP does show the expected HTTP_HOST value, i.e.: either 127.0.0.1, dummy.dev or tests.dev depending on which URL I visit.)
What did I miss?
It's unclear to me whether this is an Apache issue or a Docker one.
(Please note this is a different question from how to host multiple apps on the same domain with different port. In my case, I do want the virtual hosts to be all inside/on the same app/port/container.)
Turns out this was an Apache configuration issue.
I needed to explicitly enable domain-named virtualhosts, like so:
NameVirtualHost *:80
This answer helped.
Docker had nothing to do with the matter.
The fab/centos does not exist in public docker hub, so not sure why you are experiencing the issue.
My recommendation would be to take a step back and try to make it work with a simple example.
docker search apache yields eboraas/apache as most starred image, so I'll use that one for the example.
In a test directory, use your sample:
File: httpd.conf
<VirtualHost *:80> # first host = default host
DocumentRoot /var/www/html/default
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/dummy
ServerName dummy.dev
ServerAdmin webmaster#dummy.dev
ErrorLog logs/dummy.dev-error_log
CustomLog logs/dummy.dev-access_log common
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /var/www/html/tests
ServerName tests.dev
ServerAdmin webmaster#tests.dev
ErrorLog logs/tests.dev-error_log
CustomLog logs/tests.dev-access_log common
</VirtualHost>
Then create the vhost websites & the logs directory.
mkdir -p logs; for i in default tests dummy; do mkdir -p $i; echo "hello $i" > $i/index.html; done
Finally, run docker.
docker run -it -v $(pwd):/var/www/html -v $(pwd)/httpd.conf:/etc/apache2/sites-available/000-default.conf -v $(pwd)/logs:/etc/apache2/logs -p 9090:80 --rm --name apache_c eboraas/apache
Note that I use basically the same volumes as you did in your docker-compose.yml, except that I use site-available instead of changing the httpd.conf.
To test, make sure you have tests.dev and dummy.dev in your /etc/hosts pointing at the right Docker IP and test:
$> curl dummy.dev:9090
hello dummy
$> curl tests.dev:9090
hello tests
From this point, build up on top of this by first trying the docker apache image that you are using, then try with your custom /etc/hosts file, then put it back in a docker-compose file
Turn on NameVirtualHost in your httpd config:
File : /etc/httpd/conf/httpd.conf
NameVirtualHost *:80
NameVirtualHost *:443
I am kind of stuck. I dont get my local machine ready for development because I fail to get my vHost config working. I just cant access my document root with a browser (connection failes. Can connect to server).
Details:
OS: OSX 10.9.2
Apache/2.2.26
The vHost config looks like this:
<VirtualHost *:80>
ServerAdmin me#something.com
ServerName something.loc
ServerAlias *.someting.loc
UseCanonicalName Off
VirtualDocumentRoot /path/to/vhosts/%2/%1/htdocs
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
<FilesMatch "\.js\.gzip$">
AddType "text/javascript" .js.gzip
</FilesMatch>
<FilesMatch "\.css\.gzip$">
AddType "text/css" .css.gzip
</FilesMatch>
AddEncoding x-gzip .gzip
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain text/html
</IfModule>
</virtualHost>
In /path/to/vhosts/test/dev/htdocs I have a simple index.html, that says Hello World!.
The Host that should match this folder is dev.test.something.loc, which I included in my /etc/hosts:
127.0.0.1 dev.test.something.loc
The mod_vhost_alias apache module ist loaded:
LoadModule vhost_alias_module libexec/apache2/mod_vhost_alias.so
So I cant access the index.html via browser, but I dont get anything in an apache error.log when trying. So lets try with wget:
$ wget dev.test.something.loc
--2014-04-05 10:12:40-- http://dev.test.something.loc/
Resolving dev.test.something.loc... 127.0.0.1
Connecting to dev.test.something.loc|127.0.0.1|:80... failed: Connection refused.
Ok, not working. Next approach:
wget --header="Host: dev.test.something.loc" -O - http://localhost
--2014-04-05 10:14:53-- http://localhost/
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 67 [text/html]
Saving to: 'STDOUT'
0% [ ] 0 --.-K/s <html>
<head>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
100%[============================>] 67 --.-K/s in 0s
2014-04-05 10:14:53 (5.32 MB/s) - written to stdout [67/67]
Look, thats my index.html! Lets try 127.0.0.1 instead of localhost:
wget --header="Host: dev.test.something.loc" -O - 127.0.0.1
--2014-04-05 10:17:24-- http://127.0.01/
Resolving 127.0.0.1... 127.0.0.1
Connecting to 127.0.0.1|127.0.0.1|:80... failed: Connection refused.
That is all I got so far. Something is obviously wrong. But I am not familiar enough with the whole apache config thing, that I can figgure ot by myself what it is I am missing. Maybe somebody can provide helpful suggestions?
Thanks in advance.
I still dont know what the problem was, but formating the SSD and starting with a fresh OS X from scratch did the job. The very same config worked like a charm. Closing the question.