Cant connect to VirtualDocumentRoot on local machine with apache vHosts - apache

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.

Related

No public_html access

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

apache allowing only localhost by IP returns error 301

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.

httpd local virtualhost example

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

Apache/2.4.7 (Ubuntu) - mod_dumpio not logging post data

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&param2=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

Why is my PHP source code showing?

So, I added the following to my http.conf file:
Listen443
NameVirtualHost *:443
<VirtualHost *:443>
DocumentRoot /PATH_TO/www
ServerName www.domain.com
SSLEngine on
SSLCertificateFile /PATH_TO/domain.crt
SSLCertificateKeyFile /PATH_TO/domain.key
SSLCertificateChainFile /PATH_TO/intermediate.crt
</VirtualHost>
https://www.domain.com works now, but it displays the PHP source code? What could be the reason for this?
"Server configuration error" is the reason ;-)
It is choosing to serve PHP files as "plain content" instead of handing them to a PHP engine for processing. This doesn't have anything to do with SSL (it will just as happily send the "plain content" over a secure connection).
From PHP: Installation:
AddModule mod_php.c
LoadModule php_module modules/mod_php.so
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php
And from PHP pages won't load:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
See a pattern? :-) In any case, read the fine installation instructions for the particular platform/server.
Happy coding.
It could also be that your php.ini configuration is set to not parse code between short php tags.
In centos this can happen because of php-module missing, to fix that issue we have to install
sudo yum install php-mysql php-devel php-gd php-pecl-memcache
php-pspell php-snmp php-xmlrpc php-xml
Just type LOCALHOST in your browser. then it will open the http://localhost/htdocs/ location. then navigate to your php file. It will open accordingly.