# [solved] Problems getting PHP5 to work with Apache 2

## Hannonball

I followed Gentoo Wiki HOWTO PHP5 with Apache2:

emerged apache2, and then emerged dev-lang/php with "apache2" use flag (and some random use flags like mysql)

My /etc/apache2/httpd.conf looks like follows:

```

LoadModule dir_module modules/mod_dir.so

LoadModule php5_module modules/libphp5.so

ServerRoot "/usr/lib/apache2"

ServerName localhost

ServerName *My dynamic IP here*

User apache

Group apache

Listen 80

DocumentRoot "/var/www/html"

DirectoryIndex index.php index.html index.shtml

```

and in /etc/conf.d/apache2 I've set APACHE2_OPTS="-D DEFAULT_VHOST -D PHP5"

I had index.php in /var/www/html/ and it didn't work.

Then I moved index.php in /var/www/localhost/htdocs and set DocumentRoot accordingly and it didn't work.

I've not forgotten '/etc/init.d/apache2 restart' between changes.

Thanks for your time.

EDIT: Sorry, I forgot to tell the actual problem  :Smile:  Thank you RayVad for pointing it out.

The problem is, that I see PHP in plain text when I visit my homepage (http://myip/index.php).

Instead of seeing 'hello' I see <?php echo "Hello"; ?>Last edited by Hannonball on Mon Nov 20, 2006 10:46 pm; edited 3 times in total

----------

## RayVad

I have upgraded my system from Apache 2, php4 and mysql4 to Apache2, php5 and mysql5 and run into a problem.

What kind of problem do you have? Can't you open te pages on an internet browser?

My issue: IE and Mozilla both try to download the page immediatly instead of showing the page. Apache problem or Browser issue?

Can you post you Httpd.conf?

----------

## RayVad

Your problem and mine are definitly a config issue.

It seems that apache doesn't know how to handle PHP pages.

(think my problem is of the same kind)

1) Check your httpd.conf again.

2) re-emerge PHP5 (i'm upgrading apache to the latest version at the moment)

3) do we need other php modules, which aren't installed yet?

In php4 i use to have dev-lang/mod_php, dev-php/php etc. but i don't see them for php5

If I find out something here i will share it with you here soon.

Did you btw check this treath: https://forums.gentoo.org/viewtopic-t-484317-start-0-postdays-0-postorder-asc-highlight-php5+apache+config.html

Got my problem solved bij this treath:

https://forums.gentoo.org/viewtopic-t-464840-highlight-php+displayed.html

My problem seemed to be this: 

 *Quote:*   

> Now I copied back my old httpd.conf, which has several virtual servers listed in it. The system instantly brain-farted and stopped playing php. Doing a diff on the old and new httpd.conf files, I found these lines were removed from the new version:
> 
> Code:
> 
>  LoadModule php4_module        modules/libphp4.so
> ...

 

Did you upgrade forom PHP4 to PHP5? Or did an update at all?

----------

## Hannonball

No I didn't upgrade. Never before had installed php or apache.

I tried removing those  'LoadModule php5_module modules/libphp5.so' and DirectoryIndex lines also, but nothing changed.

My httpd.conf now looks

```

LoadModule dir_module modules/mod_dir.so

ServerRoot "/usr/lib/apache2"

ServerName localhost

ServerName *my dynamic ip*

User apache

Group apache

Listen 80

DocumentRoot "/var/www/html"

```

----------

## RayVad

I think your problem is this line:

```

AddType application/x-httpd-php .php

```

