ID , 'noindex' , true)){ echo ''; } ?>

(MacBook) MAMPを卒業して、phpローカル開発環境を一から構築してみた【後編】

MAMPを卒業してMacのローカル開発環境構築(後半)

Macのローカル環境にMAMPを使わず、
PHPの開発環境を整備していく手順を紹介する(後半)。

前半では、Homebrewで必要なパッケージ(apache、mysql、php)をインストールし、自動起動設定をやるところまで実施した。後半は、上記のパッケージに対して必要な設定の手順を紹介していく。

前編
  • Homebrewのインストール
  • apacheのインストール
  • mysqlのインストール
  • phpのインストール
  • 自動起動設定(apache + mysql)

後編(今回)

後編
  • apacheの設定変更
  • mysqlの初期設定

あと+αで


 

apache関連の設定変更

今回homebrewでインストールしたのはバージョン2.4のapache

apache-version

 

httpd.conf(apacheの設定ファイル)の設定変更

apacheの設定ファイルは以下の場所にある(homebrewでインストールしたやつ)

/usr/local/etc/httpd/httpd.conf

 

こいつをviコマンドとかで変更していく。バックアップとかはとって置いて。

vi /usr/local/etc/httpd/httpd.conf

 

変更する点は以下

#httpd.confに追記 

#php7の共有モジュール読み込み(今回インストールしたのはphp7.3)


 LoadModule php7_module /usr/local/Cellar/php/7.3.9_1/lib/httpd/modules/libphp7.so

<IfModule php7_module>

 PHPIniDir /usr/local/etc/php/7.3/

</IfModule>

 

# httpd.confのDirectoryIndexに、index.phpを追記 



DirectoryIndex index.php index.html

 

# Virtual hosts のコメントアウトを解除 



Include /usr/local/etc/apache2/2.4/extra/httpd-vhosts.conf

 

# .php で php を実行できるように MIME タイプの追記 ( IfModule mime_moduleディレクティブ 内 )



AddType application/x-httpd-php .php

 

httpd-vhosts.conf(仮想ホストの設定ファイル)の設定変更

httpd-vhosts.confの場所は以下

/usr/local/etc/httpd/extra/httpd-vhosts.conf

 

こいつをviコマンドとかで変更していく。

#以下のディレクティブを追記(ドキュメントルートとかは自分の環境に合わせる)

 

<VirtualHost localhost:8080>

    DocumentRoot "/usr/local/var/www"

    ServerName localhost
 
    <Directory "/usr/local/var/www">

        Require all granted
    
 </Directory>

</VirtualHost>

 

ここまでやればとり合えずapache関連の変更は終了、apacheを再起動して変更を読み込む。そもそもスタートしていない場合は立ち上げる

#apache再起動


brew services restart http

#apache起動
brew services start http

 

そしたら試しに「localhost:8080」とブラウザにアクセスしてほしい。
「It works!」と出れば正しくapacheが起動されている。これはデフォルトのindex.html。

Itworks!

phpが正しく動いているかは、なんでも良いがphpファイルをドキュメントルートに置いて「localhost:8080//phpファイル名」でブラウザからアクセスしてみて欲しい。

とりあえず自分の場合はよくある「<?php phpinfo(); ?>」がだけが書き込まれた、phpinfo.phpファイルを置いてアクセス

phpinfoが表示された画像

 

こんなのが出ればちゃんとphpが動いている。

もしphpがちゃんと動いていないのであれば、phpファイルに書き込んだphpソースがテキストで画面にそのまま表示されてしまう。

このへんは上記apache設定手順の、「php7の共有モジュール読み込み」ってとこをちゃんとhttpd.confに追記できているかが怪しいので要確認

 

mysqlの初期設定

mysql_secure_installaiton

mysqlは前編でインストール済み

最初の設定として、mysql_secure_installaitonというコマンドで、最低限のセキュリティ設定をしていく。

と言ってもローカルで開発するのにセキュリティはあんま重要じゃない。
一応、本当のサーバ(運用してるやつとか)はやった方がいいので、勉強も兼ねて実施

rootのパスワードを設定するので、ちゃんとメモするなり覚えておくこと

$ mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD COMPONENT can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD component?

Press y|Y for Yes, any other key for No: y # パスワード強度チェッカーを有効化するかどうか

There are three levels of password validation policy:

LOW    Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 # パスワード強度の選択(低, 中, 高)
Please set the password for root here.

New password: # rootパスワードの設定

Re-enter new password: # rootパスワードの設定(確認用)

Estimated strength of the password: 50 
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : y # 設定したパスワードを確定するかどうか
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.

Remove anonymous users? (Press y|Y for Yes, any other key for No) : y # 匿名ユーザを削除するかどうか
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.

Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y # リモートからrootユーザでログインできないようにするかどうか
Success.

By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.


Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y # testデータベースを削除するかどうか
 - Dropping test database...
Success.

 - Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y # 変更を反映するために権限テーブルをリロードするかどうか
Success.

All done!

 

mysqlログイン

終わったらmysqlにコマンドからログインしてみる。

mysql -u roo -p[パスワード]

 

こんなのが出てきたらok。mysqlにログインできている状態

 

ちなみにmysqlの立ち上げコマンド、止めるコマンド、再起動コマンド(homebrewでインストールしたやつ)は以下

#立ち上げ
brew services start mysql

#ストップ
brew services stop mysql

#再起動
brew services restart mysql

 

前編も含めて以上でMAMPの環境構築は完了

環境構築でしたこと
  • Homebrewのインストール
  • apacheのインストール
  • mysqlのインストール
  • phpのインストール
  • 自動起動設定(apache + mysql)
  • apacheの設定変更
  • mysqlの初期設定

 

ここまでくるのにいくつもエラーが出て、まあまあ大変だった

あとは各サービス(apache + mysql + php) が立ち上がっていることを確認して、ドキュメントルートに自分で開発したいものを作って置けば、ブラウザからアクセスできる。

ちなみにhomebrewでインストールしたもののサービスの起動状態を確認するコマンドは以下

//サービスの起動状態を確認

brew services list

 

httpd(apache)、mysql、phpが起動状態。

手順はここまで

以下は+αとして、mysqlをブラウザから簡単に操作できるphpmyadminインストール手順を紹介

install-phpadmin

 

 

 

記事が気に入った方はシェアをお願いします!

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です