Forum Moderators: phranque

Message Too Old, No Replies

Apache/httpd on Home Computer

         

kenmorgan

5:38 pm on Mar 14, 2014 (gmt 0)

10+ Year Member



The OS on my home computers is CentOS.

I run Apache (httpd) on my home computer so that I can develop and debug my Web sites locally rather than on my VPS server. I've been doing it for years, and it's worked fine.

There is no public_html directory. Instead, the files that would normally go in public_html on a server are in this directory: /var/www/html/BibleWebSite. Also, the Web site is brought up in Firefox with this URL: localhost/BibleWebSite.

However, I recently downloaded code for a contact form that included six php files. The submit page does not display the captcha, which I believe means that the php code isn't executed at all. However, I've uploaded all the files, and the form works fine in the same Web site on the server. I just need help getting it to work locally.

My .htaccess file:


Options +Includes
Options +Indexes
AddType text/html .html
AddOutputFilter INCLUDES .html

lucy24

8:44 pm on Mar 14, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



AddOutputFilter INCLUDES .html

This jumped out at me because I was once bitten by the same thing. Unfortunately in real life, not on MAMP. Do you happen to use SSIs --as opposed to php includes-- on any pages with .php extension? (That's, of course, the extension of the physical file, independent of URL.) If so you'll need to add .php to the "Includes" list.

You should also be able to see what's going on simply by looking at the html source of the file in your browser. Not the original raw source of the physical files: the browser's "view source" option showing the html that it has actually received.

phranque

8:50 pm on Mar 14, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld, kenmorgan!


Is this the first PHP script you have tried on that server?

kenmorgan

9:08 pm on Mar 14, 2014 (gmt 0)

10+ Year Member



Thanks so much for the replies!

Lucy: Yes I do use SSIs on all the pages for the header and footer--and I noticed that it didn't work in the html portion of the contactform.php file. Since html SSIs are in comment brackets, they were just taken as a comments! I'll add ".php" to the INCLUDES list. :)

Phranque: Yes, this is the first time I've used any php files in any of my Web sites.

Any ideas why the php isn't executing though?

lucy24

10:02 pm on Mar 14, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Now, wait. Is the page getting built and going out to your browser? If so, the php is executing. Otherwise you'd just see the raw php source on your screen.

phranque

10:39 pm on Mar 14, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



if i were you i would start with a hello-world.php script and get that working first.
the apache configuration directives for enabling php are typically in a server config file rather than the .htaccess file.

kenmorgan

11:40 pm on Mar 14, 2014 (gmt 0)

10+ Year Member



The file called contactform.php has php code at the beginning followed by html code, starting with DOCTYPE. This html portion displays the input boxes for the form, plus the header, footer, and background to match the other pages in the Web site. No php code is displayed when I view contactform.php in Firefox.

However, I thought the missing captcha image implied that the php code was not executing because of this line in the html code:


<div><img alt="Captcha image" src="show-captcha.php?rand=1" style="margin-top: 20px; margin-bottom: 20px;" id="scaptcha_img" /></div>


In other words, it calls another php file to display the captcha.

But I just now thought of a test: I simply clicked the "Submit" button with all the fields blank. The error messages (such as "Please provide your email address") are generated by another php file--fgcontactform.php.

So you're right: the php code IS executing. This could mean that the problem lies within one or both of yet two more php files: captcha-creator.php and show-captcha.php. But since the very same set of files execute correctly in the Web site on my VPS, what could the problem be?

Ken

lucy24

