Nginx configuration for CodeIgniter

Nginx server configuration for CodeIgniter

Nginx server configuration for CodeIgniter

My Environment

  • System: CentOS 7
  • Web Server: Nginx v1.17.2
  • Application Server: php-fpm (based on PHP 7.2)
  • Web Framework: CodeIgniter v3.1.10

Deploy CodeIgniter project

First, Goto Official website and download CodeIgniter.

Then, create a folder named ci in the root directory of the nginx server and extract the CodeIgniter framework code into this directory. As follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[root@xyz html]# pwd
/usr/share/nginx/html
[root@xyz html]# tree -L 1 ci
ci
├── application
├── composer.json
├── contributing.md
├── index.php
├── license.txt
├── readme.rst
├── system
└── user_guide

3 directories, 5 files
[root@xyz html]#

The path /usr/share/nginx/html is the default static files path of nginx in my machine. You can use any other path actually. Just for simple, my goal to test the nginx server configuration.

Edit Nginx server configuration

Edit nginx default configuration file named default.conf, which is in /etc/nginx/conf.d.

I highly recommend you back up before changing the server configuration.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
server {
listen 80;
server_name localhost;

root /usr/share/nginx/html;
index index.html index.htm;

#access_log /var/log/nginx/host.access.log main;

location / {
try_files $uri $uri/ =404;
}

location /ci {
try_files $uri $uri/ /ci/index.php?/$request_uri;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

Test

My codeigniter folder is ci which is located in /usr/share/nginx/html/ci.

  • default controller

    http://yourdomain/ci/index.php/

    http://yourdomain/ci/index.php/welcome

    The effect of these two urls is the same. They all execute the index function of Welcome controller in Welcome.php file.

  • custom controller

    Create a file named Test.php in application/controllers directory and edit it as follow:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?php
    class Test extends CI_Controller {

    public function index() {
    echo "Test Controller index!";
    }

    public function hello() {
    echo "Hello CodeIgniter!";
    }
    }

    Restart service and test:

    http://yourdomain/ci/index.php/Test/

    http://yourdomain/ci/index.php/Test/hello

    All these work well.