The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   OS X Developer (http://hintsforums.macworld.com/forumdisplay.php?f=27)
-   -   how to make PHP be parsed correctly locally once I got Apache and PHP working right? (http://hintsforums.macworld.com/showthread.php?t=23954)

marcnyc 05-18-2004 07:17 PM

how to make PHP be parsed correctly locally once I got Apache and PHP working right?
 
Hello I have found this article (http://www.devarticles.com/c/a/Apach...P-on-Mac-OS-X/) on how to configure PHP and Apache on Mac OS X and I got everything working correctly (meaning that I saw the Apache index file and I saw the index file that is in the /Sites folder when I went to that folder in the internet browser).

Unfortunately, after moving all my sites into the ~myusername/Sites folder, opening the in the browser doesn't seem to give the effect I hoped for. PHP is not correctly parsed, loads of errors etc etc... all things that obviously don't happen when the same code is on a remote server (ie: it ain't my code).

frankko 05-18-2004 07:22 PM

Can you be more specific? What are some of the exact errors you are getting? What is some of the exact code you are trying to run? Is it just PHP that isn't working in your ~/Sites/ folder, or do regular HTML pages fail, to? Have you restarted Apache since making the changes to its conf files? Etc.

The more info you can provide the better.

marcnyc 05-18-2004 10:09 PM

I have just re-started Personal Web Sharing (even though I had already done that earlier) and tried again. Regular HTML pages work. Earlier today when I posted PHP code wasn't parsed and returned as broken code (parts of code, from here and there). Now that I just re-tried before posting instead PHP code is being shown exactly as it is in the file, just as if the browser was a text editor.

ElectusUnum 05-18-2004 10:50 PM

Sounds like PHP isn't even enabled in Apache.

You need to edit the httpd.conf. The site you mentioned tells you how to do it. Also, make sure you didn't include this line, the author was purely making an example:

AddType application/x-httpd-php .bozo

If its still no go, post your httpd.conf file and let us see.

btw, are running 10.2 or 10.3?

frisco 05-19-2004 12:33 AM

I installed the PHP binary from Marc Liyange :
http://www.entropy.ch/software/macosx/php/

Be sure that you have enabled Personal Web Sharing via System Preferences.

STEP 1: Modify /etc/httpd/httpd.conf via Terminal pico or vim...

sudo editorName /etc/httpd/httpd.conf
[enter password on prompt]

uncomment (remove # sign) on these two lines:
LoadModule php4_module
AddModule mod_php4.c

Locate and uncomment or add these lines:
AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps
DirectoryIndex index.html index.php

Save and exit file. Reboot to activate new apache httpd.conf

Step 2: Set up Safari
Double-clicking an html icon opens in Safari ok, but it is not being served. Instead, you must type the address via your Sites folder, which is...
http://127.0.0.1/~YOU/yourSiteFolder/yourPage.php

(localhost = http://127.0.0.1 on every mac)

In your Sites folder, you'll see a default index.html. Edit this index.html to point to your site folders/pages. I created a Bookmark Bar link to it. Or, if you remove the index.html, Safari will provide a complete index of pages. And, since the pages are being served by your apache, the php will work.

Hope this helps. Cheers!!

marcnyc 05-19-2004 02:02 AM

Sorry to disappoint you but I am not THAT stupid ;-)

Yes I did edit httpd.conf as that link explains and as Frisco explains and no, I did NOT enter that stupid bozo line (but that was a good one ;-)).

The only thing I did different from Frisco's description is:
add these lines:
AddType application/x-httpd-php .php3
AddType application/x-httpd-php .php4
and I did not reboot because that link I posted mentioned that it was sufficient to stop and restart Personal Web Sharing (which, YES, is enabled as we speak).
But later in the day I have installed a software which required rebooting so in the meantime the machine has been rebooted and the result didn't change.

I have also said before that both PHP and Apache work, and I said that because I have seen the Apache page when I type in localhost and I have seen the correctly parsed phpinfo.php page that I have created and that contains the line <? phpinfo(); ?>

So now that we are on the same page, does anybody else have any ideas?

marcnyc 05-19-2004 02:17 AM

Ok, with the idea to double check every single step I took, I just went back to those instructions I posted and when I typed sudo pico httpd.conf in the terminal window a blank page came up and said I was creating a new file... I don't understand... Whatever happened to the old httpd.conf? I know I didn't delete it (I am not even sure where it is) and I know I saved my changes (but even if I didn't the old version should still be there). Is there some place I can download the file and put it in the right place, or re-generated it?
By the way, do I have to edit with terminal or can I use BBedit?
PS Oh and by the way I am on 10.3

hayne 05-19-2004 05:34 AM

Quote:

Originally Posted by marcnyc
when I typed sudo pico httpd.conf in the terminal window a blank page came up and said I was creating a new file... I don't understand... Whatever happened to the old httpd.conf? I know I didn't delete it (I am not even sure where it is)

Not being sure where it is is what is causing the problem. When you type the command "sudo pico httpd.conf", that opens the file "httpd.conf" in the current directory. If you aren't in the directory "/etc/httpd/" (where the httpd.conf file lives), you will get the behaviour you described.

marcnyc 05-19-2004 10:28 AM

Ok disregard the last message of mine, that was a stupid error on my side (forgetting to switch directory)...

anyway... i found found the problem but don't know how to fix it...
basically in httpd.conf the additions I made are not kept even though I save the document...
in other words I added this line:
AddType application/x-httpd-php .php3
and I added index.php3 to the list of indexes and everytime I save and open the file again these additions are not there...

What is REALLY weird is that other changes I have made (such as uncommenting the lines LoadModule php4_module and AddModule mod_php4.c) have instead been kept... How can that be possible?

Any ideas?

Where is this httpd.conf file anyway? Why can't I find it in the finder and open it in BBedit?

gdsgds 05-19-2004 11:42 AM

To open it with BBedit, launck BBEdit, go to "File>Open Hidden" and navigate through "/etc/httpd" and you're done.

schwartze 05-19-2004 11:57 AM

ooops, my mistake - I was looking at modules, not types. Nothing to see here.

marcnyc 05-19-2004 12:15 PM

Once I locate the file in BBedit it is grayed out and I can't open it. Why?

frankko 05-19-2004 12:18 PM

By default, BBEdit only opens what it thinks are "readable" files. In the open dialog box, at the top, is a select menu called "Enable". Change it from "All Readable Files" to "All Files". That'll let you open the httpd.conf.

hayne 05-19-2004 12:46 PM

You need 'root' privileges to edit The file /etc/httpd/httpd.conf
The usual way to do this is to use 'sudo' in front of the editing command in Terminal. I think you must have used this technique earlier.

If you use BBEdit, I think there is a way to turn on the needed 'root' privileges via a menu item.

marcnyc 05-19-2004 12:47 PM

Ok that worked. So now all the necessary additions and modifications have been made in httpd.conf and I have stopped and re-started Personal Web Sharing and yet I still see the code being displayed as code, not being parsed at all... What am I doing wrong?

marcnyc 05-19-2004 01:05 PM

Ok, I restarted the machine and now it works, so I guess one must re-boot and not just re-start the web sharing... I still get errors but now they look like errors due to the fact that code won't work locally, so I guess it's out of your jurisdiction and I have to figure it out myself ;-) Thanks for all your help and patience. I really appreciate.

marcnyc 05-21-2004 01:16 AM

Ok, I made a test:

I created a file called index.php that contains this line <? include("test.inc"); ?>.
Then I created a file called test.inc that contains the word "test".
I put both files in the /Sites/ folder and accessed the folder by typing http://localhost/~myusername in the browser
Everything works nice. I see the word test.

Then I opened the file index.php and modified the line into: <? include($DOCUMENT_ROOT."test.inc"); ?>. I opened the browser and pointed it to http://localhost/~myusername/
Everything works nice. I see the word test again.

So, then I created a folder called test and inside that folder I put the modified file called index.php and left the file test.inc where it is, so now I have this directory structure:

/Sites/test.inc
/Sites/test/index.php

Now if I point the browser to http://localhost/~myusername/ I get these errors:
Warning: main(test.inc): failed to open stream: No such file or directory in /Users/memoryman/Sites/test/index.php on line 1
Warning: main(): Failed opening 'test.inc' for inclusion (include_path='.:/usr/lib/php') in /Users/memoryman/Sites/test/index.php on line 1

So my conclusion is, somehow in Mac every folder is considered the root folder so $DOCUMENT_ROOT always refers to the folder where the file is launched from, which is not a normal behaviour, considering that for years on a server if the root was:
/
and a file was in:
/folder/
I could include a file inside the root using include($DOCUMENT_ROOT."filename.inc");

How can I address this issue so that I can work locally as if I was on my server remotely? so that all links and includes work and so that one folder is considered the root folder?

georgeocrawford 05-21-2004 06:12 AM

I'm afraid the server you were using for years had not corrected a large security flaw - namely the setting of the Register Globals option in the configuration.

All PHP distributions are now packaged with Register Globals OFF by default. This means that variables like
PHP Code:

$DOCUMENT_ROOT 

are no longer set by default before a script is run. You need to access them in a more secure way:
PHP Code:

$_SERVER['DOCUMENT_ROOT']) 

However, I just tried this, and my current configuration (which may or may not be the same as yours) takes the Document Root to mean /Library/WebServer/Documents. In your case, as you are serving from ~/Sites/, you may need to be a little more specific:
PHP Code:

include(dirname($_SERVER['SCRIPT_FILENAME'])."/test/test.inc"); 

Try using
PHP Code:

$_SERVER['DOCUMENT_ROOT']) 

... it may work with your setup.

georgeocrawford 05-21-2004 06:16 AM

The alternatives, if you really don't want to change your scripts at all, are to turn Register Globals on in your php.ini config file, and either serve your site from /Library/WebServer/Documents or change the server root to ~/Sites/ in your Apache config file.

I would recommend altering your scripts, however, to ensure portability in the future.

frisco 05-21-2004 12:21 PM

Just curious ... in index.php why not simply write

<?php include("../test.inc"); ?>

works for me.
cheers!

marcnyc 05-21-2004 12:26 PM

I tried with this line (as suggested):
<? include($_SERVER['DOCUMENT_ROOT']."test.inc"); ?>
but I still get these errors:
Warning: main(/Library/WebServer/Documentstest.inc): failed to open stream: No such file or directory in /Users/memoryman/Sites/test/index.php on line 1
Warning: main(): Failed opening '/Library/WebServer/Documentstest.inc' for inclusion (include_path='.:/usr/lib/php') in /Users/memoryman/Sites/test/index.php on line 1

I am aware of the fact that this $DOCUMENT_ROOT thing was changed some time ago but since everything seemed to be working on my site I never took the time to change everything... I will do that sometimes, even though it will take me a while with all the pages... I just went by the old rule "if it ain't broken, don't fix it" ;-)

The fact is right now I just would like to work locally on my mac on some files that DO work remotely. A month ago I had a PC and I had no problem working offline with Apache and PHP... Mac's are supposed to be better, why can't I make it work? I just want a folder to be considered the document root and then I'll throw everything in that folder and that should work, right?

marcnyc 05-21-2004 12:54 PM

Quote:

Originally Posted by frisco
Just curious ... in index.php why not simply write

<?php include("../test.inc"); ?>

works for me.
cheers!

See what I am trying to do is make the files that work remotely work locally so that I can work locally and upload when I am done, simply and effortlessly... If I have to change every single path of all the includes of all the files I am working it becomes a major undertaking...

All I want is work on my HD as if it was my remote server. There's got to be a way! I was doing it on my PC, you guys are not gonna tell me I bought a Mac after 10 years of PC and I can't do this, are you? ;-)