Here is my Httpd.conf. (I deleted evetything that was marked with # so it was smaller to post here)

```

ServerRoot "/usr/lib/apache2"

<IfModule !perchild.c>

 

</IfModule>

PidFile "/var/run/apache2.pid"

Timeout 300

KeepAlive On

MaxKeepAliveRequests 100

KeepAliveTimeout 15

<IfModule prefork.c>

    StartServers         5

    MinSpareServers      5

    MaxSpareServers     10

    MaxClients         150

    MaxRequestsPerChild  0

</IfModule>

<IfModule worker.c>

    StartServers         2

    MaxClients         150

    MinSpareThreads     25

    MaxSpareThreads     75 

    ThreadsPerChild     25

    MaxRequestsPerChild  0

</IfModule>

<IfModule perchild.c>

    NumServers           5

    StartThreads         5

    MinSpareThreads      5

    MaxSpareThreads     10

    MaxThreadsPerChild  20

    MaxRequestsPerChild  0

</IfModule>

<IfModule peruser.c>

    ServerLimit          256

    MaxClients           256

    MinSpareProcessors     2

    MaxProcessors         10

    MaxRequestsPerChild 1000

    

    ExpireTimeout       1800

    

    Multiplexer nobody nobody

    

    Processor apache apache

    

</IfModule>

Listen 80

LoadModule access_module                 modules/mod_access.so

LoadModule auth_module                   modules/mod_auth.so

LoadModule auth_anon_module              modules/mod_auth_anon.so

LoadModule auth_dbm_module               modules/mod_auth_dbm.so

LoadModule charset_lite_module           modules/mod_charset_lite.so

LoadModule env_module                    modules/mod_env.so

LoadModule expires_module                modules/mod_expires.so

LoadModule headers_module                modules/mod_headers.so

LoadModule mime_module                   modules/mod_mime.so

LoadModule negotiation_module            modules/mod_negotiation.so

LoadModule setenvif_module               modules/mod_setenvif.so

LoadModule log_config_module             modules/mod_log_config.so

LoadModule logio_module                  modules/mod_logio.so

LoadModule cgi_module                    modules/mod_cgi.so

LoadModule cgid_module                   modules/mod_cgid.so

LoadModule suexec_module                 modules/mod_suexec.so

LoadModule alias_module                  modules/mod_alias.so

LoadModule rewrite_module                modules/mod_rewrite.so

<IfDefine USERDIR>

    LoadModule userdir_module            modules/mod_userdir.so

</IfDefine>

<IfDefine INFO>

    LoadModule info_module               modules/mod_info.so

    LoadModule status_module             modules/mod_status.so

</IfDefine>

LoadModule actions_module                modules/mod_actions.so

LoadModule autoindex_module              modules/mod_autoindex.so

LoadModule dir_module                    modules/mod_dir.so

LoadModule ext_filter_module             modules/mod_ext_filter.so

LoadModule deflate_module                modules/mod_deflate.so

LoadModule include_module                modules/mod_include.so

LoadModule php5_module          modules/libphp5.so

<IfDefine PROXY>

    LoadModule proxy_module                  modules/mod_proxy.so

    LoadModule proxy_connect_module          modules/mod_proxy_connect.so

    LoadModule proxy_ftp_module              modules/mod_proxy_ftp.so

    LoadModule proxy_http_module             modules/mod_proxy_http.so

</IfDefine>

Include /etc/apache2/modules.d/*.conf

User apache

Group apache

ServerAdmin root@localhost

Servername apache

UseCanonicalName Off

<Directory />

    Options FollowSymLinks

    AllowOverride None

</Directory>

<IfModule mod_userdir.c>

    UserDir public_html

    <Directory /home/*/public_html>

        AllowOverride FileInfo AuthConfig Limit Indexes

        Options MultiViews Indexes SymLinksIfOwnerMatch IncludesNoExec

        <Limit GET POST OPTIONS PROPFIND>

            Order allow,deny

            Allow from all

       </Limit>

       <LimitExcept GET POST OPTIONS PROPFIND>

            Order deny,allow

            Deny from all

       </LimitExcept>

    </Directory>

</IfModule>

AccessFileName .htaccess

<Files ~ "^\.ht">

    Order allow,deny

    Deny from all

</Files>

TypesConfig /etc/mime.types

DefaultType text/plain

<IfModule mod_mime_magic.c>

    MIMEMagicFile /etc/apache2/magic

</IfModule>

HostnameLookups Off

ErrorLog logs/error_log

LogLevel warn

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

LogFormat "%h %l %u %t \"%r\" %>s %b" common

LogFormat "%{Referer}i -> %U" referer

LogFormat "%{User-agent}i" agent

LogFormat "%v %h %l %u %t \"%r\" %>s %b %T" script

LogFormat "%v %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" VLOG=%{VLOG}e" vhost

CustomLog logs/access_log common

ServerTokens Prod

ServerSignature On

Alias /icons/ "/var/www/localhost/icons/"

<Directory "/var/www/localhost/icons/">

    Options Indexes MultiViews

    AllowOverride None

    Order allow,deny

    Allow from all

</Directory>

ScriptAlias /cgi-bin/ /var/www/localhost/cgi-bin/

<IfModule mod_cgid.c>

</IfModule>

<Directory "/var/www/localhost/cgi-bin/">

    AllowOverride None

    Options None

    Order allow,deny

    Allow from all

</Directory>

<IfModule mod_autoindex.c>

    IndexOptions FancyIndexing VersionSort

    AddIconByEncoding (CMP,/icons/compressed.gif) x-compress x-gzip

    AddIconByType (TXT,/icons/text.gif) text/*

    AddIconByType (IMG,/icons/image2.gif) image/*

    AddIconByType (SND,/icons/sound2.gif) audio/*

    AddIconByType (VID,/icons/movie.gif) video/*

    AddIcon /icons/binary.gif .bin .exe

    AddIcon /icons/binhex.gif .hqx

    AddIcon /icons/tar.gif .tar

    AddIcon /icons/world2.gif .wrl .wrl.gz .vrml .vrm .iv

    AddIcon /icons/compressed.gif .Z .z .tgz .gz .zip

    AddIcon /icons/a.gif .ps .ai .eps

    AddIcon /icons/layout.gif .html .shtml .htm .pdf

    AddIcon /icons/text.gif .txt

    AddIcon /icons/c.gif .c

    AddIcon /icons/p.gif .pl .py

    AddIcon /icons/f.gif .for

    AddIcon /icons/dvi.gif .dvi

    AddIcon /icons/uuencoded.gif .uu

    AddIcon /icons/script.gif .conf .sh .shar .csh .ksh .tcl

    AddIcon /icons/tex.gif .tex

    AddIcon /icons/bomb.gif core

    AddIcon /icons/back.gif ..

    AddIcon /icons/hand.right.gif README

    AddIcon /icons/folder.gif ^^DIRECTORY^^

    AddIcon /icons/blank.gif ^^BLANKICON^^

    DefaultIcon /icons/unknown.gif

    ReadmeName README.html

    HeaderName HEADER.html

    IndexIgnore .??* *~ *# HEADER* README* RCS CVS *,v *,t .svn

</IfModule>

AddLanguage ca .ca

AddLanguage cs .cz .cs

AddLanguage da .dk

AddLanguage de .de

AddLanguage el .el

AddLanguage en .en

AddLanguage eo .eo

AddLanguage es .es

AddLanguage et .et

AddLanguage fr .fr

AddLanguage he .he

AddLanguage hr .hr

AddLanguage it .it

AddLanguage ja .ja

AddLanguage ko .ko

AddLanguage ltz .ltz

AddLanguage nl .nl

AddLanguage nn .nn

AddLanguage no .no

AddLanguage pl .po

AddLanguage pt .pt

AddLanguage pt-BR .pt-br

AddLanguage ru .ru

AddLanguage sv .sv

AddLanguage zh-CN .zh-cn

AddLanguage zh-TW .zh-tw

LanguagePriority en ca cs da de el eo es et fr he hr it ja ko ltz nl nn no pl pt pt-BR ru sv zh-CN zh-TW

ForceLanguagePriority Prefer Fallback

AddDefaultCharset ISO-8859-1

AddCharset ISO-8859-1  .iso8859-1  .latin1

AddCharset ISO-8859-2  .iso8859-2  .latin2 .cen

AddCharset ISO-8859-3  .iso8859-3  .latin3

AddCharset ISO-8859-4  .iso8859-4  .latin4

AddCharset ISO-8859-5  .iso8859-5  .latin5 .cyr .iso-ru

AddCharset ISO-8859-6  .iso8859-6  .latin6 .arb

AddCharset ISO-8859-7  .iso8859-7  .latin7 .grk

AddCharset ISO-8859-8  .iso8859-8  .latin8 .heb

AddCharset ISO-8859-9  .iso8859-9  .latin9 .trk

AddCharset ISO-2022-JP .iso2022-jp .jis

AddCharset ISO-2022-KR .iso2022-kr .kis

AddCharset ISO-2022-CN .iso2022-cn .cis

AddCharset Big5        .Big5       .big5

AddCharset WINDOWS-1251 .cp-1251   .win-1251

AddCharset CP866       .cp866

AddCharset KOI8-r      .koi8-r .koi8-ru

AddCharset KOI8-ru     .koi8-uk .ua

AddCharset ISO-10646-UCS-2 .ucs2

AddCharset ISO-10646-UCS-4 .ucs4

AddCharset UTF-8       .utf8

AddCharset GB2312      .gb2312 .gb 

AddCharset utf-7       .utf7

AddCharset utf-8       .utf8

AddCharset big5        .big5 .b5

AddCharset EUC-TW      .euc-tw

AddCharset EUC-JP      .euc-jp

AddCharset EUC-KR      .euc-kr

AddCharset shift_jis   .sjis

AddType application/x-compress .Z

AddType application/x-gzip .gz .tgz

AddType application/x-httpd-php .php

AddType image/x-icon .ico

AddType text/html .cgi

AddHandler type-map var

AddHandler cgi-script .cgi .pl

BrowserMatch "Mozilla/2" nokeepalive

BrowserMatch "MSIE 4\.0b2;" nokeepalive downgrade-1.0 force-response-1.0

BrowserMatch "RealPlayer 4\.0" force-response-1.0

BrowserMatch "Java/1\.0" force-response-1.0

BrowserMatch "JDK/1\.0" force-response-1.0

BrowserMatch "Microsoft Data Access Internet Publishing Provider" redirect-carefully

BrowserMatch "^WebDrive" redirect-carefully

BrowserMatch "^WebDAVFS/1.[012]" redirect-carefully

BrowserMatch "^gnome-vfs" redirect-carefully

<IfDefine INFO>

    ExtendedStatus On

    <Location /server-status>

        SetHandler server-status

        Order deny,allow

        Deny from all

        Allow from localhost

    </Location>

</IfDefine>

<IfDefine INFO>

    <Location /server-info>

       SetHandler server-info

       Order deny,allow

       Deny from all

       Allow from localhost

    </Location>

</IfDefine>

Include /etc/apache2/vhosts.d/*.conf

```

----------

## Hannonball

I noticed that when I run apache2 command, it outputs 'Cannot load /usr/modules/mod_dir.so into server: /usr/modules/mod_dir.so: cannot open shared object file: No such file or directory'

mod_dir.so lies in /usr/lib/apache2/modules/. So it seems there are some incorrect (default?) paths [in configs?] or..? How could I change them? Or is my 'LoadModule dir_module/mod_dir.so' line incorrect?

Should I have 'LoadModule php5_module modules/libphp5so' line in my httpd.conf or not? 

I also tried to c&p contents of /etc/apache2/modules.d/70_mod_php5.conf in my httpd.conf, but I think it didn't help. And I think it's something I'm not even supposed to do.

There is 'AddDirectorIndex index.php index.phtml' in 70_mod_php5.conf but I still need to add '/index.php' in front of url to get page shown. 

Once again I repost my httpd.conf and 70_mod_php5.conf:

```

# httpd.conf 

LoadModule dir_module modules/mod_dir.so

ServerRoot "/usr/lib/apache2"

ServerName localhost

ServerName *my dynamic ip*

User apache

Group apache

Listen 80

DocumentRoot "/var/www/html"

```

```

# 70_mod_php5conf

<IfDefine PHP5>

        # Load the module first

        <IfModule !mod_php5.c>

                LoadModule php5_module    modules/libphp5.so

        </IfModule>

        # Set it to handle the files

        <IfModule mod_mime.c>

                AddType application/x-httpd-php .php

                AddType application/x-httpd-php .phtml

                AddType application/x-httpd-php .php3

                AddType application/x-httpd-php .php4

                AddType application/x-httpd-php .php5

                AddType application/x-httpd-php-source .phps

        </IfModule>

        AddDirectoryIndex index.php index.phtml

</IfDefine>

```

I hope my browser's cache (FF) does not play a role in my problems, but I'm pretty sure it does not. 

Sorry for wasting your storage resources with my posts.  :Sad: 

----------

## RayVad

The default Apache config should be okay by default. Only follow the wiki to make it work with PHP since there has to be some lines added (or removed if upgrading from php4 to php5)

Check also, before adding, if lines allready exist.

 *Quote:*   

> 
> 
> mod_dir.so lies in /usr/lib/apache2/modules/. So it seems there are some incorrect (default?) paths [in configs?] or..? How could I change them? Or is my 'LoadModule dir_module/mod_dir.so' line incorrect? 
> 
> 

 

the line should be correct, since mine is like:

```

LoadModule dir_module                    modules/mod_dir.so 

```

 *Quote:*   

> 
> 
> Should I have 'LoadModule php5_module modules/libphp5so' line in my httpd.conf or not
> 
> 

 

Yes it should, check my httpd.conf.

 *Quote:*   

> 
> 
> I also tried to c&p contents of /etc/apache2/modules.d/70_mod_php5.conf in my httpd.conf, but I think it didn't help. And I think it's something I'm not even supposed to do. 
> 
> 

 

No you shouldn't do that, just leave it in 70_mod_php5.conf.

Only put this into you httpd.conf if it isn't already there:

```
 Include /etc/apache2/modules.d/*.conf
```

 *Quote:*   

> 
> 
> There is 'AddDirectorIndex index.php index.phtml' in 70_mod_php5.conf but I still need to add '/index.php' in front of url to get page shown. 
> 
> 

 

Yes, that's correct.

Remove these lines from your httpd.conf if using PHP5:

```

LoadModule php4_module modules/libphp4.so

> DirectoryIndex index.html index.php

> AddHandler php-script php

> AddType text/html php

> AddType application/x-httpd-php-source phps 

```

And add this line to your httpd.conf:

```

AddType application/x-httpd-php .php 

```

Somethimes a re-emerge of PHP5 and Apache2 could solve the problem. Try it, it won't harm. It's just like a reinstall.

Config files which are allready there won't be overwritten.

Did you check the config i put here above in previous post?

You can try to use my config, just copy/past it into a file, after you renamed your httpd.conf to httpd.conf.old(or somthing like that, so you won't lose your config and put it back later)

What about your useflags? What is there in your /etc/make.conf ? (please post the line  USE=" ...... )

Check the forum for any errors you get. You won't be the onlyone with config issues. Just a learning process.

Hope we can find the solution this way for you.

First try to install Gentoo/Linux for you? Or only the fist try to install apache/php?

Don't worry, most of the time it is just a stupid config issue you overlook a 1000 times.

I know, have been wondering myself for days on this kind of things, but allways found the solution to be stupid easy  :Smile: 

----------

## Hannonball

First time installing Apache(2) or PHP. I've been using (Gentoo) Linux for a while, but quite a beginner with it too.

I put AddType in my httpd.conf, but it complains "Invalid command 'AddType'".

The only USE flag I'm using for apache is 'apache2'.

For PHP I use apache2, mysql, postgres, cgi.

EDIT: I now remerged apache + php, but still it does not work. It seems LoadModule php5_module modules/libphp5.so is not meant to be there since putting it in httpd.conf produces "[warn] module php5_module is already loaded, skipping" warning message when stopping/starting /etc/init.d/apache2.

It still does not work, and I'm starting to feel it's not getting to work  :Smile:  Thanks by the way RayVad (and others who spent time checking this thread)

EDIT 2: Heh, I got it some way to work. But it only works if I issue http://localhost/index.php in address bar, neither http://127.0.0.1/index.php nor http://*my dynamic ip*/index.php does work. This is a bit offtopic, but is this an error in my /etc/hosts? 

```

# /etc/hosts

127.0.0.1       localhost graybear

::1     localhost graybear

```

```

# /etc/conf.d/hostname

HOSTNAME="graybear"

```

----------

## RayVad

Great to hear that you are making progress and Apache is working.

I don't know what this ::1 in you hosts file is. Can you remove it and try again? Eventually do:   # /etc/init.d/net.eth0 restart

Did you try to open the page from the remote machine or local machine? Have you tried them both?

----------

## martin20450

Try adding -D PHP5 to the APACHE2_OPTS line in /etc/conf.d/apache2

----------

## Hannonball

I think ::1 is IPv6 localhost, not sure.

Also localhost and mydynamicip seems to work now, dunno why it did not work yesterday.

Thank you all.

Remerging + etc-update replaced my minimal httpd.conf with a new 'huge' (37KB) httpd.conf, and it may be why PHP got to work. Maybe.

----------

