Debian中CodeIgniter+nginx+MariaDB+phpMyAdmin配置
本文不讲述软件安装过程,记述本人在Debia中配置CodeIgniter时遇到的问题及解决方法,希望能够为有需要的人提供帮助。
一、Debian版本及所需的软件
Debian 9.8 stretch
PHP 7.0.3
Nginx 1.10.3-1
Php7.0-fpm
phpMyAdmin 4.8.5
MariaDB 10.1.37
CodeIgniter 3.1.10
二、nginx虚拟主机配置
首先说一下Debian中nginx配置文件的分布,/etc/nginx/nginx.conf中包含nginx的启动用户、进程ID和http等全局配置,具体server配置通过include /enc/nginx/sites-enabled/* 给出,site-enabled目录中只包含软链接,软链接指向site-available目录中相应的配置文件。site-available中有一个默认server配置,下面是我的site-enabled目录情况:

下面是codeigniter和php_my_admin两个文件(在sites-available目录中)的内容
codeigniter:
# Server configuration for CodeIgniter
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html/CodeIgniter-3.1.10;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name www.ci.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
location ~ \.php($|/) {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
php_my_amdin:
# Server configuration for phpMyAdmin
server {
listen 80;
root /var/www/html/phpMyAdmin;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name www.pma.com;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass PHP scripts to FastCGI server
location ~ \.php($|/) {
include snippets/fastcgi-php.conf;
# With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
}
注意:以上两个文件有以下两处不同
1、root
2、server_name
这样配置完成后,重新启动nginx会报错,因为两个server_name还没有设置,这个需要修改/etc/hosts文件,添加下面两行:
127.0.0.1 www.ci.com
127.0.0.1 www.pma.com
其中www.ci.com和www.pma.com是codeigniter和php_my_admin中server_name对应,这个两个可以根据自己的喜好设置,重启电脑就可以正常运行nginx了,可以通过www.ci.com和www.pma.com来访问相应的网站了。
注:在配置以上两个server时注意location ~ \.php($|/),默认只有$,要加上|/,否则可能会出现只能访问CI配置路由的页面,访问其他页面会出现404错误。
三、Codeigniter配置
配置文件config.php
$config['base_url'] = 'http://www.ci.com/';
这一句http://这个要有,不能少
$config['index_page'] = 'index.php';
在没有配置index.php省略时,这个要有,因为CodeIgniter中site_url会包含$config['base_url']和$config['index_page']两部分。
$config['uri_protocol'] = 'PATH_INFO';

浙公网安备 33010602011771号