frisco 05-21-2004 01:22 PM

Two Items >>

If in your Sites directory you have a php page with the line:
echo $_SERVER['Document_ROOT'];

it will show you : /Library/WebServer/Documents

Your test.inc would have to be located there, not in your Sites directory.

Like you, I develop and test php on OS X because of its time-saving advantages. You don't have to change the path of includes - just use relative paths as you would for any other file. For testing, be sure that the path in the browser address bar begins with http://127.0.0.1/~you/ ... I have not always succeeded with http://localhost/

On a somewhat different but related topic, .inc files are subject to security risks, and it's better to use a .php extension. See the section "Include Files" here: http://www.developer.com/lang/article.php/922871

I hope you find this helpful.

hayne 05-21-2004 01:24 PM

Quote:

Originally Posted by marcnyc
I tried with this line (as suggested):
<? include($_SERVER['DOCUMENT_ROOT']."test.inc"); ?>
but I still get these errors:
Warning: main(/Library/WebServer/Documentstest.inc): failed to open stream: No such file or directory in /Users/memoryman/Sites/test/index.php on line 1
Warning: main(): Failed opening '/Library/WebServer/Documentstest.inc' for inclusion (include_path='.:/usr/lib/php') in /Users/memoryman/Sites/test/index.php on line 1

