提供:すだちWiki
移動先: 案内検索

ヘルプ:nginxをインストール(CentOS7)

メインページ > ヘルプ:目次 > ヘルプ:自宅サーバー構築(CentOS7)  > nginxをインストール(CentOS7)

nginx+php7+MariaDBの環境を構築します。

nginxをインストール

インストール

[root@host2 ~]# yum --enablerepo=epel install nginx

設定ファイル編集

[root@host2 ~]# vi /etc/nginx/nginx.conf

   server {
       listen       80 default_server;
       listen       [::]:80 default_server;
       #server_name  _;              ← コメントアウト 
       server_name  sudachi.jp;      ← サーバー名追加
       root         /usr/share/nginx/html;

サービス起動

起動

[root@host2 ~]# systemctl start nginx

確認

[root@host2 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since 土 2017-05-27 21:45:13 JST; 35s ago
  Process: 30128 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 30126 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 30123 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 30130 (nginx)
   CGroup: /system.slice/nginx.service
           ├─30130 nginx: master process /usr/sbin/nginx
           ├─30131 nginx: worker process
           └─30132 nginx: worker process

 5月 27 21:45:13 host2.sudachi.jp systemd[1]: Starting The nginx HTTP and reverse....
 5月 27 21:45:13 host2.sudachi.jp nginx[30126]: nginx: the configuration file /et...k
 5月 27 21:45:13 host2.sudachi.jp nginx[30126]: nginx: configuration file /etc/ng...l
 5月 27 21:45:13 host2.sudachi.jp systemd[1]: Started The nginx HTTP and reverse ....
Hint: Some lines were ellipsized, use -l to show in full.

有効化

[root@host2 ~]# systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

稼働確認

  • テストページ作成
[root@host2 ~]# echo test >> /var/www/nginx/html/index.html
  • ブラウザで確認してtestページが表示されることを確認します。

php7をインストール

PHP7用のRemiリポジトリの優先度を1(最大)にする

エラーが出たため、PHP7用のRemiリポジトリの優先度を1(最大)にすることで問題を回避しました。

[root@host2 ~]# vi /etc/yum.repos.d/remi-php70.repo

[remi-php70]
priority=1 ← 追加
name=Remi's PHP 7.0 RPM repository for Enterprise Linux 7 - $basearch
#baseurl=http://rpms.remirepo.net/enterprise/7/php70/$basearch/
mirrorlist=http://rpms.remirepo.net/enterprise/7/php70/mirror
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-remi

インストール

[root@host2 ~]# yum install --enablerepo=epel,remi-php70 php php-mbstring php-pear php-fpm php-mcrypt php-mysql

php-fpmをインストール

nginxにてphpが使用出来るようにするためにphp-fpmをインストールします。

インストール

[root@host2 ~]# yum --enablerepo=epel,remi,remi-php70 install php70-php-fpm

設定

[root@host2 ~]# vi /etc/php-fpm.d/www.conf
・
・
user = nginx    ← 変更
group = nginx    ← 変更

サービス起動

[root@host2 ~]# systemctl start php-fpm

確認

[root@host2 ~]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: active (running) since 土 2017-05-27 23:05:49 JST; 29s ago
 Main PID: 30756 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/php-fpm.service
           ├─30756 php-fpm: master process (/etc/php-fpm.conf)
           ├─30757 php-fpm: pool www
           ├─30758 php-fpm: pool www
           ├─30759 php-fpm: pool www
           ├─30760 php-fpm: pool www
           └─30761 php-fpm: pool www

 5月 27 23:05:49 host2.sudachi.jp systemd[1]: Starting The PHP FastCGI Process Ma....
 5月 27 23:05:49 host2.sudachi.jp systemd[1]: Started The PHP FastCGI Process Man....
Hint: Some lines were ellipsized, use -l to show in full.

サービス有効化

[root@host2 ~]# systemctl enable php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

php7動作確認

  • PHPInfo を作成して PHP の動作確認をする。
[root@host2 ~]# echo "<?php phpinfo() ?>" > /usr/share/nginx/html/info.php
  • ブラウザでアクセスして動作確認。
http://192.168.1.2/info.php

phpinfo-nginx.png

php extensionのインストール

extensionの必要そうなものをインストールします。

[root@host2 ~]# yum --enablerepo=epel,remi,remi-php70 install php70-php-mcrypt php70-php-mbstring php70-php-gd