LoginSignup
0
0

More than 1 year has passed since last update.

LaravelをAWSでHTTPSデプロイする

Last updated at Posted at 2021-06-17

ローカルで開発したLaravelアプリをGitHubからクローンして、AWSでHTTPSデプロイするまでの流れを備忘録としてまとめました。
これからデプロイ挑戦する方、デプロイはできたけどHTTPS化できず困っている方(先日のわい)のお役に立てれば嬉しいです!

環境

PHP7.4
Laravel8.0
Apache2.4
EC2 Amazon Linux 2 AMI (HVM), SSD Volume Type

前提

DBの作成はナシ
EC2インスタンス作成済み
開発したLaravelプロジェクトをGitHubにpush済み

ssh接続

terminal
$ mkdir ~/.ssh
$ chmod 700 ~/.ssh
$ cd ~/.ssh
$ mkdir keys
$ cd keys
$ cp ~/Downloads/***.pem ./
$ chmod 400 ~/.ssh/keys/***.pem
$ ssh -i "***.pem" ec2-user@***.ap-northeast-1.compute.amazonaws.com

パッケージアップデート

terminal
$ sudo yum update #-yオプションをつけると確認が要らなくなる

php7.4インストール

yumだとPHP5がインストールされてしまうのでExtra Libralyを使う
php-cli,php-json,php-fpm等必要なパッケージも一緒にインストールされる

terminal
$ sudo amazon-linux-extras install php7.4

インストール可能な拡張モジュールを確認し、必要なものをインストール

terminal
$ sudo yum list php * | grep php7.4
$ sudo yum install php-mbstring php-pecl-memcached php-gd php-apcu php-xml

apacheインストール

terminal
$ sudo yum install httpd
$ sudo systemctl start httpd #起動
$ sudo systemctl enable httpd #オート起動設定
$ sudo systemctl status httpd #動作確認

ドキュメントルートの権限変更

terminal
$ sudo chown -R ec2-user:apache /var/www/html
$ sudo chmod 2775 /var/www/html && find /var/www/html -type d -exec sudo chmod 2775 {} \;

HTTPSサーバ構築

terminal
$ sudo yum install mod_ssl

秘密鍵とサーバ証明書の作成

terminal
$ sudo yum install openssl
$ openssl genrsa > server.key #鍵ファイル作成
$ openssl req -new -key server.key > server.csr #CSRファイル作成
$ openssl x509 -req -signkey server.key < server.csr > 
$ server.crt #サーバ証明書作成
$ rm server.csr #CSR削除
$ sudo mkdir /etc/httpd/conf/ssl.key
$ sudo mkdir /etc/httpd/conf/ssl.crt
$ sudo mv server.key /etc/httpd/conf/ssl.key/ #鍵配置
$ sudo mv server.crt /etc/httpd/conf/ssl.crt/ #サーバ証明書配置
$ sudo vi /etc/httpd/conf.d/ssl.conf #ssl.confに鍵と証明書のパスを設定
ssl.conf
#105行目あたり
# SSLCertificateFile /etc/pki/tls/certs/localhost.crt
SSLCertificateFile /etc/httpd/conf/ssl.crt/server.crt

#112行目あたり
# SSLCertificateKeyFile /etc/pki/tls/private/localhost.key
SSLCertificateKeyFile /etc/httpd/conf/ssl.key/server.key 

confファイル変更を反映

terminal
$ sudo systemctl restart httpd

composerインストール

terminal
<以下4行は公式の通り>
$ sudo php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ sudo php -r "if (hash_file('sha384', 'composer-setup.php') === '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ sudo php composer-setup.php
$ sudo php -r "unlink('composer-setup.php');"

<パスを通す>
$ sudo mv composer.phar /usr/local/bin/composer

<確認>
$ composer

GitHubとSSH接続

terminal
$ sudo yum install git
$ cd ~/.ssh
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" #鍵の生成
- - -
<鍵の保存先やパスワードの設定>
Enter file in which to save the key (/home/ec2-user/.ssh/id_rsa): keyの保存先(そのままEnterでOK)
Enter passphrase (empty for no passphrase): パスワード
Enter same passphrase again: 
- - -

$ cat ~/.ssh/id_rsa(又は設定したID).pub #中身をコピーしてgithubに登録(プロフィールのSettings->SSH-keys->Bodyにコピーした値をペースト->Add-SSH-Key)
$ ssh -T git@github.com #以下が表示されればOK
- - -
Hi ユーザー名/リポジトリ名!
You've successfully authenticated, but GitHub does not provide shell access.
- - -

$ vi ~/.ssh/config #config設定
~/.ssh/config
Host github
    HostName github.com
    IdentityFile ~/.ssh/id_rsa(又は設定したID)_github
    User git
terminal
$ chmod 600 ~/.ssh/config

<秘密鍵をssh-agentに登録>
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/id_rsa(又は設定したID) #Identity added: /home/ec2-user/.ssh/id_rsa と表示されればOK

<クローン作業>
$ cd /var/www/html
$ sudo git clone github:[ユーザー名]/[リポジトリ名].git

Laravel環境整備

terminal
$ cd (Laravelのプロジェクト名)
$ composer install
$ sudo cp .env.example .env #.envはclone対象外のため雛形をコピー
$ php artisan key:generate #appキーの生成(.envに書き込めないとエラーが出たら権限変更)

<プロジェクトのパーミッション変更>
$ sudo chmod 777 storage
$ sudo chmod 777 bootstrap/cache

apacheのconfファイルでドキュメントルートを変更

terminal
$ sudo vi /etc/httpd/conf/httpd.conf
httpd.conf
# 119行目あたり
# DocumentRoot "/var/www/html"
DocumentRoot "var/www/html/(Laravelのプロジェクト名)/public"

# <Directory "/var/www/html">
<Directory "/var/www/html/(Laravelのプロジェクト名)/public">
    Options Indexes FollowSymLinks
    #AllowOverride None
    AllowOverrinde All #NoneからAllに変更
    Require all granted
</Directory>

confファイル変更を反映

terminal
$ sudo systemctl restart httpd

これでHTTPSアクセスができているはず!お疲れ様です!:tada:

メモ

ローカルで散々やっていたので、npm installしなくていいの…?ってそわそわしちゃいましたが、本番環境ではそもそもフロントエンドのアセットコンパイル、webpack(バンドル:複数のファイルを1つにまとめること)は必要ないですね、そりゃそうか

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0