LoginSignup
31
56

More than 5 years have passed since last update.

Cent OS 7上でApache/PHP開発環境を構築する

Last updated at Posted at 2017-10-30

はじめに

Linuxサーバ環境として定番のCentOS7上に、PHP7.1が動作するWebサーバ環境を構築していきたいと思います。
前回までの記事で、GCP上にCentOS開発サーバを構築する手順をまとめていますので、あわせてお読みください。
* GCPに共通鍵を追加する手順まとめ
* GCP Compute Engineを使ってLinuxサーバを始める
* Cent OS 7開発用サーバを初期設定する

インストール手順

作業方針

簡単にインストールできるよう、今回はCentOS標準パッケージマネージャのyumを使います。

PHP 7.1インストール

パッケージのインストール

以下のコマンドでPHP最新版をremiリポジトリからインストールします。
※比較的よく使いそうなPEAR,APCU,XML関連のモジュールも指定しました。

yum -y install http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
yum -y install --enablerepo=remi-php71 php php-cli php-common php-devel php-mbstring php-pear php-pecl-apcu php-soap php-xml php-xmlrpc php-zip

php.iniの編集

設定ファイルを、以下を参考に編集します。
※以下はGCP n1-standard-1(vCPU x 1、メモリ 3.75 GB)程度のスペックを想定しています。

/etc/php.ini
[PHP]

;;;;;;;;;;;;;;;;;;;;
; Language Options ;
;;;;;;;;;;;;;;;;;;;;

engine = On
short_open_tag = On
precision = 14
output_buffering = 4096
zlib.output_compression = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = Off

;;;;;;;;;;;;;;;;;;;
; Resource Limits ;
;;;;;;;;;;;;;;;;;;;

max_execution_time = 30
max_input_time = 60
memory_limit = 64M

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Error handling and logging ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = Off
html_errors = On

;;;;;;;;;;;;;;;;;
; Data Handling ;
;;;;;;;;;;;;;;;;;

variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 64M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"

;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;

doc_root =
user_dir =
enable_dl = Off

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

file_uploads = On
upload_max_filesize = 32M
max_file_uploads = 20

;;;;;;;;;;;;;;;;;;
; Fopen wrappers ;
;;;;;;;;;;;;;;;;;;

allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60

;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;
; Module Settings ;
;;;;;;;;;;;;;;;;;;;

[CLI Server]
cli_server.color = On

[Date]
date.timezone = Asia/Tokyo

[Pcre]
pcre.jit=0

[mail function]
sendmail_path = /usr/sbin/sendmail -t -i
mail.add_x_header = On

[bcmath]
bcmath.scale = 0

[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5

[Assertion]
zend.assertions = -1

[mbstring]
mbstring.language = Japanese

正常にインストールできているか、以下のコマンドで確認できます。

php -v

以下のように表示されれば成功です。

PHP 7.1.10 (cli) (built: Sep 27 2017 08:27:18) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

Composerのインストール

最近のPHPでは主流となっているパッケージマネージャのComposerをインストールします。

curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/bin/composer

正常にインストールできているか、以下のコマンドで確認できます。

composer -V

以下のように表示されれば成功です。

Composer version 1.5.2 2017-09-11 16:59:25

XDebugのインストール

以下の手順で、XDebugをソースからコンパイルし、インストールします。
※XDebugはPHPプログラムをリモートデバッグを行う為のモジュールですので、必要ない方はこの項を読み飛ばしてください。

cd /usr/local/src
git clone git://github.com/xdebug/xdebug.git
cd xdebug
phpize
./configure --enable-xdebug
make
make test
make install

php.iniに以下の行を追加します。

/etc/php.ini
;;;;;;;;;;;;;;;;;;;;;;
; Dynamic Extensions ;
;;;;;;;;;;;;;;;;;;;;;;
zend_extension=xdebug.so

[XDebug]
xdebug.dump_undefined = 1
xdebug.remote_enable = 1
xdebug.remote_host=localhost
xdebug.remote_port=9000
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.remote_autostart = 1

正常にインストールできているか、以下のコマンドで確認できます。

php -v

以下のように表示されれば成功です。

PHP 7.1.10 (cli) (built: Sep 27 2017 08:27:18) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies
    with Xdebug v2.6.0-dev, Copyright (c) 2002-2017, by Derick Rethans

Apache 2.4インストール

パッケージのインストール

以下のコマンドでApache最新版をインストールします。

yum -y install httpd httpd-devel

Apache実行ユーザの作成

Apacheを実行する為のユーザとして今回はwwwを以下のコマンドで作成します。
※それ以外のユーザで動作させる場合は、この手順は読み飛ばしてださい。
※Apacheをrootユーザで実行しないように注意してください。

useradd www
mkdir /home/www/htdocs
chown www:www /home/www/htdocs
chmod 0777 /home/www/htdocs

httpd.confの編集

設定ファイルを以下のように編集します。
以下の例ではドキュメントルートは"/home/www/htdocs"になります。

/etc/httpd/conf/httpd.conf
ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf
User www
Group www
ServerTokens Prod
ServerAdmin 管理者のメールアドレス
ServerName ドメイン名:80
ErrorLog "logs/error_log"
LogLevel warn
AddDefaultCharset UTF-8
EnableSendfile on
DocumentRoot "/home/www/htdocs"
LimitRequestBody 10485760
LimitRequestFields 100
RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500
FileETag None

<Directory />
    AllowOverride none
    Require all denied
</Directory>

<Directory "/home/www">
    Options FollowSymLinks
    AllowOverride None
    Require all granted
    Require method GET POST
    <Limit GET POST>
        Order allow,deny
        Allow from all
    </Limit>
    <LimitExcept GET POST>
        Order deny,allow
        Deny from all
    </LimitExcept>
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    CustomLog "logs/access_log" combined
</IfModule>

<IfModule mime_module>
    TypesConfig /etc/mime.types
</IfModule>

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

<IfModule reqtimeout_module>
    RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500
</IfModule>

IncludeOptional conf.d/*.conf

Apacheの起動

設定が完了したら以下のコマンドで起動し、自動起動を設定します。

systemctl start httpd
systemctl enable httpd

テストPHPファイルを配置し、動作を確認しましょう。
以下のように作成し、現在時刻が表示されたら成功です。

/home/www/htdocs/index.php
<html><body>Hello, current time is <?= date('r') ?></body></html>
31
56
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
31
56