如何在 Ubuntu 22.04 上使用 Nginx 安装 Laravel PHP 框架

如何在 Ubuntu 22.04 上使用 Nginx 安装 Laravel PHP 框架

Laravel 是由 Taylor Otwell 创建的免费开源 PHP Web 框架。 它基于 Symfony 并遵循模型-视图-控制器架构模式。 它专为使用其重要且优雅的语法构建高端 Web 应用程序而设计。 它具有许多内置功能,使开发 Web 应用程序变得更容易、更快捷。 Laravel 在发布版本 3 后获得了更多的流行,其中包括 Artisan 命令行和支持数据库等方便的功能,并引入了称为捆绑包的打包系统。

本教程将向您展示如何在 Ubuntu 22.04 上使用 Nginx Web 服务器安装 Laravel PHP 框架。

先决条件

  • 运行 Ubuntu 22.04 的服务器。
  • 与您的服务器 IP 指向的有效域名。
  • 服务器上配置了 root 密码。

安装 LEMP 服务器

在开始之前,您需要在服务器上安装 Nginx Web 服务器、MariaDB 数据库系统、PHP 以及其他所需的依赖项。 您可以通过运行以下命令来安装所有这些:

apt install -y nginx mariadb-server php php-fpm php-common php-cli php-gd php-mysqlnd php-curl php-intl php-mbstring php-bcmath php-xml php-zip wget git

安装所有软件包后,使用以下命令验证 PHP 版本:

php -v

您应该看到以下输出:

PHP 8.1.2 (cli) (built: Apr  7 2022 17:46:26) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies

安装 PHP 作曲家

Composer 是 PHP 的依赖管理器,用于管理 PHP 依赖关系。 要安装 Composer,您需要将curl 包安装到您的服务器上。

apt install -y curl

接下来,使用以下命令安装 PHP Composer:

curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer

安装 Composer 后,您将获得以下输出:

All settings correct for using Composer
Downloading...

Composer (version 2.3.5) successfully installed to: /usr/bin/composer
Use it: php /usr/bin/composer

接下来,使用以下命令验证 Composer 版本:

composer --version

您应该得到以下输出:

Composer version 2.3.5 2022-04-13 16:43:00

在 Ubuntu 22.04 上安装 Laravel

首先,导航到 Nginx Web 根目录并使用 Composer 命令下载最新版本的 Laravel:

cd /var/www/html
composer create-project laravel/laravel laravel

您将得到以下输出:

55 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: laravel/sail
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: spatie/laravel-ignition
Package manifest generated successfully.
78 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
> @php artisan vendor:publish --tag=laravel-assets --ansi --force
No publishable resources for tag [laravel-assets].
Publishing complete.
> @php artisan key:generate --ansi
Application key set successfully.

接下来,将目录更改为 Laravel 并使用以下命令启动 Laravel:

cd laravel
php artisan serve --host 0.0.0.0 --port 8000

如果一切正常,您应该得到以下输出:

Starting Laravel development server: http://0.0.0.0:8000
[Sun May 22 08:17:45 2022] PHP 8.1.2 Development Server (http://0.0.0.0:8000) started

按 CTRL+C 停止 Laravel。 接下来,更改 Laravel 的所有权和权限:

chown -R www-data:www-data /var/www/html/laravel
chmod -R 0777 /var/www/html/laravel

为 Laravel 配置 Nginx

接下来,使用以下命令为 Laravel 创建 Nginx 虚拟主机配置文件:

nano /etc/nginx/conf.d/laravel.conf

添加以下行:

server {
    listen 80;
    server_name laravel.example.com;
    root /var/www/html/laravel/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php${
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

完成后保存并关闭文件,然后使用以下命令验证 Nginx 是否存在语法错误:

nginx -t

您将得到以下输出:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

接下来,重新启动 Nginx 和 PHP-FPM 服务以应用更改:

systemctl restart php8.1-fpm nginx

您还可以使用以下命令检查 Nginx 的状态:

systemctl status nginx

您应该看到以下输出:

? nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2022-05-22 08:19:20 UTC; 17s ago
       Docs: man:nginx(8)
    Process: 16865 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 16866 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 16867 (nginx)
      Tasks: 2 (limit: 2292)
     Memory: 2.6M
        CPU: 33ms
     CGroup: /system.slice/nginx.service
             ??16867 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             ??16868 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

May 22 08:19:20 ubuntu2204 systemd[1]: Starting A high performance web server and a reverse proxy server...
May 22 08:19:20 ubuntu2204 systemd[1]: Started A high performance web server and a reverse proxy server.

访问 Laravel Web 界面

至此,Laravel 已安装并配置 Nginx。 现在,您可以使用 URL http://laravel.example.com 访问 Laravel Web UI。 您应该在以下页面上看到 Laravel 仪表板:

使用 Let’s Encrypt 保护 Laravel

接下来,您需要安装 Certbot 客户端包来安装和管理 Let’s Encrypt SSL。

首先,使用以下命令安装 Certbot:

apt-get install certbot python3-certbot-nginx -y

安装完成后,运行以下命令在您的网站上安装 Let’s Encrypt SSL:

certbot --nginx -d laravel.example.com

您将被要求提供有效的电子邮件地址并接受服务条款,如下所示:

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(A)gree/(C)ancel: A

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: Y
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for laravel.example.com
Waiting for verification...
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/conf.d/laravel.conf

接下来,选择是否将 HTTP 流量重定向到 HTTPS,如下所示:

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2

输入 2 并按 Enter 键完成安装。 您应该看到以下输出:

Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/laravel.conf

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Congratulations! You have successfully enabled https://laravel.example.com

You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=laravel.example.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

IMPORTANT NOTES:
 - Congratulations! Your certificate and chain have been saved at:
   /etc/letsencrypt/live/laravel.example.com/fullchain.pem
   Your key file has been saved at:
   /etc/letsencrypt/live/laravel.example.com/privkey.pem
   Your cert will expire on 2022-08-22. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot again
   with the "certonly" option. To non-interactively renew *all* of
   your certificates, run "certbot renew"
 - Your account credentials have been saved in your Certbot
   configuration directory at /etc/letsencrypt. You should make a
   secure backup of this folder now. This configuration directory will
   also contain certificates and private keys obtained by Certbot so
   making regular backups of this folder is ideal.
 - If you like Certbot, please consider supporting our work by:

   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-le

 - We were unable to subscribe you the EFF mailing list because your
   e-mail address appears to be invalid. You can try again later by
   visiting https://act.eff.org.

结论

恭喜 您已经在 Ubuntu 22.04 上成功安装了 Laravel 和 Nginx。 您现在可以开始使用 Laravel 框架开发高性能 PHP 应用程序。 如果您有任何疑问,请随时问我。

资讯来源:由0x资讯编译自HOWTOFORGE,版权归作者所有,未经许可,不得转载

资讯来源:由a0资讯编译自THECOINREPUBLIC。版权归作者A0资讯所有,未经许可,不得转载

上一篇 2024年 5月 31日
下一篇 2024年 5月 31日

相关推荐