You need a slash (/) in your include line. "/test.inc" instead of "test.inc".
This is evident from the error message where it complains about not finding a file called "Documentstest.inc"

georgeocrawford 05-21-2004 04:16 PM

As I wrote:

Quote:

Originally Posted by georgeocrawford
PHP Code:

include(dirname($_SERVER['SCRIPT_FILENAME'])."/test/test.inc"); 



marcnyc 05-22-2004 01:10 PM

Quote:

Originally Posted by frisco
Two Items >>

If in your Sites directory you have a php page with the line:
echo $_SERVER['Document_ROOT'];

it will show you : /Library/WebServer/Documents

Your test.inc would have to be located there, not in your Sites directory.

Like you, I develop and test php on OS X because of its time-saving advantages. You don't have to change the path of includes - just use relative paths as you would for any other file. For testing, be sure that the path in the browser address bar begins with http://127.0.0.1/~you/ ... I have not always succeeded with http://localhost/

On a somewhat different but related topic, .inc files are subject to security risks, and it's better to use a .php extension. See the section "Include Files" here: http://www.developer.com/lang/article.php/922871

I hope you find this helpful.

Thank you for your very explanatory post.
I understand exactly what you are saying but using relative paths would still mean for me to change all my files (which have already been created years ago with absolute $DOCUMENT_ROOT paths) and which, as you understood, is exactly what I am trying to avoid. So I guess the next logical step would be to change the root in fttpd.conf to my user's site folder and then work from there, do you agree or are there any negative implications in that?
By the way the reason I did not use relative paths in the past is that in some instances I remember using include("../this.inc"); would not work, I don't remember when and why...

