# Apache with fastcgi



## kosovafan (Feb 2, 2011)

Hello,

i have installed Apache from ports and try since 2 days a config with fastcgi / php. But the Apache like not the PHP over CGI.

I have read something from in other forum which say all should use spawn-fcgi. So im not sure it's correct what i do, here my config:


```
/usr/local/etc/rc.d/apache22/httpd.conf
LoadModule fastcgi_module libexec/apache22/mod_fastcgi.so
```


```
/usr/local/etc/rc.d/apache22/extra/vhost.conf
ServerAdmin siefke@email.de
SuexecUserGroup silviosiefke silviosiefke
DocumentRoot "/usr/local/www/silvio_backup"
ServerName silviosiefke.de
ServerAlias www.silviosiefke.de
ErrorLog "/usr/local/www/silviosiefke/logs/error.log"
CustomLog "/usr/local/www/silviosiefke/logs/access.log" common
ScriptAlias /cgi-bin/ "/usr/local/www/silviosiefke/cgi-bin/"
AddHandler php5-fastcgi .php
FastCgiExternalServer /usr/local/www/silvio_backup -host 127.0.0.1:9000
 
<Directory "/usr/local/www/silvio_backup">
AllowOverride None
Options +ExecCGI MultiViews -Indexes
Order allow,deny
Allow from all
</Directory>

<Directory "/usr/local/www/silviosiefke/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
```


```
# httpd -M
Loaded Modules:
 core_module (static)
 mpm_prefork_module (static)
 http_module (static)
 so_module (static)
 authn_file_module (shared)
 authn_default_module (shared)
 authz_host_module (shared)
 authz_groupfile_module (shared)
 authz_user_module (shared)
 auth_basic_module (shared)
 file_cache_module (shared)
 cache_module (shared)
 disk_cache_module (shared)
 reqtimeout_module (shared)
 charset_lite_module (shared)
 deflate_module (shared)
 log_config_module (shared)
 env_module (shared)
 mime_magic_module (shared)
 cern_meta_module (shared)
 unique_id_module (shared)
 setenvif_module (shared)
 mime_module (shared)
 status_module (shared)
 autoindex_module (shared)
 asis_module (shared)
 suexec_module (shared)
 cgid_module (shared)
 cgi_module (shared)
 negotiation_module (shared)
 dir_module (shared)
 actions_module (shared)
 alias_module (shared)
 rewrite_module (shared)
 fastcgi_module (shared)
Syntax OK
```


```
# httpd -V
Server version: Apache/2.2.17 (FreeBSD)
Server built:   Feb  2 2011 12:53:55
Server's Module Magic Number: 20051115:25
Server loaded:  APR 1.4.2, APR-Util 1.3.10
Compiled using: APR 1.4.2, APR-Util 1.3.10
Architecture:   32-bit
Server MPM:     Prefork
  threaded:     no
    forked:     yes (variable process count)
Server compiled with....
 -D APACHE_MPM_DIR="server/mpm/prefork"
 -D APR_HAS_SENDFILE
 -D APR_HAS_MMAP
 -D APR_HAVE_IPV6 (IPv4-mapped addresses enabled)
 -D APR_USE_FLOCK_SERIALIZE
 -D APR_USE_PTHREAD_SERIALIZE
 -D SINGLE_LISTEN_UNSERIALIZED_ACCEPT
 -D APR_HAS_OTHER_CHILD
 -D AP_HAVE_RELIABLE_PIPED_LOGS
 -D DYNAMIC_MODULE_LIMIT=128
 -D HTTPD_ROOT="/usr/local"
 -D SUEXEC_BIN="/usr/local/sbin/suexec"
 -D DEFAULT_PIDLOG="/var/run/httpd.pid"
 -D DEFAULT_SCOREBOARD="logs/apache_runtime_status"
 -D DEFAULT_LOCKFILE="/var/run/accept.lock"
 -D DEFAULT_ERRORLOG="logs/error_log"
 -D AP_TYPES_CONFIG_FILE="etc/apache22/mime.types"
 -D SERVER_CONFIG_FILE="etc/apache22/httpd.conf"
```


```
#Spawn-FCGI (/etc/rc.conf)
spawn_fcgi_enable="YES"
spawn_fcgi_app="/usr/local/bin/php-cgi"
spawn_fcgi_pidfile="/var/run/spawn-fcgi.pid"
spawn_fcgi_username="www"
spawn_fcgi_groupname="www"
spawn_fcgi_bindaddr="127.0.0.1"
spawn_fcgi_bindport="9000"
#spawn_fcgi_bindsocket="/tmp/php.socket"
#spawn_fcgi_bindsocket_mode="0777"
spawn_fcgi_children="5"
spawn_fcgi_max_requests="1000"
```


```
/var/log/apache/httpd-error.log
[Wed Feb 02 13:32:35 2011] [notice] caught SIGTERM, shutting down
[Wed Feb 02 14:22:13 2011] [notice] suEXEC mechanism enabled (wrapper: /usr/local/sbin/suexec)
[Wed Feb 02 14:22:14 2011] [notice] FastCGI: process manager initialized (pid 2388)
[Wed Feb 02 14:22:15 2011] [notice] Apache/2.2.17 (FreeBSD) mod_fastcgi/2.4.6 configured -- resuming normal operations
```


