EC2にNginxサーバーを立てて、PHPが動くようにしてから、オレオレ証明書でhttps化をする
EC2にNginxサーバーを立てて、PHPが動くようにしてから、オレオレ証明書でhttps化をする
なんでオレオレ証明書?
Let's Encryptを試す前に、オレオレ証明書でサーバーを立ててみてからやろうと思ったから。
前提
- EC2サーバーを立てれる。
- linuxの事をなんとなく知っている。
早速開始
phpが動くようにする
EC2にphpとNginxをインストール
$ sudo yum install php71 php71-devel php71-fpm php71-mysql php71-mysqlnd php71-mbstring php71-pdo php71-zip $ sudo yum install nginx
設定ファイルを変更
/etc/php-fpm.d/www.conf
# 下のように変更する user = nginx group = nginx listen.user = nginx listen.group = nginx
/etc/nginx/conf.d/default.conf
DocumentRootは/home/ec2-user/www/publicにしました。
server { listen 80; server_name EC2のパブリックドメイン; root /home/ec2-user/www/public; location / { root /home/ec2-user/www; index index.html index.htm index.php; } location ~ \.php$ { root /home/ec2-user/www/public; fastcgi_pass unix:/var/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
誰でも見れるようにパーミッションの変更
$ sudo chmod 755 /home/ec2-user/
確認
いつものphpinfoの出力をするindex.phpを作って、
/home/ec2-user/wwwにindex.phpを置いて動作させてみる。
$ sudo service nginx start
ログの場所
何かエラーが出たらログを確認してみましょう。
$ sudo tail -f /var/log/nginx/error.log
403が出た時はパーミッションエラーの可能性があるので、www下のファイルの権限をチェック。
503の時は/etc/php-fpm.d/www.confのlistenと/etc/nginx/conf.d/default.confのfastcgi_passの内容が違うからかもしれないです。同じものにしないといけないとのこと。
/etc/php-fpm.d/www.confのlisten
; The address on which to accept FastCGI requests. ; Valid syntaxes are: ; 'ip.add.re.ss:port' - to listen on a TCP socket to a specific IPv4 address on ; a specific port; ; '[ip:6:addr:ess]:port' - to listen on a TCP socket to a specific IPv6 address on ; a specific port; ; 'port' - to listen on a TCP socket to all addresses ; (IPv6 and IPv4-mapped) on a specific port; ; '/path/to/unix/socket' - to listen on a unix socket. ; Note: This value is mandatory. listen = /var/run/php-fpm/www.sock <-- これ[f:id:Bee_Flim:20180322192006p:plain]
/etc/nginx/conf.d/default.confのfastcgi_pass
location ~ \.php$ { root /home/ec2-user/www/public; fastcgi_pass unix:/var/run/php-fpm/www.sock; <-- これ fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
SSL and Http2の設定をする
sshキーの作成をする
sudo mkdir /etc/nginx/ssl cd /etc/nginx/ssl sudo openssl req -new -x509 -sha256 -newkey rsa:2048 -days 365 -nodes -out /etc/nginx/ssl/nginx.pem -keyout /etc/nginx/ssl/nginx.key
nginxの設定を更新
/etc/nginx/conf.d/default.conf
# 80 portで接続に来た場合、httpsへ転送する server { listen 80; server_name EC2のパブリックサーバー名; return 301 https://$host$request_uri; } server { # sslとhttp2を追記 listen 443 ssl http2; server_name EC2のパブリックサーバー名; root /home/ec2-user/www; # ssl keyを追加する ssl_certificate /etc/nginx/ssl/nginx.pem; ssl_certificate_key /etc/nginx/ssl/nginx.key; # 前方秘匿性の確立 ssl_ciphers ECDHE+AESGCM:DHE+AESGCM:HIGH:!aNULL:!MD5; # キャッシュ等の設定 ssl_session_timeout 5m; ssl_prefer_server_ciphers on; # httpAccessがあっても強制でhttpsにする add_header Strict-Transport-Security "max-age=15768000; includeSubdomains"; location / { root /home/ec2-user/www; index index.html index.htm index.php; } location ~ \.php$ { root /home/ec2-user/www; fastcgi_pass unix:/var/run/php-fpm/www.sock; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
確認
chromeのエクステンションHTTP/2 and SPDY indicatorを入れて確認する。