miriwoのブログ

IT系の技術や少し趣味よりのことも投稿してゆくよ😊

CentOS7 Apache MySQLでLaravelのローカル環境を構築する

目的

  • CentOS7のPCにApacheMySQLを用いたLaravelの環境の構築方法をまとめる

実施環境

  • ハードウェア環境
項目 情報 備考
OS CentOS 7 (7.8.2003) こちらの方法で導入→Cent0S7をPCにインストールする
ハードウェア Dell Studio 1537
プロセッサ Intel(R) Core(TM)2 Duo CPU P8400 @ 2.26GHz
メモリ 4 GB DDR3
グラフィックス 不明
  • ソフトウェア環境
項目 情報 備考
Apache バージョン 2.4.6 こちらの方法で導入→CentOS7 Apacheを使ってローカルサーバを立ち上げる
MySQLバージョン 8.0.21 for Linux on x86_64 こちらの方法で導入→CentOS7 MySQLを入れてデータベースを作成する
PHP バージョン 7.4.8 こちらの方法で導入→CentOS7 Laravel環境構築のためにPHP7.4をインストールする
composer バージョン 1.10.9 こちらの方法で導入→CentOS7 composerをインストールする
Laravel バージョン 7.X

前提条件

  • OSをインストールすることができるPCがあること。

前提情報

  • 説明で実行するコマンドはクライアントPC(Mac)からSSHを用いて接続を行い実行する。

読後感

  • http://localhostにアクセスするとLaravelの初期画面が表示できる。
  • 認証機能を有したLaravelアプリを用いて認証機能を使用することができる。

概要

  1. CentOS7のPCの構築
  2. selinuxの無効化
  3. Apacheの導入
  4. MySQLの導入
  5. PHPの導入
  6. composerの導入
  7. Laravelアプリの作成
  8. 設定
  9. 確認

