CultusReport

Web, PHP, smarty, PEAR, e-commerce, or other internet technologies

シンプルなLAMP環境構築手順

LAMP環境の構築手順

ソフトウェア開発をする上で最大の障壁は環境構築なのではないかと思うことがあります。
肝心のソフトウェア開発に専念するため、今回はよくあるLAMP環境をそれなりに簡単な方法で実装します。
OSはAWSAmazon Linuxを想定していますが、CentOSであればおおよそ同じ手順で動くと思います。

ソフトウェアバージョン

Apache

インストール
sudo yum install -y httpd24

httpdとした場合は2.2系のApacheがインストールされてしまうことがあるため、httpd24でバージョン指定する

設定

Apacheのconfファイルを編集します。

sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.default
sudo vi /etc/httpd/conf/httpd.conf
 <Directory "/var/www/html">
     #
     # Possible values for the Options directive are "None", "All",
     # or any combination of:
     #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
     #
     # Note that "MultiViews" must be named *explicitly* --- "Options All"
     # doesn't give it to you.
     #
     # The Options directive is both complicated and important.  Please see
     # http://httpd.apache.org/docs/2.4/mod/core.html#options
     # for more information.
     #
-    Options Indexes FollowSymLinks
+    Options -FollowSymLinks
  # Customizable error responses come in three flavors:
  # 1) plain text 2) local redirects 3) external redirects
  #
  # Some examples:
  #ErrorDocument 500 "The server made a boo boo."
- #ErrorDocument 404 /missing.html
+ ErrorDocument 404 /index.html
  #ErrorDocument 404 "/cgi-bin/missing_handler.pl"
  #ErrorDocument 402 http://www.example.com/subscription_info.html
  #
  # Supplemental configuration
  #
  # Load config files in the "/etc/httpd/conf.d" directory, if any.
  IncludeOptional conf.d/*.conf
+ 
+ ServerTokens Prod
+ ServerSignature Off


設定後、Apacheを再起動

sudo service httpd restart

・ServerTokensは2.4.27ではデフォルトFull
core - Apache HTTP サーバ バージョン 2.4


・ServerSignatureは2.4.27ではデフォルトOff
core - Apache HTTP Server Version 2.4

・Directoryディレクティブの設定についても2.2系と異なるので、下記を参照して設定する必要有
Upgrading to 2.4 from 2.2 - Apache HTTP Server Version 2.4

・設定するとHTTPヘッダにApacheバージョンが表示されなくなる(設定前はApacheの代わりにApache 2.4.27が表示される)

PHP

インストール
 sudo yum install php70 php70-mbstring php70-mysqlnd 

・無印のphpを指定してインストールすると、PHP5.3.29がインストールされてしまうことがあるのでphp70を指定してインストールする

docs.aws.amazon.com

自動起動の設定
sudo chkconfig httpd on
設定

php.iniを編集します。

sudo cp /etc/php.ini /etc/php.ini.default
sudo vi /etc/php.ini
   ; Default timeout for socket based streams (seconds)
   ; http://php.net/default-socket-timeout
-  default_socket_timeout = 60
+  default_socket_timeout = 5
  ; Decides whether PHP may expose the fact that it is installed on the server
  ; (e.g. by adding its signature to the Web server header).  It is no security
  ; threat in any way, but it makes it possible to determine whether you use PHP
  ; on your server or not.
  ; http://www.php.net/manual/en/ini.core.php#ini.expose-php
- expose_php = On
+ expose_php = Off
  ; Log errors to specified file. PHP's default behavior is to leave this value
  ; empty.
  ; http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-log
  ; Example:
- ;error_log = php_errors.log
+ error_log = /var/log/php_errors.log
  [Date]
  ; Defines the default timezone used by the date functions
  ; http://www.php.net/manual/en/datetime.configuration.php#ini.date.timezone
- ;date.timezone = 
+ date.timezone = Asia/Tokyo

MySQL

インストール
sudo yum install mysql-server
sudo chkconfig mysqld on
設定

my.cnfを編集し、デフォルト文字コードをutf8に設定します。

sudo cp /etc/my.cnf /et/my.cnf.default
sudo vi /etc/my.cnf
      1 [mysqld]
      2 datadir=/var/lib/mysql
      3 socket=/var/lib/mysql/mysql.sock
      4 # Disabling symbolic-links is recommended to prevent assorted security risks
      5 symbolic-links=0
      6 # Settings user and group are ignored when systemd is used.
      7 # If you need to run mysqld under a different user or group,
      8 # customize your systemd unit file for mysqld according to the
      9 # instructions in http://fedoraproject.org/wiki/Systemd
+    10 character-set-server = utf8
+    11 skip-character-set-client-handshake
+    12
+    13 [mysqld]
+    14 character-set-server = utf8
+    15 skip-character-set-client-handshake
+    16
+    17 [mysqldump]
+    18 default-character-set=utf8
+    19
+    20 [mysql]
+    21 default-character-set=utf8
     22
     23 [mysqld_safe]
     24 log-error=/var/log/mysqld.log
     25 pid-file=/var/run/mysqld/mysqld.pid

再起動して設定変更を反映します。

sudo service mysqld restart
rootパスワードの設定

デフォルトではMySQLのrootユーザーにパスワードが設定されていないので、以下の手順でパスワードを設定します。

mysql -u root -p
UPDATE mysql.user SET Password=PASSWORD('NewPassword') WHERE User='root';
FLUSH PRIVILEGES;
自動起動の設定
sudo chkconfig mysqld on

Linux

OS時刻の設定

OS時刻がデフォルトでJSTではない場合があります。デフォルト時刻がUTCである場合には以下の手順でJSTに変更します。

sudo vi /etc/sysconfig/clock
-ZONE="UTC"
+ZONE="Asia/Tokyo"
 UTC=true
sudo ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime
sudo reboot
ファイルパーミッションの設定

カレントユーザーでログインした際に、サーバーモジュールを上書きできるように設定する。
カレントユーザー(ec2-user)をグループapacheに追加する。

[ec2-user ~]$ sudo usermod -a -G apache ec2-user
[ec2-user ~]$ exit
[ec2-user ~]$ groups
ec2-user wheel apache

ディレクトリのデフォルトパーミッションを755、ファイルパーミッションを644に設定する。

[ec2-user ~]$ sudo chown -R ec2-user:apache /var/www
[ec2-user ~]$ sudo chmod 2775 /var/www
[ec2-user ~]$ find /var/www -type d -exec sudo chmod 2775 {} \;
[ec2-user ~]$ find /var/www -type f -exec sudo chmod 0664 {} \;