0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

NGINXのローカル環境の作成 【PHP】編

Last updated at Posted at 2023-01-25

sudo yum install -y epel-release

EPELリポジトリ追加

sudo rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

または
sudo rpm -ivh --nosignature http://rpms.famillecollet.com/enterprise/remi-release-7.rpm
Remiリポジトリ追加

エラー参考サイト
https://atmarkit.itmedia.co.jp/flinux/rensai/linuxtips/487warningrpm.html

EPELかRemiを削除の参考サイト
https://www.kaasan.info/archives/1216

コマンドを打つ
PHP7.0なら
sudo yum -y install --enablerepo=remi-php70 php php-devel php-mbstring php-pecl-ssh2 php-pecl-redis ImageMagick-devel php-tidy php-xml php-json php-mysqlnd php-intl php-pdo php-fpm php-pear php-cli php-common php-mysql php-phpunit-PHPUnit php-pecl-xdebug php-gmp php-mcrypt php-opcache php-pdo php-gd

PHP7.4なら
sudo yum -y install --enablerepo=remi-php74 php php-devel php-mbstring php-pecl-ssh2 php-pecl-redis ImageMagick-devel php-tidy php-xml php-json php-mysqlnd php-intl php-pdo php-fpm php-pear php-cli php-common php-mysql php-phpunit-PHPUnit php-pecl-xdebug php-gmp php-mcrypt php-opcache php-pdo php-gd


php-fpmインストール
phpインストール

sudo rpm -qa | grep php

    php-fpm-7.0.33-33.el7.remi.x86_64があるか確認

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

:se nu

/etc/php-fpm.d/配下のwww.confファイルを編集
以下の用にapacheからnginxに編集

image.png

18; prefix = /path/to/pools/$pool
追加
listen = /var/run/php-fpm/php-fpm.sock
24; user = nginx編集
25; RPM: Keep a group allowed to write in log dir.
26; group = nginx編集
以下略
38; listen = 127.0.0.1:9000(コメントアウト)
以下略
48; listen.owner = nginx編集(コメント外す)
49; listen.group = nginx編集(コメント外す)
50; listen.mode = 0660コメント外す

コマンド

sudo sed -i '19i\listen = /var/run/php-fpm/php-fpm.sock' /etc/php-fpm.d/www.conf
sudo sed -i 's/user = apache/user = nginx/' /etc/php-fpm.d/www.conf
sudo sed -i 's/group = apache/group = nginx/' /etc/php-fpm.d/www.conf
sudo sed -i 's/listen = 127.0.0.1:9000/;listen = 127.0.0.1:9000/' /etc/php-fpm.d/www.conf
sudo sed -i 's/; listen.owner = apache/listen.owner = nginx/' /etc/php-fpm.d/www.conf
sudo sed -i 's/; listen.group = apache/listen.group = nginx/' /etc/php-fpm.d/www.conf
sudo sed -i 's/; listen.mode = 0660/listen.mode = 0660/' /etc/php-fpm.d/www.conf

sudo systemctl restart php-fpm

php-fpmを再起動

systemctl status php-fpm

Active: active (running)を確認する

sudo systemctl enable php-fpm

php-fpmを自動起動

sudo vi /etc/nginx/conf.d/default.conf

ドキュメントルートの場所を設定する
/etc/nginx/conf.d/配下のdefault.confファイルを編集
以下の用に追加と編集

:%d

一旦全削除

↓を貼り付け

server {
  root /usr/share/nginx/html;
  listen 80;
  server_name localhost;

  location /{
      index index.php;
      try_files $uri $uri/index.php;
   }

  error_page  500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

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

コマンド

sudo tee /etc/nginx/conf.d/default.conf > /dev/null <<'EOF' && sudo systemctl restart nginx
server {
  root /usr/share/nginx/html;
  listen 80;
  server_name localhost;

  location / {
    index index.php;
    try_files $uri $uri/index.php;
  }

  error_page  500 502 503 504 /50x.html;
  location = /50x.html {
    root /usr/share/nginx/html;
  }

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

sudo systemctl restart nginx

nginxを再起動

sudo nginx -t

nginxの設定ファイルのチェック
以下になっていればok
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

sudo nginx -s reload

nginxを再読み込み

sudo touch /usr/share/nginx/html/index.php

phpファイルの作成

sudo vi /usr/share/nginx/html/index.php

phpファイルに移動
編集する以下を貼り付ける
<?php phpinfo(); ?>

http://192.168.67.10//

をweb上で打つ

ちゃんとページが表示されてphpも動いてることを確認

image.png

次は
https://qiita.com/panda-chibi/items/01b0ed087658f1e5624f

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?