Alternative web server for PHP: React or PHP's embedded server?

4.5k Views Asked by At

I always used Apache for my PHP projects. But now, I want to leave it, to start using one of the following options:

React
or
PHP's built-in web server

Which has more performance? In definitely, what is the best? Which is your recommendation?

2

There are 2 best solutions below

1
On

Ok, so there's this cool thing called nginx. DONT STOP READING YET

One of the things that a lot of people don't realize is that nginx can do proxy requests, very quickly and very efficiently.

You may ask, what is a proxy request?! Then good.

A proxy request is recieved from the remote user to nginx. Nginx will receive that, and then forward the request along to another listener. This listener can be a unix domain socket, Apache, another nginx server on the network, or even the internal PHP built-in server.

Personally, it sounds like you don't know much about PHP-FPM, which is a socket-based parser for PHP. Back in the old days of Apache, people used to use this thing called mod_php where Apache would manually parse the request using the shared library. This was HORRIBLE.

If you are looking to leave because of that reason, I'd suggest upgrading, and boosting your performance by using PHP-FPM with Nginx. Rackspace has a nice article on this: http://www.rackspace.com/knowledge_center/article/installing-nginx-and-php-fpm-setup-for-nginx

Snippet:

Example nginx config:

server {
    server_name  www.DOMAINNAME;
    rewrite ^(.*) http://DOMAINNAME$1 permanent;
}

server {
        listen 80;
        server_name DOMAINNAME;
                root   /var/www/DOMAINNAME/htdocs;
                index index.php;
        include /etc/nginx/security;

        # Logging --
        access_log  /var/log/nginx/DOMAINNAME.access.log;
        error_log  /var/log/nginx/DOMAINNAME.error.log notice;

        # serve static files directly
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
            access_log        off;
            expires           max;
        }

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm/DOMAINNAME.socket;
                fastcgi_index index.php;
                include /etc/nginx/fastcgi_params;
        }
}

That location ~ \.php$ route will forward all PHP requests to your php-fpm instance. Of course, update your nginx config to match your socket name.

If you wanted to use this with the built-in PHP server you can do (assuming it's running on localhost:8000):

location ~ \.php$ {
    fastcgi_pass http://localhost:8000;
}

That way, you can have best of both worlds. (Remember though, the internal PHP server is not optimized or hardened. I suggest PHP-FPM.

2
On

PHP's built-in webserver is only meant for development and testing. It's a simple server that was never intended to handle real world loads.

An event-driven server would just add a bunch of needless complexity unless you really the features it adds.

Neither of them are intended for any serious or general purpose server. Unless you have a concrete reason to use a different stack, you should stick with Apache. It's stable, secure, configurable and does the job really well.