CodeIgniter是一个功能强大的PHP框架,是寻求构建功能丰富和动态Web应用程序的开发人员的首选。
在 Debian 12 书虫上安装 CodeIgniter
第 1 步。在安装 CodeIgniter 之前,您需要确保您的 Debian 12 系统是最新的,并且安装了必要的软件包。您可以通过运行以下命令来执行此操作:
<span class="pln">sudo apt update</span>
此命令将刷新存储库,允许您安装最新版本的软件包。
第 2 步。设置灯堆栈。
按照以下步骤安装和配置 Apache、MySQL 和 PHP:
- 安装阿帕奇:
<span class="pln">sudo apt install apache2</span>
- 安装 MySQL:
<span class="pln">sudo apt install mysql</span><span class="pun">-</span><span class="pln">server</span>
- 安装 PHP:
<span class="pln">sudo apt</span><span class="pun">-</span><span class="kwd">get</span><span class="pln"> install php libapache2</span><span class="pun">-</span><span class="pln">mod</span><span class="pun">-</span><span class="pln">php php</span><span class="pun">-</span><span class="pln">mysql</span>
第 3 步。正在安装作曲家。
Composer是一个强大的工具,可以管理PHP依赖关系,并有助于CodeIgniter的平稳运行。让我们启动并运行它:
<span class="pln">php </span><span class="pun">-</span><span class="pln">r </span><span class="str">"copy('https://getcomposer.org/installer', 'composer-setup.php');"</span><span class="pln"> php </span><span class="pun">-</span><span class="pln">r </span><span class="str">"if (hash_file('sha384', 'composer-setup.php') === 'baf1608c33254d00611ac1705c1d9958c817a1a33bce370c0595974b342601bd80b92a3f46067da89e3b06bff421f182') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"</span><span class="pln"> php composer</span><span class="pun">-</span><span class="pln">setup</span><span class="pun">.</span><span class="pln">php php </span><span class="pun">-</span><span class="pln">r </span><span class="str">"unlink('composer-setup.php');"</span><span class="pln"> sudo mv composer</span><span class="pun">.</span><span class="pln">phar </span><span class="pun">/</span><span class="pln">usr</span><span class="pun">/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">composer</span>
要验证安装是否成功,请运行:
<span class="pln">composer </span><span class="pun">--</span><span class="pln">version</span>
第 4 步。下载代码点火器:
现在我们已经安装了 Composer,让我们继续下载最新版本的 CodeIgniter:
<span class="pln">cd </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html mkdir my_codeigniter_project cd my_codeigniter_project composer create</span><span class="pun">-</span><span class="pln">project codeigniter4</span><span class="pun">/</span><span class="pln">appstarter </span><span class="pun">.</span>
第5步。配置阿帕奇。
要从 Debian 系统提供 CodeIgniter,您需要配置 Apache 并设置虚拟主机:
<span class="pln">sudo nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">/</span><span class="pln">sites</span><span class="pun">-</span><span class="pln">available</span><span class="pun">/</span><span class="pln">my_codeigniter_project</span><span class="pun">.</span><span class="pln">conf</span>
添加以下配置(将“”替换为项目名称和目录):my_codeigniter_project
<span class="tag"><VirtualHost</span><span class="pln"> *:80</span><span class="tag">></span><span class="pln"> ServerAdmin webmaster@localhost DocumentRoot /var/www/html/my_codeigniter_project/public </span><span class="tag"><Directory</span> <span class="pun">/</span><span class="atn">var</span><span class="pun">/</span><span class="atn">www</span><span class="pun">/</span><span class="atn">html</span><span class="pun">/</span><span class="atn">my_codeigniter_project</span><span class="pun">/</span><span class="atn">public</span><span class="tag">></span><span class="pln"> Options Indexes FollowSymLinks AllowOverride All Require all granted </span><span class="tag"></Directory></span><span class="pln"> ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </span><span class="tag"></VirtualHost></span>
启用虚拟主机:
<span class="pln">sudo a2ensite my_codeigniter_project</span><span class="pun">.</span><span class="pln">conf sudo a2enmod rewrite</span>
重新启动 Apache Web 服务器,以便进行更改:
<span class="pln">sudo systemctl restart apache2</span>
第 6 步。数据库设置:
接下来,我们将设置一个 MySQL 数据库以与 CodeIgniter 无缝协作:
<span class="pln">mysql </span><span class="pun">-</span><span class="pln">u root </span><span class="pun">-</span><span class="pln">p</span>
创建新数据库和用户:
<span class="pln">CREATE DATABASE my_codeigniter_db</span><span class="pun">;</span><span class="pln"> CREATE USER </span><span class="str">'ci_user'</span><span class="pun">@</span><span class="str">'localhost'</span><span class="pln"> IDENTIFIED BY </span><span class="str">'your_strong_password'</span><span class="pun">;</span><span class="pln"> GRANT ALL PRIVILEGES ON my_codeigniter_db</span><span class="pun">.*</span><span class="pln"> TO </span><span class="str">'ci_user'</span><span class="pun">@</span><span class="str">'localhost'</span><span class="pun">;</span><span class="pln"> FLUSH PRIVILEGES</span><span class="pun">;</span><span class="pln"> EXIT</span><span class="pun">;</span>
步骤 7.代码点火器配置:
让我们配置 CodeIgniter 以确保它与 Apache 和您的 MySQL 数据库有效通信:
<span class="pln">nano </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="pun">/</span><span class="pln">my_codeigniter_project</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span><span class="pln">config</span><span class="pun">/</span><span class="typ">App</span><span class="pun">.</span><span class="pln">php</span>
找到并修改变量以匹配虚拟主机配置:$baseURL
<span class="kwd">public</span><span class="pln"> $baseURL </span><span class="pun">=</span> <span class="str">'http://your_domain_or_ip/'</span><span class="pun">;</span>
配置数据库:
<span class="pln">nano </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="pun">/</span><span class="pln">my_codeigniter_project</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span><span class="pln">config</span><span class="pun">/</span><span class="typ">Database</span><span class="pun">.</span><span class="pln">php</span>
使用数据库详细信息更新阵列:$default
<span class="kwd">public</span><span class="pln"> $default </span><span class="pun">=</span> <span class="pun">[</span> <span class="str">'DSN'</span> <span class="pun">=></span> <span class="str">''</span><span class="pun">,</span> <span class="str">'hostname'</span> <span class="pun">=></span> <span class="str">'localhost'</span><span class="pun">,</span> <span class="str">'username'</span> <span class="pun">=></span> <span class="str">'ci_user'</span><span class="pun">,</span> <span class="str">'password'</span> <span class="pun">=></span> <span class="str">'your_strong_password'</span><span class="pun">,</span> <span class="str">'database'</span> <span class="pun">=></span> <span class="str">'my_codeigniter_db'</span><span class="pun">,</span> <span class="str">'DBDriver'</span> <span class="pun">=></span> <span class="str">'MySQLi'</span><span class="pun">,</span> <span class="str">'DBPrefix'</span> <span class="pun">=></span> <span class="str">''</span><span class="pun">,</span> <span class="str">'pConnect'</span> <span class="pun">=></span> <span class="kwd">false</span><span class="pun">,</span> <span class="str">'DBDebug'</span> <span class="pun">=></span> <span class="pun">(</span><span class="pln">ENVIRONMENT </span><span class="pun">!==</span> <span class="str">'production'</span><span class="pun">),</span> <span class="str">'cacheOn'</span> <span class="pun">=></span> <span class="kwd">false</span><span class="pun">,</span> <span class="str">'cacheDir'</span> <span class="pun">=></span> <span class="str">''</span><span class="pun">,</span> <span class="str">'charset'</span> <span class="pun">=></span> <span class="str">'utf8'</span><span class="pun">,</span> <span class="str">'DBCollat'</span> <span class="pun">=></span> <span class="str">'utf8_general_ci'</span><span class="pun">,</span> <span class="str">'swapPre'</span> <span class="pun">=></span> <span class="str">''</span><span class="pun">,</span> <span class="str">'encrypt'</span> <span class="pun">=></span> <span class="kwd">false</span><span class="pun">,</span> <span class="str">'compress'</span> <span class="pun">=></span> <span class="kwd">false</span><span class="pun">,</span> <span class="str">'strictOn'</span> <span class="pun">=></span> <span class="kwd">false</span><span class="pun">,</span> <span class="str">'failover'</span> <span class="pun">=></span> <span class="pun">[],</span> <span class="str">'port'</span> <span class="pun">=></span> <span class="lit">3306</span><span class="pun">,</span> <span class="pun">];</span>
第8步。测试安装。
配置就绪后,是时候确保 CodeIgniter 正常工作了:
<span class="pln">nano </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="pun">/</span><span class="pln">my_codeigniter_project</span><span class="pun">/</span><span class="pln">app</span><span class="pun">/</span><span class="typ">Controllers</span><span class="pun">/</span><span class="typ">Test</span><span class="pun">.</span><span class="pln">php</span>
添加一个简单的控制器代码:
<span class="pun"><?</span><span class="pln">php </span><span class="kwd">namespace</span> <span class="typ">App</span><span class="pln">\Controllers</span><span class="pun">;</span> <span class="kwd">class</span> <span class="typ">Test</span> <span class="kwd">extends</span> <span class="typ">BaseController</span> <span class="pun">{</span> <span class="kwd">public</span> <span class="kwd">function</span><span class="pln"> index</span><span class="pun">()</span> <span class="pun">{</span><span class="pln"> echo </span><span class="str">'CodeIgniter installation successful!'</span><span class="pun">;</span> <span class="pun">}</span> <span class="pun">}</span>
通过 Web 浏览器访问控制器:
现在我们访问以查看显示的消息。http://your_domain_or_ip/test
第9步。常见故障排除。
在安装过程中,您可能会遇到一些常见问题和错误。以下是一些故障排除提示:
- Mod_Rewrite未启用。
确保在 Apache 上启用了 mod_rewrite 以支持干净的 URL。使用以下命令启用它:
<span class="pln">sudo a2enmod rewrite sudo systemctl restart apache2</span>
- 权限问题:
确保必要的文件夹具有适当的权限。使用以下命令授予必要的访问权限:
<span class="pln">sudo chown </span><span class="pun">-</span><span class="pln">R www</span><span class="pun">-</span><span class="pln">data</span><span class="pun">:</span><span class="pln">www</span><span class="pun">-</span><span class="pln">data </span><span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="pun">/</span><span class="pln">my_codeigniter_project sudo chmod </span><span class="pun">-</span><span class="pln">R </span><span class="lit">755</span> <span class="pun">/</span><span class="kwd">var</span><span class="pun">/</span><span class="pln">www</span><span class="pun">/</span><span class="pln">html</span><span class="pun">/</span><span class="pln">my_codeigniter_project</span>
- 数据库连接错误。
仔细检查数据库配置,并确保提供了正确的用户名、密码和数据库名称。Database.php
感谢您使用本教程在 Debian 12 书虫上安装最新版本的 CodeIgniter。有关其他帮助或有用信息,我们建议您查看 CodeIgniter 官方网站。