1:54 am on Mar 15, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Could be anything. Are all the files in the identical location relative to each other? I did notice that your img src uses a relative link. Is there something version-specific in either of those extra php files? (If there is, get rid of it. You don't have to be that cutting-edge.)

If you're testing locally you can always throw in humble cheats like

echo "OK so far!";


and then you know that the code is at least executing up to that point. (Admittedly, php seems to prefer going into a coma and not executing at all.) You can also ask it to echo things like the value of some variable, so you know it's what it was supposed to be. If you expect it to be "26" and instead it's "nagvaalauqtara" or "" or the square root of 2, it's time to start investigating.

The first php I did was a mail/contact form that I cribbed from somewhere else. The second was when I converted my html footer to php so it wouldn't link to the page you're currently on. (Happens all over the place but it gives me the fantods.) Third was making a navigation footer for one specific set of pages so I could rearrange them without having to manually edit up to five html files each time.

If you're using one of the standard pseudo-servers like MAMP/WAMP, php is enabled by default. Anyway, the php is definitely there.

phranque

4:30 am on Mar 15, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



if the problem is simply the captcha image then you should be able to test by requesting that url directly:
http://www.example.com/show-captcha.php?rand=1

then you should check the server response and if you are getting an error code, check your server error log file for clues.

kenmorgan

4:54 pm on Mar 15, 2014 (gmt 0)

10+ Year Member



Thanks again for your continuing perseverance!

phranque: Since the problem occurs when I run the Web site locally, I don't know of any other way to refer to the file. (See paragraph 3 of my first post in this thread.) I don't think "http://www.example.com" applies. On my VPS server, all the same files are in the public_html directory, and all this code works fine.

Also, I'm not sure where the server error log file is locally. All the Web site files are in /var/www/html/BibleWebSite. There is a directory called "error" in /var/www. None of the files in that directory made any sense to me.

Lucy: I put in the debugging comments at the beginning of each of the six php files. When I brought up the contact page (contactform.php), the following list was produced:

"Enter contactform Enter fgcontactform Enter class phpmailer Enter captcha creator"

Notice conspicuous by its absence is show-captcha.php.

Also, I never heard of MAMP or WAMP. I have Apache installed on my home computer, and httpd is running.

If it would help to look at how it works (correctly) on my VPS server, here's the URL for the contact page:


http://example.com/contactform.php


These are the six php files that are used:

contactform.php
fgcontactform.php
show-captcha.php
captcha-creator.php
class.phpmailer.php
thank-you.php

[edited by: phranque at 8:12 am (utc) on Mar 18, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

g1smd

5:29 pm on Mar 15, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



On your real site the files are in the root as accessed by HTTP.
On your home server the files are in a folder as accesssed by HTTP.
Configure the home server to act the same as the real site.
You can add dummy hostnames to Apache and to the HOSTS file to mimic this.

kenmorgan

5:36 pm on Mar 15, 2014 (gmt 0)

10+ Year Member



g1smd: Thanks for the reply.

"You can add dummy hostnames to Apache and to the HOSTS file to mimic this."

I'm not sure how to do this. However, if this were the problem, how could the other four php files be found, as indicated by the output to the debugging comments?

kenmorgan

11:10 pm on Mar 16, 2014 (gmt 0)

10+ Year Member



I found another log here: /var/log/httpd/error_log

This might be the log we need. I found this error message:

[error] [client 192.168.1.254] Directory index forbidden by Options directive: /var/www/html/

I did some Bing searching, and someone said that an "Options +Indexes" line is needed. But I do have that line in my .htaccess file; see my first post in this thread.

What does this error mean, and how can I fix it?

Thanks.

Ken

kenmorgan

11:35 pm on Mar 16, 2014 (gmt 0)

10+ Year Member



I also tried entering "show-captcha.php?rand=1" directly in the Firefox URL line. A completely black page was displayed with this message:

"The image "http://localhost/BibleWebSite/show-captcha.php?rand=1" cannot be displayed because it contains errors."

This message was itself an image, not text. The show-captcha.php file is quite short. Here's the code:


<?PHP
require_once("fgcontactform.php");
require_once("captcha-creator.php");

session_start();

if(isset($_POST['fg_validate_captcha']))
{
$captchaobj = new FGCaptchaCreator('scaptcha');
header("pragma: no-cache");
header("cache-control: no-cache");

if(!$captchaobj->ValidateCaptcha($_POST['scaptcha']))
{
echo "The code does not match. Please try again!";
}
else
{
echo "success";
}
}
else
{
$captcha = new FGCaptchaCreator('scaptcha');
header("pragma: no-cache");
header("cache-control: no-cache");
$captcha->DisplayCaptcha();
}
?>


However, this should not really be a coding error because the code works on my VPS server.

lucy24

3:42 am on Mar 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I did some Bing searching, and someone said that an "Options +Indexes" line is needed.

No, not at all. +Indexes means to auto-generate an index if there isn't an explicitly named index file. You very rarely want to do this, and definitely not for the whole site.

What you may be missing is the appropriate DirectoryIndex directive. The default is, I think, only "index.html". So if your index files are called something else, like "index.htm" or "index.php", you have to say so. You can list more than one if your site uses more than one. List them in the order of frequency; the server will grab the first one that fits:

DirectoryIndex index.html index.php index.html

Ordinarily this is a relative link without leading slash.

This message was itself an image, not text.

Good, I think, because it means something is happening. Otherwise the browser wouldn't know that something in .php is supposed to be an image.
new FGCaptchaCreator('scaptcha')

What is this, and where does it live?

kenmorgan

2:22 pm on Mar 17, 2014 (gmt 0)

10+ Year Member



Thanks again for your continued help.

(1) The DirectoryIndex line in my httpd.conf file is this:

DirectoryIndex index.html index.html.var

(2) The function FGCaptchaCreator is defined in captcha-creator.php. This file begins this way:


<?PHP
class FGCaptchaCreator extends FG_CaptchaHandler
{
var $image;
var $width;
var $height;
var $margin_y;
var $charset;
var $nChars;
var $linecolor;
var $code;
var $show_captcha_var;
var $nlines;
var $enc_key;
var $captcha_var_name;

function FGCaptchaCreator($captcha_var_name)
{
$this->width = 150;
$this->height= 60;
$this->charset="2356789ABCDEFGHJKLMNPQRSTUVWXYZ";
$this->nChars=6;
$this->margin_y = 10;

$this->nlines = 2;
$this->font_file = 'SFOldRepublicSCBold.ttf';
$this->enc_key="GhsnR5^Hyhsfg";
$this->captcha_var_name = $captcha_var_name;
}
[/cpde]

The remainder of the file continues a long list of functions, all in this "class." The function is also called in the main file, contactform.php.

By the way, the .htaccess file for this Web site on my VPS is:

[code]
RewriteEngine on
AddHandler server-parsed .html
RewriteCond %{HTTP_HOST} ^example\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.org$
RewriteRule ^/?$ "http\:\/\/example\.com\/" [R=301,L]

[edited by: phranque at 8:18 am (utc) on Mar 18, 2014]
[edit reason] Please Use example.com [webmasterworld.com] [/edit]

g1smd

3:13 pm on Mar 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Replace the .htaccess code with this:

AddHandler server-parsed .html

RewriteEngine on

RewriteCond %{HTTP_HOST} example\.(com|org)
RewriteCond %{HTTP_HOST} !^(example\.com)?$
RewriteRule (.*) http://example.com/$1 [R=301,L]


replacing "example" with your site name.

kenmorgan

3:45 pm on Mar 17, 2014 (gmt 0)

10+ Year Member



Thanks, g1smd. But I really don't understand. Are you referring to the .htaccess file on my local Web site? If so, there is no .com or .org. This is what goes in the URL line of Firefox to run the Web site:

http://localhost/BibleWebSite

Also, the AddHandler line doesn't seem to work on the local .htaccess. I don't know why. What I need for the SSIs to work locally is the following two lines in the local .htaccess file:

Options +Includes
AddOutputFilter INCLUDES .html .php

On the other hand, if you mean the .htaccess file on the VPS, the contact page works there. Or are you simply recommending a better syntax to use?

[edited by: phranque at 8:18 am (utc) on Mar 18, 2014]
[edit reason] unlinked url [/edit]

kenmorgan

4:06 pm on Mar 17, 2014 (gmt 0)

10+ Year Member



For some reason, the forum software refuses to print the entire URL for the local Web site, which I wanted to do to show that there is no .com or .org in the URL. So I'll do it this way:

(1) It begins with http://

(2) Then it continues with localhost/BibleWebSite

(3) To display a specific page, add: /contactform.php

g1smd

10:35 pm on Mar 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The replacement htaccess code in my post is to replace the htaccess code you posted immediately before my post.

Additionally, as I said several posts back, one major difference in the setup of the two sites is the fact the real site uses root URLs, but the test site is accessed as a sub-folder. You should aim to access the test site as root URLs. I use dummy entries in the HOSTS file matching made up hostnames in the Apache config to achieve this.

lucy24

10:58 pm on Mar 17, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



the real site uses root URLs, but the test site is accessed as a sub-folder

Does WAMP-or-equivalent do this automatically? MAMP does. It means that I can test things locally even when they use root-absolute links. (The browser, or possibly my OS, can handle /directory/ links adequately, but draws the line at / root.)

kenmorgan

11:04 pm on Mar 17, 2014 (gmt 0)

10+ Year Member



I'd be glad to try it. Could you give me a few pointers? For example, is the Apache config file another name for httpd.conf? What would be the syntax of the lines I need to add? Similarly for the hosts file: where it is and the lines that need to be added. I'm afraid I'm not an Apache expert. :)

Back to the .htaccess file on the VPS. Again I'd be glad to do it. But in this case when the current file is working, I'm curious about the difference between the two versions.

Ken

kenmorgan

1:47 am on Mar 18, 2014 (gmt 0)

10+ Year Member



Lucy, I think you got your post in while I was writing my response to g1smd. But as you can tell from that response, I've had no experience with what you two are talking about.

However, I did all of a sudden think of a question regarding g1smd's suggestion. I currently have eight Web sites that I develop, run, and debug locally:

/var/www/html/BibleWebSite
/var/www/html/MessiahWebSite
etc.

The way I have Apache set up now handles them all. But if "public_html" is used, how can I implement more that one Web site?

On my VPS, of course, all the Web sites are completely separate accounts set up with WHM, have their own cpanel, etc. Each has its own public_html. For example,

/home/kmorgan/public_html
etc.

I don't have any idea how to set something like this up on my home computer. I don't have WHM on it.

lucy24

6:17 am on Mar 18, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



g1 may say differently, but I advise you to stick with htaccess even though you're in a position to use the config file itself. This is on the assumption that your "real" site is limited to htaccess. So no point in getting in the habit of something and then discovering you can't do it on the live site.

Besides, the main issue with htaccess is server load. Obviously this is not going to be a problem on your local machine.

g1smd

7:18 am on Mar 18, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The sugggestion is to define dummy hostnames example1.com, example2.com, etc in the Apache config with each pointing to the respective folder where the site lives.

In order for this configuration to work, the dummy domains also have to be added to the HOSTS file on your computer with each pointing to 127.0.0.1. You'll no longer request your test sites as localhost plus folder name, you'll use the dummy invented hostnames.

When you request example.com you'll be looking at the real site out there on the web.
When you request example1.com you'll be looking at your local copy, served as if it were a real site.

kenmorgan

11:40 am on Mar 18, 2014 (gmt 0)

10+ Year Member



Thank's, Lucy, your advice sounds good. Anyway, I couldn't implement g1smd's suggestions without answers to my questions in my post labeled "11:04 pm on Mar 17."

But I'm wondering if the issue between using my current setup running the sites on "localhost" versus setting up dummy .com names is relevant to the real problem at hand: the problem of why show-captcha.php does not execute correctly when the other php files do? In your post labeled "3:42 am on Mar 17," Lucy, you suggested that were making progress on this problem.

lucy24

6:24 pm on Mar 18, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is the show-captcha program not loading at all, or just not executing? And if it's not loading, is it because the server can't find it or because it contains an error?

If you're not sure, add a random line somewhere in the show-captcha file that says something like
echo "Hi there! I'm your captcha program!"


If this line doesn't show up, proceed to:
comment-out everything in show-captcha except that one line, and now see if it displays.

Is the whole thing inside <img> tags? If so, try moving the function reference outside of the <img> just to see if it behaves differently.

Oh, as long as we're here: add something like this to your local htaccess (never the live one!):

ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType text/html "access"

Change "1 month" to something shorter if you change your non-page files frequently. (Replace or rename doesn't matter: only if you change the content of existing files.) Add a line for "text/php" if appropriate-- but don't quote me on the wording. The object of these lines is to stop your browser from caching any page, ever, so you don't have to keep reloading or refreshing.

kenmorgan

1:12 am on Mar 19, 2014 (gmt 0)

10+ Year Member



Lucy, here goes:


(1) "Is the show-captcha program not loading at all, or just not executing? And if it's not loading, is it because the server can't find it or because it contains an error?"


See your post, "10:02 pm on Mar 14" and my response, "11:40 pm on Mar 14." Then note the error message on my post, "11:35 pm on Mar 16." (I sure wish this forum software numbered the posts! :) )


(2) "If you're not sure, add a random line somewhere in the show-captcha file that says something like
echo "Hi there! I'm your captcha program!"


At your suggestion in post "1:54 am on Mar 15," I put in the debugging comments, and the result is described in my post, "4:54 pm on Mar 15." So as per your statement in "10:02 pm on Mar 14," the show-capptcha.php is executing.


(3) "If this line doesn't show up, proceed to:
comment-out everything in show-captcha except that one line, and now see if it displays."


When I directly display show-captcha.php, as I said in (1), not even the echo debugging comment at the beginning of the code is displayed; rather an error message is. However, when I comment out everything following my echo, then the echo is displayed on a normal white background. BUT--even with everything following the echo commented out, the echo line, "Enter show-captcha," still is not displayed when I run contactform.php as described in my post, "4:54 pm on Mar 15."


(4) "Is the whole thing inside <img> tags? If so, try moving the function reference outside of the <img> just to see if it behaves differently."


I don't know. The show-captcha.php file is called by the src in an <img> statement in contactform.php, as I described in my "11:40 pm on Mar 14" post. I put the entire show-captcha.php file in my post "11:35 pm on Mar 16."

lucy24

5:59 am on Mar 19, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Jeepers, ken, that was three days ago. Nobody can remember that far back :-P

If it's inside img tags, then most routine debugging approaches won't work, because the browser is expecting to find an image. Did you at some point explain where the program lives, physically? The key question is whether it's in the same directory that houses your local server.

Say you've got your local site living in
/myHD/somedirectory/widgets/sitefiles/
If the captcha program is located anywhere within /sitefiles/, then you can load it up within your server by manually typing the rest of its pathname. (You can't open it up directly, the way you can with html pages. php requires a server*.)

I'm told that I can get my MAMP installation to support multiple sites concurrently if I fiddle with the vhosts file (formally "httpd-vhosts.conf" -- I just looked it up). On my system this lives in a directory called "extra" which is side by side with the "httpd.conf" file. (That's the one people mean when they talk about the config file.) Both live inside a directory called /apache/ which in turn lives side by side with eight different php directories (this strikes me as excessive) and one more called /SQLiteManager. Does any of this sound familiar?

Near the top of your config file is a line beginning "ServerRoot". For reasons I don't pretend to understand, the one on my system names a directory which is not the one containing the config file.

At this point I'd better stop looking, as I note that the current timestamp on the apache_error.log file is only fourteen minutes ago and I swear I didn't do anything! But if you're doing some parallel snooping, see if you can find the cluster of files with "blahblah_error.log" in their names.


* Technically there are alternatives, but it's not worth investigating.

g1smd

12:31 am on Mar 20, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I sure wish this forum software numbered the posts.

It does. See the 7-digit number after msg: over on the left.
The number survives when posts are moved around or other preceding posts are deleted.