```
/var/log/apache/virtuellerserver.log
- - [02/Feb/2011:14:22:25 +0100] "GET /phpinfo.php HTTP/1.1" 200 17
```

Since 2 days i try all, but Apache say no. Before i has use a config with suPHP, but this make more Problems and now i must change the CMS, so need the new configuration. 


Im happy when someone has idea. 


Regards
Silvio


----------



## AndyUKG (Feb 2, 2011)

```
/var/log/apache/httpd-error.log
[Wed Feb 02 13:32:35 2011] [notice] caught SIGTERM, shutting down
[Wed Feb 02 14:22:13 2011] [notice] suEXEC mechanism enabled (wrapper: /usr/local/sbin/suexec)
[Wed Feb 02 14:22:14 2011] [notice] FastCGI: process manager initialized (pid 2388)
[Wed Feb 02 14:22:15 2011] [notice] Apache/2.2.17 (FreeBSD) mod_fastcgi/2.4.6 configured -- resuming normal operations
```

In your error log it seems to just show you doing a shutdown at 13.32 and then a start at 14.22. What is the error you are seeing?

thanks Andy.


----------



## Alt (Feb 3, 2011)

Why you need FCGI for PHP ? Its already working under mod_php which give is almost same speed...
Anyway if you are going to move PHP to FCGI better use different server (lighthttpd/nginx). Cus you will get no benefit using apache (he will fork() anyway).


----------



## kosovafan (Feb 4, 2011)

Hello,


*S*o *I* have found the problems. 

First the step for install the system when apache and php (compiled with cgi) is on system:


```
cd /usr/ports/www/mod_fastcgi && make install clean
cd /usr/ports/www/spawn_fcgi && make install clean

Activate in httpd.conf:
LoadModule fastcgi_module libexec/apache22/mod_fastcgi.so
AddHandler php5-fastcgi .php
FastCgiExternalServer /usr/local/www/apache22/data -host localhost:9000

echo 'spawn_fcgi_enable="YES"' >> /etc/rc.conf
service spawn-fcgi start
service apache22 start
```

The mistake by me was, that in default "short_open_tag" is off. Set it on or write the code with <?php phpinfo(); ?>. 


Regards
Silvio


----------



## nORKy (Feb 5, 2011)

Why not php-fpm?

Alt, myself I use php-fpm (or spawn_fcgi) because I can't use suexec with mod_php. It's important because we have many users and websites.


----------



## kosovafan (Feb 5, 2011)

Hello,

Sorry for my question, can you explain what you mean with php-fpm? Haven't heard of it before.

Regards
Silvio


----------



## kosovafan (Feb 5, 2011)

Hey,

@nORKy

I have looked now at the website. Its sounds well. Do you use php-fpm? For example, with fastcgi and PHP 5.3.3 I have problems with DirectoryListings. It gives two ways to fix the problem.

1.) use Rewrite Engine

```
RewriteEngine On
RewriteCond %{REQUEST_URI} \/$
RewriteRule ^(.*)$ index.php [L,QSA]
```

2.) Use AliasFakes

```
AddHandler php5-fastcgi .php
Action php5-fastcgi /fake-handler
FastCgiExternalServer /usr/local/www/data/fake-handler -host 127.0.0.1:9000
Alias /fake-handler /usr/local/www/data
```

The last make problems when you use RewriteRules then, you have error 503. 

So what I read is intresting, but the install, it's only a patch? How must I use it with PHP Port, and the configuration?

Can you explain?

Silvio


----------



## alexus (Mar 12, 2012)

I'm having the same issue and I can't find anywhere how one would resolve this issue.

*S*o how is it "Solved"??


----------



## kosovafan (Mar 12, 2012)

Hello,

For me it is not solved, I changed the webserver, *I* use nginx and with it it runs without any problems. 


Regards
Silvio


----------



## alexus (Mar 13, 2012)

kosovafan said:
			
		

> Hello,
> 
> For me it is not solved, I changed the webserver, *I* use nginx and with it it runs without any problems.
> 
> ...



*S*o why did you change it as "solved" then when clearly it's not. *I*'m thinkin*g* of switchin*G* to nginx too, but not by choice just because *I* can't get apache to work with fastcgi properly.


----------



## DutchDaemon (Mar 13, 2012)

[Solved] removed, because that appears to have been premature.


----------



## kosovafan (Mar 13, 2012)

Hello,

*I* have not changed to solved. I have tried many Howtos and nothing really was correct. So *I* switched to nginx and fixed it for me. I follow the dialogs in net for apache but at the moment nothing really runs correctly.



Regards
Silvio


----------



## alexus (Mar 13, 2012)

That sucks. *I*'m thinking about switching to nginx as well, that's something *I* have very little experience with and it's nothing like Apache.


----------



## nORKy (Mar 13, 2012)

I don't understand the problem. My directory listing is OK


----------



## alexus (Mar 13, 2012)

nORKy said:
			
		

> I don't understand the problem. My directory listing is OK



*A*re you using Apache + mod_fastcgi? if so please share your configuration, especially the part for mod_fastcgi.


----------

