CentOS7+nginx+php環境の構築

今まではhttpd環境でのWebサーバ構築が主流でしたが、高速で軽量なnginxも人気なのでnginxでのWebサーバ構築を行ってみました。
nginxにするとLAMPならぬLNMP?になるのでしょうか。

早速nginxのインストールを進めていきます。

Contents

nginxのインストール

httpdが既にインストールされた環境では事前にhttpdを削除しておきましょう。
yumコマンドだとこのように実行します。

# yum remove httpd*

nginx実行ユーザの追加

nginxの実行ユーザをログインシェルが使用できないようにしてユーザを追加します。

# useradd -s /bin/false nginx

nginxのリポジトリを登録してインストール

CentOSの標準環境では、yumでnginxのインストールが行えないためレポジトリを追加します。

# rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

レポジトリの登録が終わったらyumでnginxをインストールします。

# yum install nginx

インストールが終わったらnginxが起動するか確認します。

# systemctl start nginx

nginxのドキュメントルートはここになります。後ほどnginxの設定で必要になる情報です。
nginxのドキュメントルート
/usr/share/nginx/html

Mysqlのインストール

Centos7から標準でインストールされるDBがMariaDBへ変わりましたが、自分はMysqlの方が勝手がわかって良いのでMysqlをインストールします。

Mysqlのリポジトリを登録してインストール

mysqlをyumでインストールするためにレポジトリを追加します。

# rpm -ivh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm

レポジトリの登録が終わったらyumでmysqlをインストール

# yum install mysql-community-server

インストールが終わったらmysqlが起動するか確認しておきます。

# systemctl start mysqld

mysqlのrootユーザのパスワードを設定

起動が確認できたらrooのパスワードをしておきます。

# mysql -u root
mysql> update mysql.user set password=password('パスワード') where user = 'root';
mysql> flush privileges;
mysql> exit;

設定したパスワードでログインが可能か確認します。

# mysql -u root -p
Enter password:

PHPインストール

nginxでphpを使用する場合は、php-fpmを使用するようになります。
php-fpmはPHPのマニュアルによると、

FPM ( FastCGI Process Manager ) は PHP の FastCGI 実装のひとつで、 主に高負荷のサイトで有用な追加機能を用意しています。

のようにCGIとして実行するPHPとなります。

php-fpmのリポジトリを登録しインストール

yumでphp-fpmのインストールを行うためレポジトリを追加します。
remiリポジトリとremiリポジトリを入れる為のepelリポジトリが必要になります。

epelリポジトリの追加

# yum -y install epel-release
# rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

レポジトリの登録が終わったらyumでphp-fpmをインストールします。

# yum install --enablerepo=epel  --enablerepo=remi  --enablerepo=remi-php70 php php-mcrypt php-mbstring php-fpm php-mysqlnd php-opcache php-apcu php-gd

※依存関係でhttpdが入ってしまうのでhttpdを削除します。
一緒にphpも削除されてしまいますが、使用するのはphp-pfmなので特に問題はありません。

# yum remove httpd*

php-fpmの設定

/etc/php-fpm.d/www.confのバックアップを作成し編集します。

# vi /etc/php-fpm.d/www.conf

実行ユーザをnginxに変更します。

; RPM: apache Choosed to be able to access some dir as httpd
user = nginx ←nginxに変更
; RPM: Keep a group allowed to write in log dir.
group = nginx ←nginxに変更

続いて/etc/nginx/conf.d/ default.confのバックアップを作成し編集します。

#vi /etc/nginx/conf.d/ default.conf
server {
listen 80;
server_name localhost;location / {
root /usr/share/nginx/html;
index index.php index.html index.htm; <<– デフォルトページにindex.phpを追記
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass 127.0.0.1;
#}

#pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

ここからコメントアウトを外します。
location ~ \.php$ {
root /usr/share/nginx/html; <<– 絶対パスに変更
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; <<– 「$document_root」を追記
include fastcgi_params;
}
ここまでコメントアウトを外します。

# deny access to .htaccess files, if Apache’s document root
# concurs with nginx’s one
#
#location ~ /\.ht {

# deny all;
#}
}

編集が終わり保存したら以下のコマンドで php-fpmを起動、nginxを再起動します。

# systemctl start php-fpm
# systemctl restart nginx

自動起動の設定

最後にインストールしたnginx、Mysql、php-pfm、vsfpdがサーバ起動時に自動起動するように設定しておきます。

systemctl enable nginx
systemctl enable mysqld
systemctl enable php-fpm

以上でCentOS7環境へのnginx設定が完了となります。

ftpを使用したい場合は、「CentOS7環境のvsftpdの設定」を参照ください。

(Visited 654 times, 1 visits today)