LoginSignup
26
28

More than 5 years have passed since last update.

AWSでPHP+nginxの環境を作る

Last updated at Posted at 2016-02-12

はじめに

AWSでPHPインストールからnginxを設定する機会があったのでそのメモになります。

概要

  1. OSのアップデート
  2. 必要なモジュールをインストール
  3. PHPのインストール
  4. nginxのインストール
  5. SSL作成
  6. nginx設定
  7. 必要なサービスを起動

詳細


OSのアップデート

まず起動したてのインスタンスはアップデートしてくださいのメッセージが出るのでアップデートしときます。



       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2015.09-release-notes/
23 package(s) needed for security, out of 42 available
Run "sudo yum update" to apply all updates.
$ sudo yum update

必要なモジュールのインストール

必要そうなモジュールを入れときます。

$ sudo yum install curl-devel memcached

memcachedは起動するように設定しておきます。

$ sudo chkconfig memcached on

PHPのインストール

PHP56のインストールをします。まずレポジトリにあるか確認します。

$ sudo yum info php56
読み込んだプラグイン:priorities, update-motd, upgrade-helper
利用可能なパッケージ
名前                : php56
アーキテクチャー    : x86_64
バージョン          : 5.6.17
リリース            : 1.121.amzn1
容量                : 3.0 M
リポジトリー        : amzn-updates/latest
要約                : PHP scripting language for creating dynamic web sites
URL                 : http://www.php.net/
ライセンス          : PHP and Zend and BSD
説明                : PHP is an HTML-embedded scripting language. PHP attempts to make it
                    : easy for developers to write dynamically generated web pages. PHP also
                    : offers built-in database integration for several commercial and
                    : non-commercial database management systems, so writing a
                    : database-enabled webpage with PHP is fairly simple. The most common
                    : use of PHP coding is probably as a replacement for CGI scripts.
                    : 
                    : The php package contains the module (often referred to as mod_php)
                    : which adds support for the PHP language to Apache HTTP Server.

確認すると存在しそうなので必要そうなのをインストールします。

$ sudo yum install php56 php56-mbstring php56-pdo php56-ldap php56-xml php56-mcrypt php56-gd php56-imap php56-pecl-imagick php56-pecl-apc php56-jsonc php56-fpm php56-pecl-memcached

php.iniを最低限の設定します。

$ sudo vi /etc/php.ini
date.timezone = Asia/Tokyo
default_charset = "UTF-8"
mbstring.language = Japanese
mbstring.internal_encoding = UTF-8
mbstring.detect_order = UTF-8,SJIS,EUC-JP,JIS,ASCII

php-fpmの設定をする場合はこちらから設定できます。

$ sudo vi /etc/php-fpm-5.6.d/www.conf
user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
;listen.acl_users = apache,nginx

php-fpmを起動するように設定しときます。

$ sudo chkconfig php-fpm-5.6 on

nginxのインストール

まずnginxがあるか確認します。

$ sudo yum info nginx
読み込んだプラグイン:priorities, update-motd, upgrade-helper
利用可能なパッケージ
名前                : nginx
アーキテクチャー    : x86_64
エポック            : 1
バージョン          : 1.8.0
リリース            : 10.25.amzn1
容量                : 555 k
リポジトリー        : amzn-main/latest
要約                : A high performance web server and reverse proxy server
URL                 : http://nginx.org/
ライセンス          : BSD
説明                : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
                    : IMAP protocols, with a strong focus on high concurrency, performance and low
                    : memory usage.

ありそうなのでインストールします。

$ sudo yum install nginx

自動起動するように設定します。

sudo chkconfig nginx on

SSL作成

コマンドでSSLを作成する。

$ sudo mkdir -p /etc/nginx/ssl
$ cd /etc/nginx/ssl
$ openssl genrsa 2048 > server.key
$ openssl req -new -key server.key > server.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:JP
State or Province Name (full name) []:Tokyo
Locality Name (eg, city) [Default City]:Chuo-ku
Organization Name (eg, company) [Default Company Ltd]:XXXXXX
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:*.XXXXX.com
Email Address []:XXXXX@aaaaaa.bb

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

$ openssl x509 -days 3650 -req -signkey server.key < server.csr > server.crt

※AmazonだとSSLを無料で作成してくれます。(但し、東京リージョンではサポートされていないようです。バージニアに変更します。)

ドメイン名設定すれば、あとは適当に進めば依頼がされるようです。
メールが通知されるようで、その後設定作業等入るんだと思います。

追記)
AWSのSSLのやり方のってました。ELBとかCloud front使用しないと反映できなそうですね。。。
https://aws.amazon.com/jp/blogs/aws/new-aws-certificate-manager-deploy-ssltls-based-apps-on-aws/


nginxの設定

nginx設定をしていきます。/etc/nginx/conf.d/配下にバーチャルホストの設定をします。

server {
       listen       443 ssl;
       server_name  xxx.XXXXX.com;
       root   ドキュメントフォルダ;
       ssl                  on;
       ssl_certificate      /etc/nginx/ssl/server.crt;
       ssl_certificate_key  /etc/nginx/ssl/server.key;
       ssl_session_timeout  5m;
       ssl_protocols  SSLv2 TLSv1;
       ssl_ciphers  HIGH:!aNULL:!MD5;
       ssl_prefer_server_ciphers   on;

        location / {
            fastcgi_index  index.php;
            fastcgi_pass   unix:/var/run/php-fpm/php-fpm.sock;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            index  index.html index.php;
        }
    }

nginx.confも合わせて更新した方がよいです。
以下入れるとエラーの時にバージョンが表示されなくなります。

/etc/nginx/nginx.conf
http {
    server_tokens off;
:

必要なサービスを起動

必要なサービスを起動します。

$ sudo service php-fpm-5.6 start
$ sudo service nginx start

あとがき

とりあえず起動まで行けました。あとはデフォルトで開いているページを閉じとけば良いかと思います。

その他

参考:
nginx を aws EC2 にインストールする
EC2にnginx+php(php-fpm socket)環境を最速で構築
オレオレ証明書をopensslで作る(詳細版)

履歴

2016/02/15 見出しを少し変更、AWSのSSL作成方法について追記、nginxのコンフィグのserver_tokensについて追加
2016/02/18 タイトルが誤解を招きそうだったので変えました。

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