# Web server q -- playing nice w/ PHP

**URL:** <https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460>\
**Category:** Rails\
**Created:** [December 4, 2005, 10:12pm UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460 "2005-12-04T22:12:17Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![viniosity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/58f4c7/32.png) [@viniosity](https://www.ruby-forum.com/u/viniosity)\
**Post date:** [December 4, 2005, 10:12pm UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460/1 "2005-12-04T22:12:17Z")

</div>

I have a webserver that’s happily running apache2 and PHP. I’d like to  
deploy a site that I wrote using RoR on that same server but read that  
there might be some conflicts with PHP and RoR trying to access the same  
MySQL database.

How real are those conflicts? Could I mess up my running PHP site if I  
install mod\_ruby and try to use Apache2?

Would I be better off installing lighttpd and running it and apache2 at  
the same time? (Is that even possible?)

---

<div class="post-metadata">

**Author:** ![viniosity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/58f4c7/32.png) [@viniosity](https://www.ruby-forum.com/u/viniosity)\
**Post date:** [December 4, 2005, 10:59pm UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460/2 "2005-12-04T22:59:46Z")

</div>

On 4-dec-2005, at 22:12, viniosity wrote:

> Would I be better off installing lighttpd and running it and  
> apache2 at  
> the same time? (Is that even possible?)

I wasn’t able to get PHP and mod\_ruby working with MySQL on the same  
Apache instance. Even when I compiled  
both of them myself. PHP had access to MySQL but Ruby didn’t. When I  
removed mod\_php from the list of modules Ruby can get to the database  
easily.

Running lighttpd proxied by Apache works reasonably well.

–  
Julian ‘Julik’ Tarkhanov  
me at julik.nl

---

<div class="post-metadata">

**Author:** ![viniosity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/58f4c7/32.png) [@viniosity](https://www.ruby-forum.com/u/viniosity)\
**Post date:** [December 4, 2005, 11:44pm UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460/3 "2005-12-04T23:44:29Z")

</div>

I recently took a site running apache2/mod\_php and converted it to  
running lighttpd/fastcgi. Fastcgi is now driving both a (legacy) PHP  
application and an RoR application, and RoR is connecting to the legacy  
app’s MySQL instance without issues.

I too was hesitant to migrate a functioning apache2/mod\_php application  
to lighttpd but it wasn’t that difficult. The hardest part was finding  
good docs and examples for the lighttpd.conf file. Once I had the config  
file ironed out everything went very smoothly. The site has been online  
for about six weeks now without any problems. Lighttpd only implements a  
small subset of apache2’s features, but IMO if everything you need is  
covered by lighttpd, it is a better choice than the apache2 + proxy +  
lighttpd solution.

If anyone is interested I’ll post my lighttpd.conf file here.

---

<div class="post-metadata">

**Author:** ![viniosity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/58f4c7/32.png) [@viniosity](https://www.ruby-forum.com/u/viniosity)\
**Post date:** [December 4, 2005, 11:58pm UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460/4 "2005-12-04T23:58:35Z")

</div>

.

> If anyone is interested I’ll post my lighttpd.conf file here.

I’m definitely interested! Actually, if you have step by step  
instructions that would be fab. Which distro did you use? I’m  
currently on Debian sarge which does not have a package for lighttpd…

---

<div class="post-metadata">

**Author:** ![viniosity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/58f4c7/32.png) [@viniosity](https://www.ruby-forum.com/u/viniosity)\
**Post date:** [December 5, 2005, 2:05am UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460/5 "2005-12-05T02:05:15Z")

</div>

viniosity wrote:

> I have a webserver that’s happily running apache2 and PHP. I’d like to  
> deploy a site that I wrote using RoR on that same server but read that  
> there might be some conflicts with PHP and RoR trying to access the same  
> MySQL database.

Here I was running Apache2 + mod\_php4. I added rails sites without too  
much efforts using scgi. The single non-trivial point is to give a  
separate scgi port number to each vhost.

---

<div class="post-metadata">

**Author:** ![viniosity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/58f4c7/32.png) [@viniosity](https://www.ruby-forum.com/u/viniosity)\
**Post date:** [December 5, 2005, 5:37pm UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460/6 "2005-12-05T17:37:36Z")

</div>

On Dec 4, 2005, at 6:25 PM, Vince W. wrote:

> My situation is that I have a rails app I want to deploy but only have  
> an apache2/php4 server at my disposal.

```
SCGI is definitely a nice option but I have had very good luck with

```

lighttpd proxied behind and apache vhost. This would allow you to  
pretty much leave your good running apache alone and just add one  
vhost per rails site like this:

\<VirtualHost \*:80\>  
ServerName [example.com](http://example.com)  
ServerAlias [www.example.com](http://www.example.com)  
#ProxyPreserveHost on #\<-apache2 only  
ProxyPass / [http://example.com:8000/](http://example.com:8000/)  
ProxyPassReverse / [http://example.com:8000/](http://example.com:8000/)

```
Then run your rails app on fastcgi/lighttpd. This way you can

```

connect to your db from php and ruby with no problems. This is a  
tried and trusted way of doing things and m any shared web hosts that  
offer rails support do it this way.

Cheers-  
-Ezra Z.  
WebMaster  
Yakima Herald-Republic Newspaper  
removed\_email\_address@domain.invalid  
509-577-7732

---

<div class="post-metadata">

**Author:** ![viniosity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/58f4c7/32.png) [@viniosity](https://www.ruby-forum.com/u/viniosity)\
**Post date:** [December 5, 2005, 3:25am UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460/7 "2005-12-05T03:25:04Z")

</div>

> Here I was running Apache2 + mod\_php4. I added rails sites without too  
> much efforts using scgi. The single non-trivial point is to give a  
> separate scgi port number to each vhost.

I would appreciate any guidance you can give. I am not familiar with  
scgi at all and so am not familiar with what it would take to  
install/configure it and/or rails on my webserver.

My situation is that I have a rails app I want to deploy but only have  
an apache2/php4 server at my disposal.

---

<div class="post-metadata">

**Author:** ![viniosity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/58f4c7/32.png) [@viniosity](https://www.ruby-forum.com/u/viniosity)\
**Post date:** [December 5, 2005, 6:27pm UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460/8 "2005-12-05T18:27:03Z")

</div>

Vince W. wrote:

> My situation is that I have a rails app I want to deploy but only have  
> an apache2/php4 server at my disposal.

Same here 😉

> I would appreciate any guidance you can give. I am not familiar with  
> scgi at all and so am not familiar with what it would take to  
> install/configure it and/or rails on my webserver.

I first became familiar with scgi (er… simply I installed mod\_scgi and  
scgi\_rails on the dev box, read all the docs that Zed wrote and made  
some trys). Then some days later I made the same on the production box.

---

<div class="post-metadata">

**Author:** ![viniosity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/58f4c7/32.png) [@viniosity](https://www.ruby-forum.com/u/viniosity)\
**Post date:** [December 5, 2005, 6:10pm UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460/9 "2005-12-05T18:10:34Z")

</div>

Here are the important sections of my lighttpd.conf file,  
with some comments added. I am using Gentoo but I don’t think  
there is anything distro-specific here.

This config was pieced together from many examples. I’d love  
to hear comments or suggestions if anyone has them.

One caveat: Initially I had trouble getting the rails  
application to route properly. I traced the problem to  
some of the rails code referenced in this ticket/patch:

[http://dev.rubyonrails.org/ticket/1048](http://dev.rubyonrails.org/ticket/1048)

Removing the apache-specific condition in relative\_url\_root  
fixed the problem. I haven’t been able to determine if this  
is a problem in my setup or in rails itself.

-----------------------------------------start

###########################################################  
###########################################################

# 

# lighttpd.conf file serving both PHP and Ruby on Rails

# applications via fastcgi. This file is for lighttpd 1.4.7

# 

# removed\_email\_address@domain.invalid

# 

#######################################################

# General server config, see lighttpd docs for details.

var.logdir = “/var/log/lighttpd”

server.modules = (  
“mod\_rewrite”,  
“mod\_access”,  
“mod\_auth”,  
“mod\_accesslog”,  
“mod\_fastcgi”,  
“mod\_alias”  
)

include “mime-types.conf”

server.username = “lighttpd”  
server.groupname = “lighttpd”  
server.pid-file = “/var/run/lighttpd.pid”  
server.indexfiles = (“dispatch.fcgi”, “index.php”, “index.html”,  
“index.htm”, “default.htm”)

server.tag = “lighttpd”  
server.follow-symlink = “enable”

static-file.exclude-extensions = (“.php”, “.pl”, “.cgi”, “.fcgi”, “.rb”,  
“.rhtml”)  
url.access-deny = (“~”, “.inc”, “.conf”)

server.errorlog = var.logdir + “/error.log”  
accesslog.filename = var.logdir + “/access.log”

########################################################

# By default, display an empty website. Rules below will

# override this setting. This may not be appropriate or

# necessary for all sites.

server.document-root = “/var/www/empty”

########################################################

# A common directory for all web roots (optional).

alias.url = (“/common/” =\> “/var/www/common/”)

############################################################

# Main configuration. In this config, the root of the web

# site hosts a legacy PHP application ([http://the-site.com](http://the-site.com))

# while an RoR application is available beneath the root at

# [http://the-site.com/rorapp](http://the-site.com/rorapp)

$HTTP[“host”] =~ “[the-site.com](http://the-site.com)” {

# Root of the php application.

server.document-root = “/var/www/phpapp”

# Rewrite all requests to /rorapp to use dispatch.fcgi, except

# static content. The rails app is in /var/www/rorapp. There is

# a symlink from /var/www/phpapp/rorapp → /var/www/rorapp/public.

url.rewrite = (“^/rorapp/stylesheets/(.+)” =\>  
“/rorapp/stylesheets/$1”,  
“^/rorapp/images/(.+)” =\> “/rorapp/images/$1”,  
“^/rorapp/javascripts/(.+)” =\>  
“/rorapp/javascripts/$1”,  
“^/rorapp/(.\*)$” =\> “/rorapp/dispatch.fcgi”)

# Fastcgi configuration. The PHP application is spawned externally

# via spawn-fcgi, while the RoR application is spawned and monitored

# using the rails command script/process/spinner.

# PHP must be compiled with fastcgi support. PHP/spawn-fcgi will

# handle multiple processes internally, you only need to specify

# the base port number here. See the spawn-fcgi docs for more info.

# For the rails app, you need to specify each port spawned by

# the spinner/spawner. This example uses three handlers. You may

# need to install the fcgi ruby gem as well.

fastcgi.server = ( “.php” =\>  
( “localhost” =\>  
(  
“host” =\> “127.0.0.1”,  
“port” =\> 1200  
)  
),

```
".fcgi" => (
  "localhost" =>
  (
    "host" => "127.0.0.1",
    "port" => 1220
  ),
  (
    "host" => "127.0.0.1",
    "port" => 1221
  ),
  (
    "host" => "127.0.0.1",
    "port" => 1222
  )
)

```

)  
}

################################################

# SSL (optional). Specify the IP address of your

# external interface here.

$SERVER[“socket”] == “172.16.0.1:443” {  
ssl.engine = “enable”  
ssl.pemfile = “/etc/lighttpd/ssl/the-site.com.pem”  
ssl.ca-file = “/etc/lighttpd/ssl/the-site.com.ca”  
}

###########################################################

# Another web root is specified here. In my implementation

# I have several of these serving different sites. This

# is optional of course.

$HTTP[“host”] =~ “[another-site.com](http://another-site.com)” {  
ssl.engine = “disable”  
server.document-root = “/var/www/another-site”  
}

---------------------------------------------end

---

<div class="post-metadata">

**Author:** ![viniosity](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/58f4c7/32.png) [@viniosity](https://www.ruby-forum.com/u/viniosity)\
**Post date:** [December 8, 2005, 12:39am UTC](https://www.ruby-forum.com/t/web-server-q-playing-nice-w-php/49460/10 "2005-12-08T00:39:12Z")

</div>

Ok, I tried and tried and completely failed to get this working. In a  
moment of complete frustration I erased my box and started from scratch.  
This time with Ubuntu instead of Debian. Step 1: Get Rails working.  
Step 2. Get PHP working.

Thanks to this fantastic tutorial I was able to accomplish Step 1.  
[http://fo64.com/articles/2005/10/20/rails-on-breezy](http://fo64.com/articles/2005/10/20/rails-on-breezy)

Now can anyone help me get my PHP sites back up? I assume I have to  
disable FastCGI for the particular site in Apache2’s sites-enabled and  
sites-available conf files but that’s just me guessing…
