[How to] Install silex with nginx in centos 6.4

nginx_200

Here is starting point before you start this tutorial :

And, here is the step :

  1. Download silex

    cd /var/www/ && wget http://silex.sensiolabs.org/get/silex_fat.zip && unzip silex_fat.zip
    
  2. Try it with PHP builtin web server

    check php version

    php --version
    PHP 5.4.23 (cli) (built: Dec 11 2013 06:48:07)
    Copyright (c) 1997-2013 The PHP Group
    Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend
    

    Edit index.php file

    vim /var/www/silex/web/index.php
    
    <?php
    
    require_once __DIR__.'/../vendor/autoload.php';
    $filename = __DIR__.preg_replace('#(\?.*)$#', '', $_SERVER['REQUEST_URI']);
    if (php_sapi_name() === 'cli-server' && is_file($filename)) {
        return false;
    }
    
    $app = new Silex\Application();
    
    $app->get('/hello', function() {
        return 'Hello!';
    });
    
    $app->run();
    
  3. Start the server

    php -S localhost:8080 -t web web/index.php
    

    If you try this with server, try to change the localhost with your server ip

  4. Check the browser if it works

    curl localhost:8080/hello
    

    You should see the hello message

  5. Nginx Configuration with virtual host

    Check nginx.conf

    Make sure nginx.conf accept all setting (uncomment “include /etc/nginx/conf.d/*.conf”) in /etc/nginx/conf.d/*.conf.

    Create vhost file

    vim /etc/nginx/conf.d/virtual.conf

    server {
        listen 8090;
        server_name silex;
        root /var/www/silex/web;
        index index.php;
    
        #site root is redirected to the app boot script
        location = / {
            try_files @site @site;
        }
    
        #all other locations try other files first and go to our front controller if none of them exists
        location / {
            try_files $uri $uri/ @site;
        }
    
        #return 404 for all php files as we do have a front controller
        location ~ \.php$ {
            return 404;
        }
    
        location @site {
            fastcgi_pass   unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME $document_root/index.php;
            #uncomment when running via https
            #fastcgi_param HTTPS on;
        }
    }
    

    Restart Nginx Server

    /etc/init.d/nginx restart
    
  6. Test it in browser or curl

    curl http://127.0.0.1:8090/hello
    

    Selamat, now You learn how to install silex in Nginx

  7. References

    1. Silex Documentation
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s