CodeIgniter项目部署到服务器后网站无法正常渲染怎么解决?
问题解决步骤
访问站点直接返回PHP源码的核心原因是生产环境Web服务器未正确配置PHP解析,配合CodeIgniter项目的专属配置调整即可解决,具体操作如下:
1. 优先排查Web服务器PHP解析配置
- 若使用Apache服务器:
- 确认已安装对应版本的PHP模块,且Apache配置文件中已开启
php_module,未被注释 - 确认Apache配置中添加了PHP文件类型解析规则:
AddType application/x-httpd-php .php - 确认站点根目录的执行权限配置正确,允许PHP文件执行
- 确认已安装对应版本的PHP模块,且Apache配置文件中已开启
- 若使用Nginx服务器:
- 确认站点配置文件中已正确配置PHP-FPM转发规则,所有
.php后缀的请求都会转发到PHP处理进程 - 确认PHP-FPM服务处于正常运行状态
- 确认站点配置文件中已正确配置PHP-FPM转发规则,所有
- 验证方式:在站点根目录新建
info.php文件,写入以下代码,访问该文件若能正常显示PHP信息页面则解析配置正常,完成后删除该测试文件
<?php phpinfo(); ?>
2. 修正CodeIgniter入口文件缺失配置
你当前的index.php入口文件删除冗余代码后缺失了$application_folder、BASEPATH的核心定义代码,需要补全缺失部分,路径按照你实际的项目文件夹结构调整即可:
/* *--------------------------------------------------------------- * APPLICATION DIRECTORY NAME *--------------------------------------------------------------- * * If you want this front controller to use a different "application" * directory than the default one you can set its name here. The directory * can also be renamed or relocated anywhere on your server. If you do, * use an absolute (full) server path. * * NO TRAILING SLASH! */ $application_folder = 'application'; /* *--------------------------------------------------------------- * VIEW DIRECTORY NAME *--------------------------------------------------------------- * * If you want to move the view directory out of the application * directory, set the path to it here. The directory can be renamed * and relocated anywhere on your server. If blank, it will default * to the standard location inside your application directory. * If you do move this, use an absolute (full) server path. * * NO TRAILING SLASH! */ $view_folder = ''; /* * --------------------------------------------------------------- * Resolve the system path for increased reliability * --------------------------------------------------------------- */ // Set the current directory correctly for CLI requests if (defined('STDIN')) { chdir(dirname(__FILE__)); } if (($_temp = realpath($system_path)) !== FALSE) { $system_path = $_temp.DIRECTORY_SEPARATOR; } else { // Ensure there's a trailing slash $system_path = strtr( rtrim($system_path, '/\\'), '/\\', DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR ).DIRECTORY_SEPARATOR; } // Is the system path correct? if ( ! is_dir($system_path)) { header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); echo 'Your system folder path does not appear to be set correctly. Please open the following file and correct this: '.pathinfo(__FILE__, PATHINFO_BASENAME); exit(3); // EXIT_CONFIG } /* * ------------------------------------------------------------------- * Now that we know the path, set the main path constants * ------------------------------------------------------------------- */ // The name of THIS file define('SELF', pathinfo(__FILE__, PATHINFO_BASENAME)); // Path to the system directory define('BASEPATH', $system_path); // Path to the front controller (this file) directory define('FCPATH', dirname(__FILE__).DIRECTORY_SEPARATOR); // Name of the "system" directory define('SYSDIR', basename(BASEPATH)); /* * --------------------------------------------------------------- * Resolve the application path for increased reliability * --------------------------------------------------------------- */ if (is_dir($application_folder)) { if (($_temp = realpath($application_folder)) !== FALSE) { $application_folder = $_temp; } else { $application_folder = strtr( rtrim($application_folder, '/\\'), '/\\', DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR ); } } elseif (is_dir(BASEPATH.$application_folder.DIRECTORY_SEPARATOR)) { $application_folder = BASEPATH.strtr( trim($application_folder, '/\\'), '/\\', DIRECTORY_SEPARATOR.DIRECTORY_SEPARATOR ); } else { header('HTTP/1.1 503 Service Unavailable.', TRUE, 503); echo 'Your application folder path does not appear to be set correctly. Please open the following file and correct this: '.SELF; exit(3); // EXIT_CONFIG }
3. 调整.htaccess适配生产环境
你当前的.htaccess规则可正常使用,若后续出现路径404问题可将重写规则调整为:
RewriteEngine On RewriteBase / DirectoryIndex index.php RewriteCond $1 !^(index\.php|resources|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
4. 生产环境参数调整
调试完成后将index.php中的环境变量设置调整为生产环境,避免泄露敏感信息:
define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'production');
内容的提问来源于stack exchange,提问作者Sagar
相关产品推荐
相关产品推荐

