在EC2上安装Laravel(PHP-FPM)的备忘录

前提 (paraphrased in Chinese) : 前提 tí)

在云计算服务中使用EC2时

    • publicサブネット:webサーバー(nginx)

 

    privateサブネット:appサーバー(php-fpm)

以两台设备组成的构想。

为了安装时需要互联网连接,私有子网的EC2通过NAT网关进行互联网连接。

目标

スクリーンショット 2022-11-11 10.11.46.png

安装Nginx

我在这边进行中

 

安装php和php-fpm。

更新yum的软件包

$ sudo yum update

确认是否已安装了 Amazon Linux Extras 存储库。

$which amazon-linux-extras
/usr/bin/amazon-linux-extras

如果没有的话 dehuà)

$sudo yum install -y amazon-linux-extras

确认可安装的php版本。

$ amazon-linux-extras | grep php
 42  php7.4                   available    [ =stable ]
 51  php8.0                   available    [ =stable ]

我决定安装最新的PHP 8.0版本。

php8.0をインストールすると関連してインストールされるパッケージの確認

$ amazon-linux-extras info php8.0
php8.0 recommends php-cli                    # yum install php-cli
php8.0 recommends php-pdo                    # yum install php-pdo
php8.0 recommends php-fpm                    # yum install php-fpm
php8.0 recommends php-mysqlnd                # yum install php-mysqlnd

安装PHP

$ sudo amazon-linux-extras install php8.0 -y

//確認
$ php -v
PHP 8.0.20 (cli) (built: Jun 23 2022 20:34:07) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.20, Copyright (c) Zend Technologies

$ php-fpm -v
PHP 8.0.20 (fpm-fcgi) (built: Jun 23 2022 20:36:04)
Copyright (c) The PHP Group
Zend Engine v4.0.20, Copyright (c) Zend Technologies
//php-fpm自動起動
$ sudo systemctl enable php-fpm

//php-fpm起動・停止・再起動
$ sudo systemctl start php-fpm
$ sudo systemctl stop php-fpm
$ sudo systemctl restart php-fpm

确认 PHP 扩展包。

$ sudo yum list php*
読み込んだプラグイン:extras_suggestions, langpacks, priorities, update-motd
インストール済みパッケージ
php-cli.x86_64                                                     8.0.20-1.amzn2                                           @amzn2extra-php8.0
php-common.x86_64                                                  8.0.20-1.amzn2                                           @amzn2extra-php8.0
php-fpm.x86_64                                                     8.0.20-1.amzn2                                           @amzn2extra-php8.0
php-mysqlnd.x86_64                                                 8.0.20-1.amzn2                                           @amzn2extra-php8.0
php-pdo.x86_64                                                     8.0.20-1.amzn2                                           @amzn2extra-php8.0
利用可能なパッケージ
php.x86_64                                                         8.0.20-1.amzn2                                           amzn2extra-php8.0
php-bcmath.x86_64                                                  8.0.20-1.amzn2                                           amzn2extra-php8.0
php-dba.x86_64                                                     8.0.20-1.amzn2                                           amzn2extra-php8.0
php-dbg.x86_64                                                     8.0.20-1.amzn2                                           amzn2extra-php8.0
php-devel.x86_64                                                   8.0.20-1.amzn2                                           amzn2extra-php8.0
php-embedded.x86_64                                                8.0.20-1.amzn2                                           amzn2extra-php8.0
php-enchant.x86_64                                                 8.0.20-1.amzn2                                           amzn2extra-php8.0
php-gd.x86_64                                                      8.0.20-1.amzn2                                           amzn2extra-php8.0
php-gmp.x86_64                                                     8.0.20-1.amzn2                                           amzn2extra-php8.0
php-intl.x86_64                                                    8.0.20-1.amzn2                                           amzn2extra-php8.0
php-ldap.x86_64                                                    8.0.20-1.amzn2                                           amzn2extra-php8.0
php-mbstring.x86_64                                                8.0.20-1.amzn2                                           amzn2extra-php8.0
php-mysql.x86_64                                                   5.4.16-43.amzn2                                          amzn2-core
php-odbc.x86_64                                                    8.0.20-1.amzn2                                           amzn2extra-php8.0
php-opcache.x86_64                                                 8.0.20-1.amzn2                                           amzn2extra-php8.0
php-pear.noarch                                                    1:1.10.12-9.amzn2                                        amzn2-core
php-pecl-imagick.x86_64                                            3.5.1-1.amzn2.0.1                                        amzn2extra-php8.0
php-pecl-imagick-devel.noarch                                      3.5.1-1.amzn2.0.1                                        amzn2extra-php8.0
php-pecl-memcache.x86_64                                           3.0.8-4.amzn2                                            amzn2-core
php-pgsql.x86_64                                                   8.0.20-1.amzn2                                           amzn2extra-php8.0
php-process.x86_64                                                 8.0.20-1.amzn2                                           amzn2extra-php8.0
php-pspell.x86_64                                                  8.0.20-1.amzn2                                           amzn2extra-php8.0
php-recode.x86_64                                                  5.4.16-46.amzn2.0.2                                      amzn2-core
php-snmp.x86_64                                                    8.0.20-1.amzn2                                           amzn2extra-php8.0
php-soap.x86_64                                                    8.0.20-1.amzn2                                           amzn2extra-php8.0
php-sodium.x86_64                                                  8.0.20-1.amzn2                                           amzn2extra-php8.0
php-xml.x86_64                                                     8.0.20-1.amzn2                                           amzn2extra-php8.0
php-xmlrpc.x86_64                                                  5.4.16-46.amzn2.0.2                                      amzn2-core

各种设定 –

スクリーンショット 2022-11-11 10.15.33.png

进行设置以满足此配置。

AWS的设置

网络服务器(公共子网)

スクリーンショット 2022-11-11 10.50.25.png

应用服务器(专用子网)

スクリーンショット 2022-11-11 10.51.22.png

nginx的设置

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include /etc/nginx/mime.types;
    charset UTF-8;
    default_type application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen 80;
        root /var/source;
        index index.php;
        server_name 10.0.10.164;

        location / {
                try_files $uri $uri/ /index.php$is_args$args;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass   10.0.10.164:9000;
                fastcgi_index  index.php;

                include        fastcgi_params;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param  PATH_INFO $fastcgi_path_info;
        }
    }
}

PHP-FPM的配置

user = nginx
group = nginx
listen = 0.0.0.0:9000
listen.allowed_clients = 10.0.0.196

更改所有者

$ chown -R nginx:nginx /var/source

更改权限

$ chmod 644 /var/source/index.php
$ ll /var/source/
-rw-r--r-- 1 nginx nginx 20 11月 10 09:16 index.php

确认

スクリーンショット 2022-11-11 10.55.52.png

我先确认了PHP能够运行。

尝试运行Laravel

安装Composer

 

$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 
$ php -r "if (hash_file('sha384', 'composer-setup.php') === '55ce33d7678c5a611085589f1f3ddf8b3c52d662cd01d4ba75c0ee0459970c2200a51f492d557530c71c15d8dba01eae') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup. php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"
$ sudo mv composer.phar /usr/local/bin/composer
//確認
$ composer -v
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.4.4 2022-10-27 14:39:29

安装Laravel

$ composer create-project laravel/laravel example-app
广告
将在 10 秒后关闭
bannerAds