I am a PHP developer who has started learning Ruby on Rails. I love how easy it is to get up and running developing Rails applications. One of the things I love most is WEBrick. It makes it so you don't have to configure Apache and Virtual Hosts for every little project you are working on. WEBrick allows you to easily start up and shut down a server so you can click around your web application.
I don't always have the luxury of working on a Ruby on Rails app, so I was wondering how I might configure (or modify) WEBrick to be able to use it to serve up my PHP projects and Zend Framework applications.
Have you attempted this? What would be the necessary steps in order to achieve this?
To get php support in webrick you can use a handler for php files. To do this you have to extend HttpServlet::AbstractServlet and implement the do_GET and do_POST methods. These methods are called for GET and POST requests from a browser. There you just have to feed the incoming request to php-cgi.
To get the PHPHandler to handle php files you have to add it to the HandlerTable of a specific mount. You can do it like this:
s = HTTPServer.new(
:Port => port,
:DocumentRoot => dir,
:PHPPath => phppath
)
s.mount("/", HTTPServlet::FileHandler, dir,
{:FancyIndexing => true, :HandlerTable => {"php" => HTTPServlet::PHPHandler}})
The first statement initializes the server. The second adds options to the DocumentRoot mount. Here it enables directory listings and handling php files with PHPHandler. After that the server can be started with s.start().
I have written a PHPHandler myself as I haven't found one somewhere else. It is based on webricks CGIHandler, but reworked to get it working with php-cgi. You can have a look at the PHPHandler on GitHub:
https://github.com/questmaster/WEBrickPHPHandler
You can use nginx or lighttpd
Here's a minimal lighttpd config.
Install PHP with FastCGI support and adjust the "bin-path" option below for your system. You can install it with MacPorts using sudo port install php5 +fastcgi
Name this file lighttpd.conf
then simply run lighttpd -f lighttpd.conf from any directory you'd like to serve.
Open your webbrowser to localhost:8000
lighttpd.conf:
server.bind = "0.0.0.0"
server.port = 8000
server.document-root = CWD
server.errorlog = CWD + "/lighttpd.error.log"
accesslog.filename = CWD + "/lighttpd.access.log"
index-file.names = ( "index.php", "index.html",
"index.htm", "default.htm" )
server.modules = ("mod_fastcgi", "mod_accesslog")
fastcgi.server = ( ".php" => ((
"bin-path" => "/opt/local/bin/php-cgi",
"socket" => CWD + "/php5.socket",
)))
mimetype.assign = (
".css" => "text/css",
".gif" => "image/gif",
".htm" => "text/html",
".html" => "text/html",
".jpeg" => "image/jpeg",
".jpg" => "image/jpeg",
".js" => "text/javascript",
".png" => "image/png",
".swf" => "application/x-shockwave-flash",
".txt" => "text/plain"
)
# Making sure file uploads above 64k always work when using IE or Safari
# For more information, see http://trac.lighttpd.net/trac/ticket/360
$HTTP["useragent"] =~ "^(.*MSIE.*)|(.*AppleWebKit.*)$" {
server.max-keep-alive-requests = 0
}
If you'd like to use a custom php.ini file, change bin-path to this:
"bin-path" => "/opt/local/bin/php-fcgi -c" + CWD + "/php.ini",
If you'd like to configure nginx to do the same, here's a pointer.
I found this, but I really think it isn't worth the hassle. Is making a virtual host (which isn't even necessary) that difficult? In the time it would take you to set this up to work with PHP, if you can even get it working, you could have written a script that creates virtual host entries for you, making it as easy as webrick.
It looks like WEBrick has CGI support, which implies that you can get PHP running by invoking it as a CGI script. The #! line at the top of each executable file would just need to point towards the absolute path to php-cgi.exe.
It's worth noting that you'd need to remove the #! line when moving the file to any other server that doesn't think of PHP as a CGI script, which would be ... uh ... all of'em.
Related
I am trying to retrieve a network file from an OpenSUSE 13.1 Host to my OpenSUSE 13.2 Webserver, but I don't seem to have any success.
First I check if target directory is a directory:
$path = "\\\\192.168.xxx.xxx\\public";
if(is_dir($path)){ // returns FALSE every time.
return true;
}
In this case the function is_dir() returns FALSE, although the folder exists and has 777 permisions, the IP is correct, and the computer is turned ON.
I have tried all the combinations of formatting the network path, including $path = "smb://192.168.xxx.xxx/public"; witch returns an error
Unable to find the wrapper "smb" - did you forget to enable it when
you configured PHP?
Could someone tell me what I am missing?
Would it work the same through a OpenVPN with my Centos 6 webserver and an Ubuntu 14 host?
PHP has no built-in Samba support on the Linux platform. You will need to mount the remote share (requires superuser-equivalent access) and access its contents like a local filesystem.
I'm using the PECL extension to extract the ID3 tags from an mp3 and this is my code to print_r the array :
<?php
$tag = id3_get_tag( "/var/www/music/rem.mp3");
print_r ($tag);
?>
but when i run the file in the browser nothing happens i tried to execute it in the terminal and it works perfectly fine it gives me the output:
maniteja#maniteja:~$ sudo php /var/www/index.php
Array
(
[title] => Lexter - Who's Laughing now (Mr. Day Lens remix)
[album] => LEXTER - Who's Laughing now
[releaseTime] => 2013
[track] => 01
[genre] => (255)
[artist] => Алексей
[publisher] => Jamendo
[copyright] => 2013-11-28T21:32:03+01:00 Алексей. Licensed to the public underhttp://creativecommons.org/licenses/by/3.0/ verify at http://www.jamendo.com/album/129077/
[encodedBy] => Jamendo : http://www.jamendo.com | LAME
[commInfo] => http://www.jamendo.com
[copyrightInfo] => http://creativecommons.org/licenses/by/3.0/
[webOffAudioFile] => http://www.jamendo.com/en/track/1083749
[webOffArtist] => http://www.jamendo.com/en/artist/Mr._Day_Lens
[webOffAudioSrc] => http://www.jamendo.com/en/album/129077
[webOffPubl] => http://www.jamendo.com
[taggingTime] => 2013-11-28T20:33:57
)
is syntax wrong??? I'm new at this..
Edit:
As #ajtrichards says:
"You don't have permission to read the files in /var/www/music. You'll need to allow access to your web server"
Go to your /var/www/music folder and give the right permissions for your server to READ in there.
If you use Apache:
How to make a directory apache readable on ubuntu
PHP has two environments.
The web environment.
The CLI environment.
Your extension works perfectly on the CLI environment because you added the PECL extension on the CLI php.ini.
You need to make sure the extension is added to the web php.ini.
To test it, make a test.php with a phpinfo call.
And access it via browser.
This will output all the php.ini configuration for the "web" PHP.
Then search for your ID3 extension in all the list of extensions.
If it does not display, then is not added to the "web environment" php.ini.
To add it, you must do the same that you did to add it to the CLI php.ini
Something like, adding this line
extension=id3.so
PS:
Dude, where's my php.ini?
More info on ID3
http://www.php.net/manual/en/id3.installation.php
This works via the command line PHP as the user root has permission to read from the folder /var/www/music, as Maniteja uses sudo php to run the php file.
However, when the script is running via PHP in the web browser the web server does not have the permission to read from the folder, hence the error:
PHP Warning: id3_get_tag(/var/www/music/rem.mp3): failed to open
stream: Permission denied in /var/www/index.php on line 2
To resolve the problem - grant read access to your web server user.
I have a PHP script that interacts with OS's CLI, and I'm expecting this script to run on different Linux distributions, so I have to interact with different Linux falvours differently to achieve something, but I couldn't find a way for PHP to differentiate between them.
I've tried using php_uname('s') and PHP_OS they both return Linux; which isn't any useful for me(I was testing on Arch Linux)
I'm asking this question, because -for example- if I wanted to manage a service/daemon in Ubuntu I'll use the service command , and in Arch ill be using the systemctl command, so without knowing which Linux distro is running my PHP script there's no way I can deal with such problems.
I found a flexible solution to this issue. Which is by looking-up the release file from the /etc directory(instead of printing what's actually INSIDE the file), so if I'm running this script on Arch Linux, it'll check for arch-release file in /etc directory, and so on for other Linux distros.
Here's the code:
IMPORTANT NOTES:
PHP won't scan /etc directory if you don't modify php.ini file and make it allowed to do file operations on it(modify open_basedir option, by adding :/etc/ to the end.)
The list of recognizable distros in the following code is easily extensible, all you need is the pretty name of the distro(e.g. Ubuntu) and the name of its release file(e.g. lsb-release). For a complete list of famous linux distros, click here.
function getLinuxDistro()
{
//declare Linux distros(extensible list).
$distros = array(
"Arch" => "arch-release",
"Debian" => "debian_version",
"Fedora" => "fedora-release",
"Ubuntu" => "lsb-release",
'Redhat' => 'redhat-release',
'CentOS' => 'centos-release');
//Get everything from /etc directory.
$etcList = scandir('/etc');
//Loop through /etc results...
$OSDistro;
foreach ($etcList as $entry)
{
//Loop through list of distros..
foreach ($distros as $distroReleaseFile)
{
//Match was found.
if ($distroReleaseFile === $entry)
{
//Find distros array key(i.e. Distro name) by value(i.e. distro release file)
$OSDistro = array_search($distroReleaseFile, $distros);
break 2;//Break inner and outer loop.
}
}
}
return $OSDistro;
} }
PHP uname will usually return what uname would in console, which is usually the Linux kernel. Since different distributions have different ways of telling their version, I cannot suggest a deffinitive method for all of them, however, have a read at this: http://www.cyberciti.biz/faq/find-linux-distribution-name-version-number/
Combine that with php's exec
Well, I'm wondering if theres a way to run both rails and PHP on Lighty, on Ubuntu. I want to run both my PHP projects and Rails projects on the one server/domain.
I have little experience with Linux really, so forgive my naivety.
If theres a way of doing this please let me know :)
It's really quite simple to run them both. I do it all the time (ROR to run Redmine, and PHP for the rest).
You have 2 real options for ROR. Either serve it from FastCGI (what I do), or run it with a standalone server (like Mongrel, etc) and proxy to it. Both have advantages. FastCGI has the advantage that it's self-contained (no secondary server to run). The standalone has the advantage that it's easier to configure.
If you have specific questions, I can guide, but there are guides on the internet on how to do this.
My lighttpd.conf:
$HTTP["host"] =~ "my.ror.site" {
server.error-handler-404="/dispatch.fcgi"
fastcgi.server = (".fcgi" => ("ror_1" => (
"min-procs"=>8,
"max-procs" => 8,
"socket" => "/tmp/myrorlock.fastcgi",
"bin-path"=> "/path/to/ror/site/public/dispatch.fcgi",
"kill-signal" => 9,
"bin-environment" => ( "RAILS_ENV" => "production" )
)))
}
fastcgi.server = ( ".php" =>
(
(
"socket" => "/tmp/php-fastcgi.socket",
"bin-path" => "/usr/bin/php-cgi -c /etc/php.ini",
"min-procs" => 1,
"disable-time" => 1,
"max-procs" => 1,
"idle-timeout" => 20,
"broken-scriptfilename" => "enable",
"bin-copy-environment"=> (
"PATH", "SHELL", "USER"
),
"bin-environment" => (
"PHP_FCGI_CHILDREN" => "40",
"PHP_FCGI_MAX_REQUEST" => "50000"
)
)
)
)
And that's it. Note the kill-signal option. that's important, otherwise you'll wind up with zombie processes everywhere every time you restart the server...
Check out fastcgi.conf in the conf.d subdirectory of Lighty's configuration directory (not sure where it's located on Ubuntu, but a quick search suggests /etc/lighttpd). There are commented-out examples for both PHP and Rails; by combining the two, you should be able to get the set-up you're looking for (though I'd suggest getting one working first and then setting up the other).
FastCGI is the method by which Lighty can communicate with runtimes like Ruby or PHP. Lighty can also use SCGI, though I've never use it myself and am not sure how well it works (last I heard it was still experimental-ish).
You may also find the Optimizing FastCGI page on Lighty's documentation wiki helpful, though it's fairly PHP/MySQL-specific.
I don't use Lighty. Rails is best served with Passenger and Apache, considering the power of Passenger add-on to Apache. I served Wordpress (PHP) in the same domain as my Rails app by pointing its path to somewhere else. Here's an article to follow. HTH.
I am using lighty for windows, yes i know it's not linux, but atm can only afford local hosting, which then allows me to do a lot of learning and practicing my web skills.
I am aware that fast-cgi, does not work on windows, but I am wondering what other ways, to improve performance are there?
Also I was wondering how to hide all those lightpd.exe window/boxes that come up, everytime anyone or a bot visits the site...can lighttpd be run from the background? I am running it as a service, and that is fine...
But all in all, why is there so little support for lighty on windows?
And I really could care less for 1 more lecture on why everything should be on linux or windows...That discussion is really a waste of time...mine and yours...
If you have some useful information, I definitely want to hear it.
I guess I am one of those guys, who always wants to learn how to improve things, it's like a drug for me, to eak out any percent more in performance...
Like for example, I have added a subdomain, because yslow loves subdomain hosting of images,css and javascript...
I really like lighty, just hope I am not the only one there...using it on windows...and all the lighty for windows sites seem to be dead...or forgotten...
Thank You for your time..
-Craig
I also run lighttpd for Windows, but I've made my own very well optimized lighttpd mod with PHP and Python support which I run from a USB pen drive, since I switched to Windows 7 all the command line windows keep appearing whenever I access the server (I also don't know how to keep this from happening).
I did several things to make my lighttpd server faster (since I run it from a USB pen drive):
disable all kinds of logs (specially access logs)
keep the config file as small as possible (mine has only 20 lines)
activate PHP only on .php files, Python only on .py files
disable all kinds of modules that you don't need, like SSL and so on (I only have 5)
Here it is, my config file:
var.Doo = "C:/your/base/path/here"
# LightTPD Configuration File
server.port = 80
server.name = "localhost"
server.tag = "LightTPD/1.4.20"
server.document-root = var.Doo + "/WWW/"
server.upload-dirs = ( var.Doo + "/TMP/" )
server.errorlog = var.Doo + "/LightTPD/logs/error.log"
server.modules = ( "mod_access", "mod_cgi", "mod_dirlisting", "mod_indexfile", "mod_staticfile" )
# mod_access
url.access-deny = ( ".db" )
# mod_cgi
cgi.assign = ( ".php" => var.Doo + "/PHP/php-cgi.exe", ".py" => var.Doo + "/Python/python.exe" )
# mod_dirlisting
dir-listing.activate = "enable"
# mod_indexfile
index-file.names = ( "index.php", "index.html" )
# mod_mimetype
mimetype.assign = ( ".css" => "text/css", ".gif" => "image/gif", ".html" => "text/html", ".jpg" => "image/jpeg", ".js" => "text/javascript", ".png" => "image/png", ".txt" => "text/plain", ".xml" => "text/xml" )
# mod_staticfile
static-file.exclude-extensions = ( ".php", ".py" )
And the modules that I've active:
mod_access
mod_cgi
mod_dirlisting
mod_indexfile
mod_staticfile
Bottom line is, even when running from the USB pen the server still is blazing fast.
PS: I also considered switching to nginx but given the current performance I can get and the even smaller user base of nginx I decided I would keep LightTPD.
By local hosting, I'm guessing you mean on your own box, so essentially free. If you're not too strapped for cash, you could probably pick up a cheap box, and install a headless linux on there. Well, that's only if you're adverse to using linux as a desktop...
So, first, since you're only learning, I'm assuming you're not trying to put up a production site yet, so you can shut down lighty when you're not using it (getting rid of the boxes popping up for bots). Excuse me if this is unacceptable, since there is probably a solution out there (and how are you getting bots for a sandbox site? oO). Same goes for the performance: it's just a testing grounds, so optimization shouldn't matter too much yet (don't worry about it: remember the maxim that premature optimization is the root of all... something). If you still want fastcgi, there's another stackoverflow question/answer on that: FastCGI on Windows and Lighttpd. Also, check out scgi, which might be a different story on windows.
Also, here's some thoughts from Atwood on yslow: codinghorror.com/blog/archives/000932.html
Finally; last I checked, lighty was no where near as popular as apache, meaning a much smaller user base. When you also consider IIS, then lighty wouldn't really have that many users under Windows. Just noting, you might have a not-so-smooth road ahead of you if you want to continue with lighttpd on windows. Also note, you'll probably end up shifting the server to another box or offsite eventually. I've served stuff from my desktop, and it's not all too fun in the long run.
Try nginx - another lightweight alternative to apache, fast and stable. fastcgi on windows works ok.
Regarding your question - I think the reason is that lighttpd is loosing its popularity, look at the web server stats. So less people use it, less features are tested, more bugs are lurking around.