如何在 Debian 12 上安装 Laravel(英语:Laravel

Linux命令 Edge插件网 1年前 (2023-08-06) 280次浏览 已收录 0个评论

Laravel已经成为Web开发的领先PHP框架之一,提供了一种优雅而高效的方法来构建Web应用程序。

如何在 Debian 12 上安装 Laravel(英语:Laravel

在 Debian 12 书虫上安装 Laravel(英语:Laravel on Debian <> bookworm)

第 1 步。Debian 12 上安装任何软件之前,建议更新软件包列表以确保您拥有最新版本的软件。您可以通过在终端中运行以下命令来执行此操作:

<span class="pln">sudo apt update
sudo apt upgrade</span>

Step 2. Installing Apache.

Now that the repository is up to date, you can proceed with the installation of Apache:

<span class="pln">sudo apt install apache2</span>

安装完成后,您可以检查 Apache 服务状态以确保其正常运行:

<span class="pln">sudo systemctl status apache2</span>

如果 Apache 成功运行,您将在输出中看到“活动(正在运行)”消息。您还可以通过在 Web 浏览器中访问服务器的 IP 地址或域名进行验证。如果一切设置正确,您应该会看到默认的 Apache 登录页面。

第 3 步。安装 PHP。

现在您的系统是最新的,是时候在 Debian 12 上安装 PHP 了。首先,通过在终端中执行以下命令,将 Ondřej Surý PHP 仓库添加到 Debian 12 中:

<span class="pln">wget </span><span class="pun">-</span><span class="pln">O </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">trusted</span><span class="pun">.</span><span class="pln">gpg</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">gpg https</span><span class="pun">:</span><span class="com">//packages.sury.org/php/apt.gpg</span><span class="pln">
echo </span><span class="str">"deb https://packages.sury.org/php/ $(lsb_release -sc) main"</span> <span class="pun">|</span><span class="pln"> sudo tee </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">apt</span><span class="pun">/</span><span class="pln">sources</span><span class="pun">.</span><span class="pln">list</span><span class="pun">.</span><span class="pln">d</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">list</span>

这些命令将使您的系统能够从存储库中获取 PHP 包。

接下来,再次更新软件包列表,并通过运行以下命令安装 PHP 和必要的扩展:

<span class="pln">sudo apt update
sudo apt install php8</span><span class="pun">.</span><span class="lit">2</span>

要安装Laravel,您必须启用一些PHP扩展,例如fileinfo,mbstring和OpenSSL。您可以通过 php.ini 文件启用这些扩展;

<span class="pln">sudo nano </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="lit">8.2</span><span class="pun">/</span><span class="pln">apache2</span><span class="pun">/</span><span class="pln">php</span><span class="pun">.</span><span class="pln">ini</span>

取消注释以下行以启用扩展文件信息、mbstring 和 openssl。

<span class="pln">extension</span><span class="pun">=</span><span class="pln">fileinfo
extension</span><span class="pun">=</span><span class="pln">mbstring
extension</span><span class="pun">=</span><span class="pln">openssl</span>

保存文件并重新启动 Apache2 服务并应用更改:

<span class="pln">sudo systemctl restart apache2</span>

第 4 步。在 Debian 12 上安装 MariaDB。

要安装 MariaDB 的最新稳定版本,我们将官方 MariaDB 存储库添加到我们的系统中:

<span class="pln">wget https</span><span class="pun">:</span><span class="com">//downloads.mariadb.com/MariaDB/mariadb_repo_setup</span>

使下载的文件可执行:

<span class="pln">chmod </span><span class="pun">+</span><span class="pln">x mariadb_repo_setup</span>

运行安装文件:

<span class="pln">sudo </span><span class="pun">./</span><span class="pln">mariadb_repo_setup</span>

安装脚本将配置存储库并导入存储库密钥。

添加 MariaDB 存储库后,我们需要再次更新存储库并安装 MariaDB 服务器包,执行以下命令:

<span class="pln">sudo apt install mariadb</span><span class="pun">-</span><span class="pln">server</span>

在安装过程中,系统将提示您输入密码。选择一个强密码并确保其安全。

安装完成后,使用以下命令启动 MariaDB 服务:

<span class="pln">sudo systemctl start mariadb</span>

为了改进默认安全设置,MariaDB 提供了一个安全脚本。运行脚本并按照提示操作以增强安装的安全性:

<span class="pln">sudo mysql_secure_installation</span>

像这样配置它:

<span class="pun">-</span> <span class="typ">Set</span><span class="pln"> root password</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y
</span><span class="pun">-</span> <span class="typ">Remove</span><span class="pln"> anonymous users</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y
</span><span class="pun">-</span> <span class="typ">Disallow</span><span class="pln"> root login remotely</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y
</span><span class="pun">-</span> <span class="typ">Remove</span><span class="pln"> test database </span><span class="kwd">and</span><span class="pln"> access to it</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y
</span><span class="pun">-</span> <span class="typ">Reload</span><span class="pln"> privilege tables now</span><span class="pun">?</span> <span class="pun">[</span><span class="pln">Y</span><span class="pun">/</span><span class="pln">n</span><span class="pun">]</span><span class="pln"> y</span>

接下来,运行以下查询以创建新的数据库 lavarels、用户 lavarels 和密码您的强密码。请务必在以下查询中更改密码。

<span class="pln">CREATE DATABASE laravels</span><span class="pun">;</span><span class="pln">
CREATE USER laravels@localhost IDENTIFIED BY </span><span class="str">'your-strong-password'</span><span class="pun">;</span><span class="pln">
GRANT ALL PRIVILEGES ON laravels</span><span class="pun">.*</span><span class="pln"> TO laravels@localhost</span><span class="pun">;</span><span class="pln">
FLUSH PRIVILEGES</span><span class="pun">;</span>

之后,运行以下查询以确保用户 lavarels 可以访问数据库 lavarels

<span class="pln">SHOW GRANTS FOR laravels@localhost</span><span class="pun">;</span>

第5步。正在安装作曲家。

现在,是时候下载 Composer 安装脚本并验证其完整性了。执行以下命令:

<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>

现在我们有了 Composer 安装脚本,我们可以将可执行文件移动到系统上全局可访问的位置。运行以下命令:

<span class="pln">sudo php composer</span><span class="pun">-</span><span class="pln">setup</span><span class="pun">.</span><span class="pln">php </span><span class="pun">--</span><span class="pln">install</span><span class="pun">-</span><span class="pln">dir</span><span class="pun">=</span><span class="str">/usr/</span><span class="kwd">local</span><span class="pun">/</span><span class="pln">bin </span><span class="pun">--</span><span class="pln">filename</span><span class="pun">=</span><span class="pln">composer</span>

要验证 Composer 是否已成功安装在您的 Debian 系统上,请运行以下命令:

<span class="pln">composer </span><span class="pun">--</span><span class="pln">version</span>

如果安装成功,您将看到屏幕上显示的 Composer 版本信息,指示 Composer 已准备就绪,可供使用。

第 6 步。创建一个拉拉维尔项目。

现在一切都设置好了,是时候创建你的Laravel项目了。运行以下命令:

<span class="pln">sudo mkdir </span><span class="pun">-</span><span class="pln">p </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">cache</span><span class="pun">,.</span><span class="pln">config</span><span class="pun">,</span><span class="pln">newproject</span><span class="pun">}</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">cache</span><span class="pun">,.</span><span class="pln">config</span><span class="pun">,</span><span class="pln">newproject</span><span class="pun">}</span>

接下来,移动到目录并运行下面的作曲家命令以创建第一个Laravel项目。您的第一个Laravel项目存储在目录:/var/www/newproject//var/www/newproject/

<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">newproject</span><span class="pun">/</span><span class="pln">
sudo </span><span class="pun">-</span><span class="pln">u www</span><span class="pun">-</span><span class="pln">data composer create</span><span class="pun">-</span><span class="pln">project laravel</span><span class="pun">/</span><span class="pln">laravel </span><span class="pun">.</span>

创建Laravel项目后,接下来您将通过配置文件为项目设置数据库。该文件用作Laravel项目的基本配置:.env.env

<span class="pln">nano </span><span class="pun">.</span><span class="pln">env</span>

使用Laravel项目的本地域名更改参数:APP_URL

<span class="pln">APP_URL</span><span class="pun">=</span><span class="pln">http</span><span class="pun">:</span><span class="com">//newproject.local</span>

现在将数据库配置DB_CONNECTION更改为MySQL,并使用您的详细信息MariaDB数据库和用户更改DB_DATABASE,DB_USERNAME和DB_PASSWORD的详细信息。

<span class="pln">DB_CONNECTION</span><span class="pun">=</span><span class="pln">mysql
DB_HOST</span><span class="pun">=</span><span class="lit">127.0</span><span class="pun">.</span><span class="lit">0.1</span><span class="pln">
DB_PORT</span><span class="pun">=</span><span class="lit">3306</span><span class="pln">
DB_DATABASE</span><span class="pun">=</span><span class="pln">laravels
DB_USERNAME</span><span class="pun">=</span><span class="pln">laravels
DB_PASSWORD</span><span class="pun">=</span><span class="pln">ypur</span><span class="pun">-</span><span class="pln">strong</span><span class="pun">-</span><span class="pln">password</span>

保存文件,然后运行以下命令以迁移数据库:

<span class="pln">sudo </span><span class="pun">-</span><span class="pln">u www</span><span class="pun">-</span><span class="pln">data php artisan migrate</span>

步骤 7.配置阿帕奇虚拟主机。

现在创建 Apache2 虚拟主机配置:/etc/apache2/sites-available/laravel.conf

<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">laravel</span><span class="pun">.</span><span class="pln">conf</span>

添加以下文件:

<span class="tag"><VirtualHost</span><span class="pln"> *:80</span><span class="tag">></span><span class="pln">

    ServerAdmin admin@newproject.local
    ServerName testapp.local
    DocumentRoot /var/www/newproject/public

    </span><span class="tag"><Directory</span> <span class="tag">/></span><span class="pln">
            Options FollowSymLinks
            AllowOverride None
    </span><span class="tag"></Directory></span><span class="pln">
    </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">newproject</span><span class="tag">></span><span class="pln">
            AllowOverride All
    </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>

保存文件,然后通过执行以下命令并应用更改来重新启动 apache2 服务:

<span class="pln">sudo a2ensite laravel</span><span class="pun">.</span><span class="pln">conf
sudo a2enmod rewrite
sudo systemctl restart apache2</span>

第8步。访问拉拉维尔网络界面。

成功安装后,打开您的网络浏览器并使用 URL 访问 Laravel网络界面 .您将被重定向到以下页面:http://newproject.local

如何在 Debian 12 上安装 Laravel(英语:Laravel

第9步。故障排除提示。

  • 如果遇到任何与 PHP 扩展相关的问题,请验证是否安装了正确的扩展。用于检查已启用的扩展。php -m
  • 如果 Composer 安装失败,请仔细检查 PHP 版本并确保安装了必要的依赖项。
  • 对于与数据库相关的错误,请查看 Laravel 项目文件中的数据库配置。.env

感谢您使用本教程在 Debian 12 Bookworm 上安装 Laravel PHP 框架。如需其他帮助或有用信息,我们建议您查看Laravel官方网站


Edge插件网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何在 Debian 12 上安装 Laravel(英语:Laravel
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址