VM上のubuntu18.04でnginxを使いLaravelプロジェクトを動かす

概要

0.準備

0-1.ubuntu18.04へのLravelインストール

0-2.ubuntuの準備

  • リポジトリ一覧を更新 & パッケージを更新
  • 1
    sudo apt update && sudo apt upgrade -y
  • ubuntuに新しいuserを作成 後述のphp-fpmとnginxの設定に使用します

    • nginxという名前のグループとユーザを作成、ログインシェルにbashを指定
      • 1
        sudo useradd -U nginx -s /bin/bash

1.php-fpmの設定

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

  • 1
    $ sudo apt install php-fpm
  • 下記コマンドでソケットファイルを確認できる

    • 1
      2
      3
      $ ls /run/php

      php7.2-fpm.pid php7.2-fpm.sock
  • 後述のNginxの設定ファイルにphp7.2-fpm.sockへのパスを設定する

1-2.php-fpmのww.conf編集

  • www-data になっているところを nginx に変更
  • 1
    2
    3
    4
    5
    6
    $ vi sudo /etc/php/7.2/fpm/pool.d/www.conf

    user = nginx
    group = nginx
    listen.owner = nginx
    listen.group = nginx

2.Nginxの設定

2-1.Nginxインストール

  • 1
    $ sudo apt install nginx
  • etc/nginxの所有者をユーザ nginx にする

    • 1
      sudo chown -R  nginx /etc/nginx

2-2.Nginx設定

  • /etc/nginx/conf.d/default.conf
  • 追加するphp7.2-fpm.sockのパスはunix:/run/php/php7.2-fpm.sock

nginx.confの編集

  • /etc/nginx/nginx.confuserの値を nginx に変更
    • 1
      user nginx;

defaultの編集

  • /etc/nginx/sites-enabled/defaultを編集
  • 編集前のdefault

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      ##
      # You should look at the following URL's in order to grasp a solid understanding
      # of Nginx configuration files in order to fully unleash the power of Nginx.
      # https://www.nginx.com/resources/wiki/start/
      # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
      # https://wiki.debian.org/Nginx/DirectoryStructure
      #
      # In most cases, administrators will remove this file from sites-enabled/ and
      # leave it as reference inside of sites-available where it will continue to be
      # updated by the nginx packaging team.
      #
      # This file will automatically load configuration files provided by other
      # applications, such as Drupal or Wordpress. These applications will be made
      # available underneath a path with that package name, such as /drupal8.
      #
      # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
      ##

      # Default server configuration
      #
      server {
      listen 80 default_server;
      listen [::]:80 default_server;

      # SSL configuration
      #
      # listen 443 ssl default_server;
      # listen [::]:443 ssl default_server;
      #
      # Note: You should disable gzip for SSL traffic.
      # See: https://bugs.debian.org/773332
      #
      # Read up on ssl_ciphers to ensure a secure configuration.
      # See: https://bugs.debian.org/765782
      #
      # Self signed certs generated by the ssl-cert package
      # Don't use them in a production server!
      #
      # include snippets/snakeoil.conf;

      root /var/www/html;

      # Add index.php to the list if you are using PHP
      index index.html index.htm index.nginx-debian.html;

      server_name _;

      location / {
      # First attempt to serve request as file, then
      # as directory, then fall back to displaying a 404.
      try_files $uri $uri/ =404;
      }

      # pass PHP scripts to FastCGI server
      #
      #location ~ \.php$ {
      # include snippets/fastcgi-php.conf;
      #
      # # With php-fpm (or other unix sockets):
      # fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
      # # With php-cgi (or other tcp sockets):
      # fastcgi_pass 127.0.0.1:9000;
      #}

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


      # Virtual Host configuration for example.com
      #
      # You can move that to a different file under sites-available/ and symlink that
      # to sites-enabled/ to enable it.
      #
      #server {
      # listen 80;
      # listen [::]:80;
      #
      # server_name example.com;
      #
      # root /var/www/example.com;
      # index index.html;
      #
      # location / {
      # try_files $uri $uri/ =404;
      # }
      #}
  • 編集後のdefault

    • location ~ \.php$ から始まるブロックを追加している
    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      ##
      # You should look at the following URL's in order to grasp a solid understanding
      # of Nginx configuration files in order to fully unleash the power of Nginx.
      # https://www.nginx.com/resources/wiki/start/
      # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
      # https://wiki.debian.org/Nginx/DirectoryStructure
      #
      # In most cases, administrators will remove this file from sites-enabled/ and
      # leave it as reference inside of sites-available where it will continue to be
      # updated by the nginx packaging team.
      #
      # This file will automatically load configuration files provided by other
      # applications, such as Drupal or Wordpress. These applications will be made
      # available underneath a path with that package name, such as /drupal8.
      #
      # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
      ##

      # Default server configuration
      #
      server {
      listen 80 default_server;
      listen [::]:80 default_server;

      # SSL configuration
      #
      # listen 443 ssl default_server;
      # listen [::]:443 ssl default_server;
      #
      # Note: You should disable gzip for SSL traffic.
      # See: https://bugs.debian.org/773332
      #
      # Read up on ssl_ciphers to ensure a secure configuration.
      # See: https://bugs.debian.org/765782
      #
      # Self signed certs generated by the ssl-cert package
      # Don't use them in a production server!
      #
      # include snippets/snakeoil.conf;

      root /var/www/html;

      # Add index.php to the list if you are using PHP
      index index.html index.htm index.nginx-debian.html;

      server_name _;

      location / {
      # First attempt to serve request as file, then
      # as directory, then fall back to displaying a 404.
      try_files $uri $uri/ =404;
      }

      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;
      }

      # pass PHP scripts to FastCGI server
      #
      #location ~ \.php$ {
      # include snippets/fastcgi-php.conf;
      #
      # # With php-fpm (or other unix sockets):
      # fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
      # # With php-cgi (or other tcp sockets):
      # fastcgi_pass 127.0.0.1:9000;
      #}

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


      # Virtual Host configuration for example.com
      #
      # You can move that to a different file under sites-available/ and symlink that
      # to sites-enabled/ to enable it.
      #
      #server {
      # listen 80;
      # listen [::]:80;
      #
      # server_name example.com;
      #
      # root /var/www/example.com;
      # index index.html;
      #
      # location / {
      # try_files $uri $uri/ =404;
      # }
      #}

2-3.Nginx起動

  • nginxを起動

    • 1
      $ sudo service nginx start
  • サーバIP(今回は192.168.33.11/info.php)にアクセス

    • Nginx初期画面が表示されるか確認

試しにファイルを作成

3-1.LaravelプロジェクトをNginxのルートに設定

  • Laravelのインストール
  • 2-2 で編集した/etc/nginx/sites-enabled/defaultのrootをLaravelプロジェクトのルートに変更、location /に「index index.php index.html index.htm;」を追加
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    root /home/vagrant/laravel_project/public;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    index index.php index.html index.htm;
    try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
    root /home/vagrant/laravel_project/public;
    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;
    }
  • サーバIP(http://192.168.33.11/)へアクセス

    • エラーになる
    • 1
      2
             UnexpectedValueException
      The stream or file "/home/vagrant/laravel_project/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied
  • ユーザ nginx がLaravelプロジェクトに対して権限を持っていないのが原因っぽい

  • /home/vagrant/laravel_projectの所有者もユーザ nginx にしてみる

    • 1
      sudo chown -R  nginx /home/vagrant/laravel_project
  • 一応サーバを再起動して実行

  • 1
    sudo service php7.2-fpm restart && sudo service nginx restart
  • Laravelのデフォルトページが表示されたら成功です。

感想

参考