LoginSignup
0
1

More than 3 years have passed since last update.

初心者がconohaでVPSを借りてnginxでphpページを公開するまで -nginxとphp編-

Last updated at Posted at 2019-06-25

nginxとphpをインストールして動かす

1:最新のnginxをインストールする

今回はapacheではなくてnginxでサーバーを立てようと思います。

#root権限で以下の作業を実行する
sudo -s

#nginx 公式サイトからPGPkeyをダウンロード
wget https://nginx.org/keys/nginx_signing.key

#PGPkeyを登録
apt-key add nginx_signing.key

#aptのリストに追記する
vi /etc/apt/sources.list
→ deb http://nginx.org/packages/ubuntu/ bionic nginx
→ deb-src http://nginx.org/packages/ubuntu/ bionic nginx

#aptのリストを更新
apt update

#nginxをaptでインストール
apt install nginx

#サーバー起動時にnginxも起動するように設定
systemctl enable nginx

#nginxを起動
systemctl start nginx

これでひとまずnginxのインストールは完了!

2:最新のphpをインストールする

僕はphp大好き人間なのでphp使います。

#root権限で以下の作業を実行する
sudo -s

#aptの一覧にphpを追加する
add-apt-repository ppa:ondrej/php

#aptの一覧の更新
apt-get update

#aptの一覧からphpの最新のバージョンを確認(今回は7.3だった)
apt list|grep -i mariadb-server

#phpをaptでインストール
apt-get install php7.3 php7.3-fpm php7.3-mysql php7.3-mbstring php7.3-zip

#phpの設定ファイルを編集する(その1)
vi /etc/php/7.2/cli/php.ini
→ cgi.fix_pathinfo の値を 0 に
→ date.timezone の値を "Asia/Tokyo"#phpの設定ファイルを編集する(その2)
sudo /etc/php/7.2/fpm/php.ini 
→ cgi.fix_pathinfo の値を 0 に
→ date.timezone の値を "Asia/Tokyo"

これでphpもとりあえずインストール完了!

3:nginxとphpを設定する

nginxとphpの設定を弄ってちゃんと動くようにします。


#root権限で以下の作業を実行する
sudo -s

#nginxの設定ファイルを以下を参考に編集する
vi /etc/nginx/conf.d/default.conf

- - - - -

location / {
  root   /usr/share/nginx/html;
  index  index.html index.htm;
}

  ↓

location / {
  root /usr/share/nginx/html;
  index index.html index.htm index.php;
}

- - - - -
- - - - -

#location ~ \.php$ {
#  root           html;
#  fastcgi_pass   127.0.0.1:9000;
#  fastcgi_index  index.php;
#  fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
#  include        fastcgi_params;
#}

  ↓

location ~ \.php$ {
  root /usr/share/nginx/html;
  fastcgi_pass unix:/run/php/php7.2-fpm.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  include fastcgi_params;
}

- - - - -
- - - - -

#location ~ /\.ht {
#  deny  all;
#}

  ↓

location ~ /\.ht {
  deny all;
}

- - - - -

#phpの設定ファイルを編集する
/etc/php/7.3/fpm/pool.d/www.conf
→ user の値を nginx に
→ group の値を nginx に
→ listen.owner の値を nginx に
→ listen.group の値を nginx に

#サーバー起動時にphpも起動するように設定
systemctl enable php7.3-fpm

#phpを再起動
systemctl restart php7.3-fpm

#nginxを再起動
systemctl restart nginx

#nginxグループにユーザーを加入させる
usermod -aG nginx ユーザー名

#ドキュメントルートの所有者を変更する
chown -R nginx:nginx /usr/share/nginx/html/

#ドキュメントルートの権限を変更する
chmod -R 755 /usr/share/nginx/html/

ここまでやればwebは動く。

…ハズ。
大体設定を変に弄っちゃってエラー吐くパターンが多いです。
僕もかれこれ6時間くらいはハマりました、めげずにがんばりましょう!!

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