메뉴 건너뛰기

컴' 게시판

본문시작

IT/개발
2013.08.24 16:32

Nginx Codeigniter 설정

조회 수 14 추천 수 0 댓글 0

Step 1 - Updating Apt with sources

As we are running Ubuntu (But this should work in Debian too, worth a try right?) we would want to use apt. So first let's get a fresh package list.

sudo apt-get update

Step 2 - Installing Nginx

Thanks to apt this is really easy.

sudo apt-get install nginx
That just too easy isn't it no need to compile! Test that you Nginx is indeed working by going to localhost:80, if not use some Google-fu.

Step 3 - Installing PHP5

Again thanks to Apt this is just as easy. This is the usual suite of modules that I install so customize to your own needs. 
 
sudo apt-get install php5-mysql php5-curl php5-gd php5-idn php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
Check if install by successful by issues the command 

php -v
If your Version of PHP is shown all is OK if not, check for any errors that may have occurred during install.

Step 4 - Installing PHP-FPM

Nginx is quite diffrent from Apache. First and foremost it's non-blocking while Apache blocks, but for our context Nginx does not run PHP itself. Nginx was built as a reverse proxy, so it simply forwards requests to the specified url / address. So to run PHP we need a container that would run the PHP and act as the host where Nginx is sending requests to. This is where PHP-FPM comes in. PHP runs as a container and runs the PHP files when requests come in. Great so lets install it! 
 
apt-get install php5-fpm
That should install FPM and start the service. Great so we are half-way there!

Step 5 - Configure Nginx to forward requests for PHP to PHP-FPM

Now we have a default installation of Nginx and PHP-FPM running. Wow! Let's configure Nginx to forward requests to PHP-FPM and in a way that would allow CodeIgniter to run just as it would have on Apache with Rewrite. I'm assuming your using Rewrite and no Query String Url. Edit Nginx's Site Configuration at /etc/nginx/sites-available/default and include the following configuration: 

server {
    server_name example.com;
    root path_to_your_public_root_dir;

    index index.html index.php index.htm;

    # set expiration of assets to MAX for caching
    location ~* .(ico|css|js|gif|jpe?g|png)(?[0-9]+)?$ {
        expires max;
        log_not_found off;
    }

    location / {
        # Check if a file exists, or route it to index.php.
        try_files $uri $uri/ /index.php;
    }

    location ~* .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+.php)(.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

}

Replace servernamerootfastcgipass with your enviroment values.

If you plan on hosting multiple CodeIgniter sites with this Instance, think about moving the common settings to a file and including the file.

So the  /etc/nginx/sites-available/default would contain:

server {
    server_name example.com;
    root path_to_your_public_root_dir;
    include /etc/nginx/ci_host;
}

and /etc/nginx/ci_host would contain:

index index.html index.php index.htm;

# set expiration of assets to MAX for caching
location ~* .(ico|css|js|gif|jpe?g|png)(?[0-9]+)?$ {
    expires max;
    log_not_found off;
}

location / {
    # Check if a file exists, or route it to index.php.
    try_files $uri $uri/ /index.php;
}

location ~* .php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+.php)(.*)$;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}

Step 6 - Restart and Rejoice !

After all the configration is done restart Nginx by using:

sudo service nginx restart

If you visit your configured site you it should show and behave just like to would have under Apache 2 with Rewrites. Good Job!

For a bit of a speed boost consider install PHP-APC too. The combination of Nginx, PHP-FPM and PHP APC is very fast!

Leave comments about common issues so we can help each other out!


  1. 25
    Sep 2013
    04:56

    한번쯤 확인해봐야 할 UI체...

    CategoryIT/개발 By꿈자 Reply0 Views17475 Votes0
    Read More
  2. 14
    Sep 2013
    14:57

    Mac 음성파일 녹음 시키기

    Category개인자료 By꿈자 Reply0 Views133 Votes0
    Read More
  3. 13
    Sep 2013
    07:27

    Nginx 설정 후 파일업로드...

    CategoryIT/개발 By꿈자 Reply0 Views16269 Votes0
    Read More
  4. 13
    Sep 2013
    07:25

    iPhone5 PSD 파일

    Category자료실 By꿈자 Reply0 Views21056 Votes0 file
    Read More
  5. 12
    Sep 2013
    01:00

    Git Rollback Undo

    CategoryIT/개발 By꿈자 Reply0 Views15672 Votes0
    Read More
  6. 11
    Sep 2013
    01:27

    CODEIGNITER ASSET HELPER ...

    CategoryIT/개발 By꿈자 Reply0 Views14772 Votes0 secret
    Read More
  7. 05
    Sep 2013
    01:56

    안과의사가 솔직히 까놓고 ...

    Category개인자료 By꿈자 Reply0 Views42678 Votes0
    Read More
  8. 27
    Aug 2013
    01:40

    Nginx 502 Bad Gateway

    CategoryIT/개발 By꿈자 Reply0 Views16335 Votes0
    Read More
  9. 26
    Aug 2013
    05:57

    ubuntu Nginx 1.4.x 업그레...

    CategoryIT/개발 By꿈자 Reply0 Views18 Votes0 file
    Read More
  10. 24
    Aug 2013
    16:32

    Nginx Codeigniter 설정

    CategoryIT/개발 By꿈자 Reply0 Views14 Votes0
    Read More
Board Pagination Prev 1 ... 8 9 10 11 12 ... 17 Next
/ 17