当サイトはMacにインストールしたMAMP&Wordpressで構築しておりましたが、Python3.7でWSGIやりたいのにうまくいかなかったので、MAMP依存をやめて1からサイトを構築することにしました。
結構手間がかかったのでメモです。
0. 前提
- サーバ機: Mac mini 2018
- OS: MacOS 10.14.4 Mojave
- Python 3.7.3 (環境構築方法)
- Command Line Tools
- Git
- Homebrew
- WordPressのMySQLデータベースはエキスポート済み
- htdocsをまるごと再利用
1. Apacheのインストール
1 2 3 4 5 6 7 8 9 10 11 12 13 | $ brew install httpd (中略) DocumentRoot is /usr/local/var/www. The default ports have been set in /usr/local/etc/httpd/httpd.conf to 8080 and in /usr/local/etc/httpd/extra/httpd-ssl.conf to 8443 so that httpd can run without sudo. To have launchd start httpd now and restart at login: brew services start httpd Or, if you don't want/need a background service you can just run: apachectl start |
2. PHPのインストール
2019.5.3追記
↓でインストールしたPHPがWordpressですこぶる調子悪いのでPHPBrewでインストールし直しました。
Homebrewでのインストールをやめて、PHPBrewでインストールしましょう。
追記終わり
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | $ brew install php (中略) To enable PHP in Apache add the following to httpd.conf and restart Apache: LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch> Finally, check DirectoryIndex includes index.php DirectoryIndex index.php index.html The php.ini and php-fpm.ini file can be found in: /usr/local/etc/php/7.3/ To have launchd start php now and restart at login: brew services start php Or, if you don't want/need a background service you can just run: php-fpm |
ApacheでPHPを使うときの設定を教えてくれますが、今のところは後回し。
まず、php.iniの次の行をいじります。後ほどインストールするphpMyAdminでインポート、エキスポートするファイルサイズを大きくするためです。
1 2 3 | memory_limit = 128M upload_max_filesize = 128M post_max_size = 128M |
3. MySQLのインストール
MAMPのMySQLのバージョンに近い5.7をインスールします。
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 | $ brew install mysql@5.7 (中略) We've installed your MySQL database without a root password. To secure it run: mysql_secure_installation MySQL is configured to only allow connections from localhost by default To connect run: mysql -uroot mysql@5.7 is keg-only, which means it was not symlinked into /usr/local, because this is an alternate version of another formula. If you need to have mysql@5.7 first in your PATH run: echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile For compilers to find mysql@5.7 you may need to set: export LDFLAGS="-L/usr/local/opt/mysql@5.7/lib" export CPPFLAGS="-I/usr/local/opt/mysql@5.7/include" For pkg-config to find mysql@5.7 you may need to set: export PKG_CONFIG_PATH="/usr/local/opt/mysql@5.7/lib/pkgconfig" To have launchd start mysql@5.7 now and restart at login: brew services start mysql@5.7 Or, if you don't want/need a background service you can just run: /usr/local/opt/mysql@5.7/bin/mysql.server start ==> Summary 🍺 /usr/local/Cellar/mysql@5.7/5.7.25: 319 files, 234MB |
パスを通して、MySQLを起動します。
1 2 3 | echo 'export PATH="/usr/local/opt/mysql@5.7/bin:$PATH"' >> ~/.bash_profile source ~/.bash_profile mysql.server start |
セキュリティー向上のための設定です。
rootのパスワードの設定を行います。それ以外の設定は全て’y’
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 | $ mysql_secure_installation Securing the MySQL server deployment. Connecting to MySQL using a blank password. VALIDATE PASSWORD PLUGIN 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 plugin? 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: Re-enter new password: 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 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 - 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! |
一旦サーバを終了します
1 | mysql.server stop |
4. phpMyAdminのインストール
Apachの設定を教えてくれますが後回し。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | $ brew install phpmyadmin ==> Downloading https://files.phpmyadmin.net/phpMyAdmin/4.8.5/phpMyAdmin-4.8.5-a ######################################################################## 100.0% ==> Caveats To enable phpMyAdmin in Apache, add the following to httpd.conf and restart Apache: Alias /phpmyadmin /usr/local/share/phpmyadmin <Directory /usr/local/share/phpmyadmin/> Options Indexes FollowSymLinks MultiViews AllowOverride All <IfModule mod_authz_core.c> Require all granted </IfModule> <IfModule !mod_authz_core.c> Order allow,deny Allow from all </IfModule> </Directory> Then open http://localhost/phpmyadmin The configuration file is /usr/local/etc/phpmyadmin.config.inc.php ==> Summary 🍺 /usr/local/Cellar/phpmyadmin/4.8.5: 2,438 files, 32.3MB, built in 10 seconds |
5. mod_wsgiのインストール
ApacheからPythonスクリプトを実行できるように、mod_wsgiを導入します。
Pythonのバージョンが3.7.3であることに注目。
この環境下でmakeするとPython 3.7.3で動作するmod_wsgiが出来上がります。
リポジトリのCloneからインストールまでの手順。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $ python --version Python 3.7.3 $ git clone https://github.com/GrahamDumpleton/mod_wsgi.git cd mod_wsgi ./configure make make install # 違うディレクトリにインストールされるので移動 mv /usr/local/lib/httpd/modules/mod_wsgi.so /usr/local/opt/httpd/lib/httpd/modules # パーミッションを他のモジュールに合わせる chmod 444 /usr/local/opt/httpd/lib/httpd/modules/mod_wsgi.so |
/usr/local/opt/httpd/lib/httpd/modules/mod_wsgi.so
がインストールされました。
6. Apacheの設定
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 | # (注意)変更箇所の抜粋です。 # モジュールを有効化 LoadModule socache_shmcb_module lib/httpd/modules/mod_socache_shmcb.so LoadModule ssl_module lib/httpd/modules/mod_ssl.so LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so # サーバ名とポート番号 ServerName localhost:8080 #ドキュメントルートの設定 DocumentRoot "/Users/webmaster/webApp/apache/htdocs" <Directory "/Users/webmaster/webApp/apache/htdocs"> # ディレクトリの中身が見えないようにする Options All -Indexes # .htaccess有効 AllowOverride All </Directory> # URLを補完してくれる <IfModule dir_module> DirectoryIndex index.php index.html </IfModule> # Macの隠しファイル(.DS_Store)へのアクセス禁止 <Files ~ "^\.DS_Store"> Require all denied </Files> # .で始まるファイルを表示させない RedirectMatch 404 /\..*$ <IfModule alias_module> # phpMyAdminのエイリアス # http://localhost:8080/phpmyadmin Alias /phpmyadmin /usr/local/share/phpmyadmin <Directory /usr/local/share/phpmyadmin/> Options Indexes FollowSymLinks MultiViews AllowOverride All <IfModule mod_authz_core.c> Require local </IfModule> <IfModule !mod_authz_core.c> Order allow,deny Allow from localhost </IfModule> </Directory> </IfModule> # 使わないけど一応変更しておいた <Directory "/Users/webmaster/webApp/apache/cgi-bin"> AllowOverride None Options None Require all granted </Directory> # バーチャルホスト設定ファイル読み込み Include /usr/local/etc/httpd/extra/httpd-vhosts.conf # SSL設定ファイル読み込み Include /usr/local/etc/httpd/extra/httpd-ssl.conf # PHP 7.3.4 LoadModule php7_module /usr/local/opt/php/lib/httpd/modules/libphp7.so <FilesMatch \.php$> SetHandler application/x-httpd-php </FilesMatch> # mod_wsgi (Python 3.7.3) LoadModule wsgi_module lib/httpd/modules/mod_wsgi.so |
1 2 3 4 5 6 7 8 | <VirtualHost *:8080> ServerAdmin webmaster@blue-black.ink DocumentRoot "/Users/webmaster/webApp/apache/htdocs/blue-black.ink" ServerName blue-black.ink ServerAlias www.blue-black.ink ErrorLog "/usr/local/var/log/httpd/blue-black.ink_error_log" CustomLog "/usr/local/var/log/httpd/blue-black.ink_access_log" common </VirtualHost> |
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 | Listen 8443 SSLCipherSuite HIGH:MEDIUM:!MD5:!RC4:!3DES SSLProxyCipherSuite HIGH:MEDIUM:!MD5:!RC4:!3DES SSLHonorCipherOrder on SSLProtocol all -SSLv3 SSLProxyProtocol all -SSLv3 SSLPassPhraseDialog builtin SSLSessionCache "shmcb:/usr/local/var/run/httpd/ssl_scache(512000)" SSLSessionCacheTimeout 300 <VirtualHost _default_:8443> DocumentRoot "/Users/webmaster/webApp/apache/htdocs/blue-black.ink" ServerName blue-black.ink ServerAdmin webmaster@blue-black.ink ErrorLog "/usr/local/var/log/httpd/blue-black.ink_error_log" TransferLog "/usr/local/var/log/httpd/blue-black.ink_access_log" SSLEngine on SSLCertificateFile "/etc/letsencrypt/live/blue-black.ink/cert.pem" SSLCertificateKeyFile "/etc/letsencrypt/live/blue-black.ink/privkey.pem" SSLCertificateChainFile "/etc/letsencrypt/live/blue-black.ink/chain.pem" <FilesMatch "\.(cgi|shtml|phtml|php)$"> SSLOptions +StdEnvVars </FilesMatch> <Directory "/Users/webmaster/webApp/apache/cgi-bin" /> SSLOptions +StdEnvVars </Directory> BrowserMatch "MSIE [2-5]" \ nokeepalive ssl-unclean-shutdown \ downgrade-1.0 force-response-1.0 CustomLog "/usr/local/var/log/httpd/ssl_request_log" \ "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" </VirtualHost> |
7. サーバ起動&データ移行
サーバを起動します。
もしエラーが発生したら設定を見直します。
1 2 | mysql.server start apachectl start |
localhost:8080/phpmyadmin
にアクセスします。
phpMyAdminのログインページが開いたらゴールは間もなく。うまくいかない場合は設定を見直します。
rootでログインしてWordpress用のユーザ及びデータベースを作成し、データをインポートします。
やり方は省略。
MAMPで使っていたhtdocs内のデータをまるごと移動させます。
cgi置き場であるcgi-binも作ります。
1 2 3 4 5 6 | # htdocsをまるごと移動する mkdir -p /Users/webmaster/webApp/apache/ cp -R /Applications/MAMP/htdocs /Users/webmaster/webApp/apache/ # (使わないけど)cgi-binを作成 mkdir /Users/webmaster/webApp/apache/cgi-bin |
WordPressの設定ファイルwp-config.phpを編集して、新しいMySQL環境に合わせます。
MySQLが使用するポートが8889から3906に変更になりましたので忘れずに。
1 2 3 4 | define('DB_NAME', 'databasename'); define('DB_USER', 'databaseuser'); define('DB_PASSWORD', 'databasepassword'); define('DB_HOST', 'localhost:3906'); |
8. ルータの設定変更
ルータのポート設定を変更しましょう。
- TCP80番(HTTP)→サーバ機のTCP8080番
- TCP443番(HTTPS)→サーバ機のTCP8443番
9. サーバ起動&テスト
1 2 | mysql.server start apachectl start |
https://blue-black.ink/
へアクセスするとちゃんと表示できました。やったね!
10. サーバ自動起動
Macならlaunchdを使うのが定番。Homebrewは、plistファイルの作成とloadを全自動でやってくれるので便利です。
1 2 3 4 5 | $ brew services start mysql@5.7 ==> Successfully started `mysql@5.7` (label: homebrew.mxcl.mysql@5.7) $ brew services start httpd ==> Successfully started `httpd` (label: homebrew.mxcl.httpd) |
これで、ログインするとサーバが自動起動するようになりました。
以上で完了です。