Also thanks for pointing out that there is a difference between http://localhost/~myusername and http://127.0.0.1/~myusername - I was totally unaware of that, and my browser actually often self-redirects itself to http://myusername.local (without the ~) which seems to be even more convenient but I guess is still not as good as the IP format, right?

Thanks for that other link too, I will check that out when I have some time... Anyway, so far I have used .inc only for text includes and .php for code includes.

hayne 05-22-2004 04:08 PM

Don't neglect the configuration settings you can specify in php.ini
And if you haven't already done so, stop everything and go and read all of the documentation on configuration and includes at www.php.net

marcnyc 05-22-2004 11:58 PM

Ok I am getting crazy over this, can't seem to figure it out...
I have changed the two instances of the document root settings in httpd.conf to Library/WebServer/Documents/Sites I have created a folder called Sites inside the Documents and a simple include now works, except my scripts still won't work... Now I get a call to undefined function error as soon as I start any script with any function (which I repeat are all easy functions that work perfectly when launched remotely on my server)... THERE HAS GOT TO BE A WAY TO MAKE THIS WORK EASILY!!!!!!!!

hayne 05-23-2004 03:17 AM

Ann undefined function error usually means that your includes are incorrect. Try adding in a statement to print out the path that is being included, and then go and look in that file to see if the troublesome function is in that file.

marcnyc 05-23-2004 11:23 AM

Quote:

Originally Posted by georgeocrawford
All PHP distributions are now packaged with Register Globals OFF by default. This means that variables like
PHP Code:

$DOCUMENT_ROOT 

are no longer set by default before a script is run. You need to access them in a more secure way:
PHP Code:

$_SERVER['DOCUMENT_ROOT'


Quote:

Originally Posted by georgeocrawford
The alternatives, if you really don't want to change your scripts at all, are to turn Register Globals on in your php.ini config file, and either serve your site from /Library/WebServer/Documents or change the server root to ~/Sites/ in your Apache config file.

I would recommend altering your scripts, however, to ensure portability in the future.

It seems like the only problem I had left is that Register Globals were OFF on my local PHP and ON on my remote installation, therefore I am going to follow georgeocrawford's suggestion and take the extra step and change all instances of
PHP Code:

$DOCUMENT_ROOT 

to
PHP Code:

$_SERVER['DOCUMENT_ROOT'

in order to ensure portability, which is always a must nowadays.

I want to thank you ALL for taking the time to helping me out on this issue. I do REALLY appreciate your help, time and effort. You are great people with a great heart.

Now that this is solved, I will have to change all these instances of that variable in houndred of files, which brings me to my next question about what GREP softwares are available for Mac OSX to batch this process effectively. In order to keep these forums nice and clean I posted this question in a more appropriate section of the forums and I will post the address here if any of you would be so kind and provide me with any hint you might have about this. Thank you very much once again. Here is the link:
http://forums.macosxhints.com/showth...596#post125596


All times are GMT -5. The time now is 05:46 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.