詳細

  1. CentOS7のPCの構築
    1. 下記の方法を用いてCentOS7のPCを構築する。
  2. selinuxの無効化
    1. 下記の方法を用いてCentOS7のPCのselinuxを無効化する。
  3. Apacheの導入
    1. 下記の方法を用いてApacheの導入を行う。
  4. MySQLの導入
    1. 下記の方法を用いてMySQLの導入を行う。
  5. PHPの導入
    1. 下記の方法を用いてPHPの導入を行う。
  6. composerの導入
    1. 下記の方法を用いてcomposerの導入を行う。
  7. Laravelアプリの作成
    1. 任意のディレクトリにて下記コマンドを実行する。(command not foundが出た場合こちら記事のパス通しの作業を実施する→CentOS7 composerをインストールする)

       $ composer global require laravel/installer
      
    2. 下記コマンドを実行して$ laravelコマンドが正常に実行できることを確認する。

       $ laravel
      
    3. 下記コマンドを実行して/var/wwwの権限を変更する。

       $ sudo chmod 777 /var/www
      
    4. 下記コマンドを実行して/var/wwwに移動する。

       $ cd /var/www
      
    5. 下記コマンドを実行して「test」というLaravelアプリを新規作成する。

       $ laravel new test
      
    6. 下記コマンドを実行して/var/wwwのオーナーを変更する。

       $ sudo chown -R apache:apache /var/www
      
  8. 設定
    1. 下記コマンドを実行してApacheの設定ファイルを開く。

       $ sudo vi /etc/httpd/conf/httpd.conf
      
    2. 下記の様にドキュメントルートの記載部分を修正する。(別名のLaravelアプリを作成した場合はパスのtestの部分を皆さんの作成したアプリ名で記載する。)
      • 修正前

          #
          # DocumentRoot: The directory out of which you will serve your
          # documents. By default, all requests are taken from this directory, but
          # symbolic links and aliases may be used to point to other locations.
          #
          DocumentRoot "/var/www/html"
        
      • 修正後

          #
          # DocumentRoot: The directory out of which you will serve your
          # documents. By default, all requests are taken from this directory, but
          # symbolic links and aliases may be used to point to other locations.
          #
          #下記を修正
          DocumentRoot "/var/www/test/public"
          #下記を追加
          <Directory /var/www/test/public>
              AllowOverride All
          </Directory>
          #上記までを追加
        
      • 修正後のファイルの全体を下記に記載する。

        
          #
          # Dynamic Shared Object (DSO) Support
          #
          # To be able to use the functionality of a module which was built as a DSO you
          # have to place corresponding `LoadModule' lines at this location so the
          # directives contained in it are actually available _before_ they are used.
          # Statically compiled modules (those listed by `httpd -l') do not need
          # to be loaded here.
          #
          # Example:
          # LoadModule foo_module modules/mod_foo.so
          #
          Include conf.modules.d/*.conf
        
          #
          # If you wish httpd to run as a different user or group, you must run
          # httpd as root initially and it will switch.
          #
          # User/Group: The name (or #number) of the user/group to run httpd as.
          # It is usually good practice to create a dedicated user and group for
          # running httpd, as with most system services.
          #
          User apache
          Group apache
        
          # 'Main' server configuration
          #
          # The directives in this section set up the values used by the 'main'
          # server, which responds to any requests that aren't handled by a
          # <VirtualHost> definition.  These values also provide defaults for
          # any <VirtualHost> containers you may define later in the file.
          #
          # All of these directives may appear inside <VirtualHost> containers,
          # in which case these default settings will be overridden for the
          # virtual host being defined.
          #
        
          #
          # ServerAdmin: Your address, where problems with the server should be
          # e-mailed.  This address appears on some server-generated pages, such
          # as error documents.  e.g. admin@your-domain.com
          #
          ServerAdmin root@localhost
        
          #
          # ServerName gives the name and port that the server uses to identify itself.
          # This can often be determined automatically, but we recommend you specify
          # it explicitly to prevent problems during startup.
          /root
              Require all denied
          </Directory>
        
          #
          # Note that from this point forward you must specifically allow
          # particular features to be enabled - so if something's not working as
          # you might expect, make sure that you have specifically enabled it
          # below.
          #
        
          #
          # DocumentRoot: The directory out of which you will serve your
          # documents. By default, all requests are taken from this directory, but
          # symbolic links and aliases may be used to point to other locations.
          #
          DocumentRoot "/var/www/test/public"
        
          <Directory /var/www/test/public>
              AllowOverride All
          </Directory>
        
          #
          # Relax access to content within /var/www.
          #
          <Directory "/var/www">
              AllowOverride None
              # Allow open access:
              Require all granted
          </Directory>
        
          # Further relax access to the default document root:
          <Directory "/var/www/html">
              #
              # Possible values for the Options directive are "None", "All",
              # or any combination of:
              #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
              #
              # Note that "MultiViews" must be named *explicitly* --- "Options All"
              # doesn't give it to you.
              #
              # The Options directive is both complicated and important.  Please see
              # http://httpd.apache.org/docs/2.4/mod/core.html#options
              # for more information.
              #
              Options Indexes FollowSymLinks
        
              #
              # AllowOverride controls what directives may be placed in .htaccess files.
              # It can be "All", "None", or any combination of the keywords:
              #   Options FileInfo AuthConfig Limit
              #
              AllowOverride None
        
    3. 下記コマンドを実行してApacheを再起動する。

       $ sudo service httpd restart
      
    4. 下記コマンドを実行してアプリ名ディレクトリに移動する。

       $ cd /var/www/test
      
    5. 下記コマンドを実行してアプリを構成するディレクトリの権限を変更する。

       $ sudo chmod 777 storage -R
       $ sudo chmod 777 bootstrap/cache -R
      
    6. 下記コマンドを実行してキャッシュのクリアを行う。

       $ php artisan config:cache
      
    7. 下記コマンドを実行してアプリの設定ファイルを開く。

       $ sudo vi .env
      
    8. 設定ファイルを下記のように修正する。
      • 修正前

          APP_NAME=Laravel
          APP_ENV=local
          APP_KEY=base64:Tx/K6tIRwmdux4Z3zCV3rmMCxaF0SCJATs7E7sKLLfg=
          APP_DEBUG=true
          APP_URL=http://localhost
        
          LOG_CHANNEL=stack
        
          DB_CONNECTION=mysql
          DB_HOST=127.0.0.1
          DB_PORT=3306
          DB_DATABASE=laravel
          DB_USERNAME=root
          DB_PASSWORD=
        
          BROADCAST_DRIVER=log
          CACHE_DRIVER=file
          QUEUE_CONNECTION=sync
          SESSION_DRIVER=file
          SESSION_LIFETIME=120
        
          REDIS_HOST=127.0.0.1
          REDIS_PASSWORD=null
          REDIS_PORT=6379
        
          MAIL_MAILER=smtp
          MAIL_HOST=smtp.mailtrap.io
          MAIL_PORT=2525
          MAIL_USERNAME=null
          MAIL_PASSWORD=null
          MAIL_ENCRYPTION=null
          MAIL_FROM_ADDRESS=null
          MAIL_FROM_NAME="${APP_NAME}"
        
          AWS_ACCESS_KEY_ID=
          AWS_SECRET_ACCESS_KEY=
          AWS_DEFAULT_REGION=us-east-1
          AWS_BUCKET=
        
          PUSHER_APP_ID=
          PUSHER_APP_KEY=
          PUSHER_APP_SECRET=
          PUSHER_APP_CLUSTER=mt1
        
          MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
          MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
        
      • 修正後(「# 下記を修正する」の文字は加筆しない。)

          # 下記を修正する
          APP_NAME=Laravel
          APP_ENV=local
          APP_KEY=base64:Tx/K6tIRwmdux4Z3zCV3rmMCxaF0SCJATs7E7sKLLfg=
          APP_DEBUG=true
          APP_URL=http://localhost
        
          LOG_CHANNEL=stack
        
          DB_CONNECTION=mysql
          DB_HOST=127.0.0.1
          DB_PORT=3306
               # 下記を修正する
          DB_DATABASE=test
          DB_USERNAME=root
               # 下記を修正する
          DB_PASSWORD=MySQLのrootユーザのパスワード
        
          BROADCAST_DRIVER=log
          CACHE_DRIVER=file
          QUEUE_CONNECTION=sync
          SESSION_DRIVER=file
          SESSION_LIFETIME=120
        
          REDIS_HOST=127.0.0.1
          REDIS_PASSWORD=null
          REDIS_PORT=6379
        
          MAIL_MAILER=smtp
          MAIL_HOST=smtp.mailtrap.io
          MAIL_PORT=2525
          MAIL_USERNAME=null
          MAIL_PASSWORD=null
          MAIL_ENCRYPTION=null
          MAIL_FROM_ADDRESS=null
          MAIL_FROM_NAME="${APP_NAME}"
        
          AWS_ACCESS_KEY_ID=
          AWS_SECRET_ACCESS_KEY=
          AWS_DEFAULT_REGION=us-east-1
          AWS_BUCKET=
        
          PUSHER_APP_ID=
          PUSHER_APP_KEY=
          PUSHER_APP_SECRET=
          PUSHER_APP_CLUSTER=mt1
        
          MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
          MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
        
    9. 下記コマンドを実行してlaravelアプリ名ディレクトリの権限を777に設定する。

       $ sudo chmod 777 /var/www/test
      
    10. 下記コマンドを実行して/testディレクトリ直下の.envの権限を755に変更する。

       $ sudo chmod 777 /var/www/test/.env
      
    11. 下記コマンドを実行してアプリキーを作成する。

       $ php artisan key:generate
      
    12. 下記コマンドを実行してキャッシュクリアを行う。

       $ php artisan config:clear
      
  9. 確認
    1. 下記コマンドを実行してApacheのドキュメントルートのオーナーを変更する。

       $ sudo chown -R apache:apache /var/www/test
      
    2. ブラウザを用いて下記にアクセスして下記の画面が出ることを確認する。

      f:id:miriwo:20200725140203p:plain

    3. 下記コマンドを実行してマイグレーションを実行する。

       $ php artisan migrate
      
    4. ブラウザにて「REGISTER」をクリックしユーザ情報を入力し初回登録を行う。

      f:id:miriwo:20200725140530p:plain

    5. 下記のページが表示されれば作業完了である。

      f:id:miriwo:20200725140607p:plain

問題

下記問題発生

[shun0104@localhost laravel6]$ composer install Installing dependencies from lock file (including require-dev) Verifying lock file contents can be installed on current platform. Nothing to install, update or remove

[ErrorException]
file_put_contents(/var/www/laravel6/vendor/composer/installed.php): failed to open stream: Permission denied

install [--prefer-source] [--prefer-dist] [--dry-run] [--dev] [--no-suggest] [--no-dev] [--no-autoloader] [--no-scripts] [--no-progress] [--no-install] [-v|vv|vvv|--verbose] [-o|--optimize-autoloader] [-a|--classmap-authoritative] [--apcu-autoloader] [--apcu-autoloader-prefix APCU-AUTOLOADER-PREFIX] [--ignore-platform-req IGNORE-PLATFORM-REQ] [--ignore-platform-reqs] [--] []...

[shun0104@localhost laravel6]$ cd vendor/composer/ [shun0104@localhost composer]$ ll 合計 1472 -rw-rw-r-- 1 apache apache 13475 1月 27 09:45 ClassLoader.php -rw-rw-r-- 1 apache apache 28990 1月 27 09:45 InstalledVersions.php -rw-rw-r-- 1 apache apache 1070 1月 27 09:45 LICENSE -rw-rw-r-- 1 apache apache 549362 1月 27 09:45 autoload_classmap.php -rw-rw-r-- 1 apache apache 2249 1月 27 09:45 autoload_files.php -rw-rw-r-- 1 apache apache 355 1月 27 09:45 autoload_namespaces.php -rw-rw-r-- 1 apache apache 5413 1月 27 09:45 autoload_psr4.php -rw-rw-r-- 1 apache apache 2521 1月 27 09:45 autoload_real.php -rw-rw-r-- 1 apache apache 603708 1月 27 09:45 autoload_static.php -rw-rw-r-- 1 apache apache 238330 1月 27 09:45 installed.json -rw-rw-r-- 1 apache apache 26131 1月 27 09:45 installed.php -rw-rw-r-- 1 apache apache 925 1月 27 09:45 platform_check.php

アプリ名ディレクトリ/composer のディレクトリ配下を権限777にしてみる

[shun0104@localhost composer]$ sudo chmod 777 * [sudo] shun0104 のパスワード: [shun0104@localhost composer]$ ll 合計 1472 -rwxrwxrwx 1 apache apache 13475 1月 27 09:45 ClassLoader.php -rwxrwxrwx 1 apache apache 28990 1月 27 09:45 InstalledVersions.php -rwxrwxrwx 1 apache apache 1070 1月 27 09:45 LICENSE -rwxrwxrwx 1 apache apache 549362 1月 27 09:45 autoload_classmap.php -rwxrwxrwx 1 apache apache 2249 1月 27 09:45 autoload_files.php -rwxrwxrwx 1 apache apache 355 1月 27 09:45 autoload_namespaces.php -rwxrwxrwx 1 apache apache 5413 1月 27 09:45 autoload_psr4.php -rwxrwxrwx 1 apache apache 2521 1月 27 09:45 autoload_real.php -rwxrwxrwx 1 apache apache 603708 1月 27 09:45 autoload_static.php -rwxrwxrwx 1 apache apache 238330 1月 27 09:45 installed.json -rwxrwxrwx 1 apache apache 26131 1月 27 10:38 installed.php -rwxrwxrwx 1 apache apache 925 1月 27 09:45 platform_check.php

composer installは実行できた。

composer updateでエラー発生

[shun0104@localhost laravel6]$ ls -la 合計 736 drwxrwxrwx 14 apache apache 4096 1月 27 09:44 . drwxrwxrwx 6 apache apache 61 1月 27 09:10 .. -rw-rw-r-- 1 apache apache 220 1月 27 09:10 .editorconfig -rwxrwxrwx 1 apache apache 853 1月 27 10:01 .env -rw-rw-r-- 1 apache apache 778 1月 27 09:10 .env.example drwxrwxr-x 8 apache apache 163 1月 27 09:10 .git -rw-rw-r-- 1 apache apache 111 1月 27 09:10 .gitattributes -rw-rw-r-- 1 apache apache 163 1月 27 09:10 .gitignore -rw-rw-r-- 1 apache apache 174 1月 27 09:10 .styleci.yml drwxrwxr-x 2 apache apache 25 1月 27 09:10 .vscode -rw-rw-r-- 1 apache apache 4542 1月 27 09:10 README.md drwxrwxr-x 6 apache apache 84 1月 27 09:10 app -rwxrwxr-x 1 apache apache 1686 1月 27 09:10 artisan drwxrwxr-x 3 apache apache 34 1月 27 09:10 bootstrap -rw-rw-r-- 1 apache apache 1551 1月 27 09:10 composer.json -rw-rw-r-- 1 apache apache 223757 1月 27 09:10 composer.lock drwxrwxr-x 2 apache apache 247 1月 27 09:10 config drwxrwxr-x 5 apache apache 72 1月 27 09:10 database -rw-rw-r-- 1 apache apache 459707 1月 27 09:10 package-lock.json -rw-rw-r-- 1 apache apache 1161 1月 27 09:10 package.json -rw-rw-r-- 1 apache apache 1140 1月 27 09:10 phpunit.xml drwxrwxr-x 4 apache apache 141 1月 27 09:10 public drwxrwxr-x 6 apache apache 53 1月 27 09:10 resources drwxrwxr-x 2 apache apache 75 1月 27 09:10 routes -rw-rw-r-- 1 apache apache 563 1月 27 09:10 server.php drwxrwxrwx 5 apache apache 46 1月 27 09:10 storage drwxrwxr-x 4 apache apache 83 1月 27 09:10 tests drwxrwxr-x 40 apache apache 4096 1月 27 09:45 vendor -rw-rw-r-- 1 apache apache 537 1月 27 09:10 webpack.mix.js [shun0104@localhost laravel6]$ composer update Loading composer repositories with package information Updating dependencies Lock file operations: 0 installs, 16 updates, 0 removals - Upgrading filp/whoops (2.9.1 => 2.9.2) - Upgrading laravel/framework (v6.20.9 => v6.20.15) - Upgrading laravel/tinker (v2.5.0 => v2.6.0) - Upgrading league/mime-type-detection (1.5.1 => 1.7.0) - Upgrading phpunit/phpunit (9.5.0 => 9.5.1) - Upgrading psy/psysh (v0.10.5 => v0.10.6) - Upgrading swiftmailer/swiftmailer (v6.2.4 => v6.2.5) - Upgrading symfony/polyfill-ctype (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-iconv (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-intl-idn (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-intl-normalizer (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-mbstring (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-php72 (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-php73 (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-php80 (v1.20.0 => v1.22.0) - Upgrading vlucas/phpdotenv (v3.6.7 => v3.6.8)

[ErrorException]
file_put_contents(./composer.lock): failed to open stream: Permission denied

当該ファイルの権限を777に修正してみる。

違うエラー出た

[shun0104@localhost laravel6]$ sudo chmod 777 composer.lock [sudo] shun0104 のパスワード: [shun0104@localhost laravel6]$ sudo chmod 777 composer.lock [shun0104@localhost laravel6]$ composer update Loading composer repositories with package information Updating dependencies Lock file operations: 0 installs, 16 updates, 0 removals - Upgrading filp/whoops (2.9.1 => 2.9.2) - Upgrading laravel/framework (v6.20.9 => v6.20.15) - Upgrading laravel/tinker (v2.5.0 => v2.6.0) - Upgrading league/mime-type-detection (1.5.1 => 1.7.0) - Upgrading phpunit/phpunit (9.5.0 => 9.5.1) - Upgrading psy/psysh (v0.10.5 => v0.10.6) - Upgrading swiftmailer/swiftmailer (v6.2.4 => v6.2.5) - Upgrading symfony/polyfill-ctype (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-iconv (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-intl-idn (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-intl-normalizer (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-mbstring (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-php72 (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-php73 (v1.20.0 => v1.22.0) - Upgrading symfony/polyfill-php80 (v1.20.0 => v1.22.0) - Upgrading vlucas/phpdotenv (v3.6.7 => v3.6.8) Writing lock file Installing dependencies from lock file (including require-dev) Package operations: 0 installs, 16 updates, 0 removals

[ErrorException]
copy(/var/www/laravel6/vendor/composer/tmp-a8fe695d7f259a4fffc4451c48ae923e): failed to open stream: Permission denied

vendor/composer直下のオーナーをshun0104に修正してみる。

違うエラーが出た [RuntimeException]
Could not delete /var/www/laravel6/vendor/symfony/polyfill-php80/LICENSE:

vendor直下はオーナーを変更しないのかもしれない