4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

NginxでCodeIgniter3を動かすための設定

Last updated at Posted at 2019-02-22

はじめに

CodeIgniter3を利用して実装したものを今までApacheで運用していましたが、Nginxについに移行することになりました。
しかし、ApacheばかりでNginxは数えられる時間しか触っていなかったため、知識不足でCodeIgniter3を動かそうとした際、ハマってしまったため、スマートな設定方法ではないですが、次ハマらないために(ほぼ自分のために)メモを残すことにしました。

環境

動かす環境は、以下の通りです。

  • CentOS 7.3
  • Nginx/1.14.2
  • PHP 5.6 ←まだ5.6です。。
  • CodeIgniter3

前提条件

前提条件は、以下のとおりです。

  • index.phpと同一階層に.htaccessは追加済み。
$ vi .htaccess
1| RewriteEngine On
2| RewriteCond %{REQUEST_FILENAME} !-f
3| RewriteCond %{REQUEST_FILENAME} !-d
4| RewriteRule ^(.*)$ index.php/$1 [L]
  • CodeIgniterの「config.php」にある「$config[‘index_page’]」の設定値から『index.php』を消す。
$ vi config.php
1| $config[‘index_page’] =;

設定ファイルの更新

Nginxのdefault.confを更新します。

$ vi /etc/nginx/conf.d/default.conf
 1| server {
 2|     listen       80;
 3|     server_name  localhost;
 4| 
 5|     #charset koi8-r;
 6|     #access_log  /var/log/nginx/host.access.log  main;
 7| 
 8|     error_log /var/log/nginx/error.log debug;
 9|     rewrite_log on;
10| 
11|     location / {
12|         root   /var/www/html;
13|         index  index.html index.htm  index.php;
14| 
15|         if (-f $request_filename) {
16|             expires 30d;
17|             break;
18|         }
19|         if (!-e $request_filename) {
20|             rewrite ^(.+)$ /index.php?$1 last;
21|         }
22| 
23|         try_files $uri $uri/ /index.php;
24|     }
25| 
26|     location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|json|woff2)$ {
27|         root /var/www/html;
28|         access_log off;
29|         expires 30d;
30|     }
31| 
32|     #error_page  404              /404.html;
33| 
34|     # redirect server error pages to the static page /50x.html
35|     #
36|     error_page   500 502 503 504  /50x.html;
37|     location = /50x.html {
38|         root   /usr/share/nginx/html;
39|     }
40| 
41|     # proxy the PHP scripts to Apache listening on 127.0.0.1:80
42|     #
43|     #location ~ \.php$ {
44|     #    proxy_pass   http://127.0.0.1;
45|     #}
46| 
47|     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
48|     #
49|     location ~ \.php$ {
50|         root           /var/www/html;
51|         #fastcgi_pass   127.0.0.1:9000;
52|         fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
53|         fastcgi_index  index.php;
54|         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
55|         include        fastcgi_params;
56|     }
57| 
58|     location ~ /index.php/(.*)$ {
59|         root           /var/www/html;
60|         fastcgi_split_path_info ^(.+\.php)(/.+)$;
61|         fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
62|         fastcgi_index  index.php;
63|         fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
64|         include        fastcgi_params;
65|     }
66| 
67|     location /project/subproject {
68|         root           /var/www/html;
69|         index  index.html index.htm  index.php;
70|         try_files $uri    $uri/    /project/subproject/index.php$is_args$args;
71|     }
72| 
73|     # deny access to .htaccess files, if Apache's document root
74|     # concurs with nginx's one
75|     #
76|     location ~ /\.ht {
77|         deny  all;
78|     }
79| }

最後に

「root」がすべてのlocationの中に記載されているのが気になっていますが、外出しで1つ書いてlocation内を省略するとCodeIgniterがそもそも動かず、泣く泣くすべてのlocationの中に記載しています。
もっとNginxを勉強しようと思いました。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?