見出し画像

FreeBSDサーバの上で1からWordPressをインストールする話

今どき自宅サーバなんて流行らない!という向きも多いかと思いますけれども、やはり自分の腕次第で如何様にもセットアップできるという自鯖の柔軟性・拡張性は捨てたものではないと思っています。また、近年は安いコストで高品質な回線を引くことができますから、自宅のほかに例えば実家とか別荘にも省電力サーバを置き、データをセルフ分散バックアップさせることも容易です。
実際に私は2か所のサーバを同期させて運用中ですが、今回はそのサーバの上でWordPressをゴリゴリインストールする方法について自分向けメモの意味も込めて記録しておこうと思う次第。

Apacheとmysqlのインストール

昔は何でもかんでもソース(tar.gz)から展開してconfigure、make、make installをやっておりましたが、流石にもう十分だろうということでpkgのお世話になっております。

pkg install apache
pkg install mysql
pkg install php

なんて感じでやればOK。但し、これだけではWordPressは動きませんので、適宜php74-mysqliだとか色々必要です。
ちなみに現在稼働している状態のpkg info は次の通りです。

# pkg info
apache24-2.4.52 Version 2.4.x of Apache web server
apr-1.7.0.1.6.1_1 Apache Portability Library
augeas-1.12.0_1 Configuration editing tool
binutils-2.37_2,1 GNU binary tools
ca_root_nss-3.71 Root certificate bundle from the Mozilla Project
curl-7.80.0 Command line tool and library for transferring data with URLs
cyrus-sasl-2.1.27_2 RFC 2222 SASL (Simple Authentication and Security Layer)
db5-5.3.28_8 Oracle Berkeley DB, revision 5.3
dialog4ports-0.1.6_1 Console Interface to configure ports
expat-2.4.4 XML 1.0 parser written in C
fontconfig-2.13.94_1,1 XML-based font configuration API for X Windows
freetds-1.3.4,1 Sybase/Microsoft TDS protocol library
freetype2-2.11.1 Free and portable TrueType font rendering engine
gcc-10_4 Meta-port for the default version of the GNU Compiler Collection
gcc10-10.3.0 GNU Compiler Collection 10
gdbm-1.22 GNU database manager
gettext-runtime-0.21 GNU gettext runtime libraries and programs
giflib-5.2.1 Tools and library routines for working with GIF images
gmp-6.2.1 Free library for arbitrary precision arithmetic
groff-1.22.4_4 Software typesetting package
icu-70.1_1,1 International Components for Unicode (from IBM)
indexinfo-0.3.1 Utility to regenerate the GNU info page index
jansson-2.14 C library for encoding, decoding, and manipulating JSON data
jbigkit-2.1_1 Lossless compression for bi-level images such as scanned pages, faxes
jpeg-turbo-2.1.1_1 SIMD-accelerated JPEG codec which replaces libjpeg
libargon2-20190702 Memory hard password hashing program and library
libedit-3.1.20210216,1 Command line editor library
libevent-2.1.12 API for executing callback functions on events or timeouts
libffi-3.3_1 Foreign Function Interface
libgcrypt-1.9.4 General purpose cryptographic library based on the code from GnuPG
libgd-2.3.1,1 Graphics library for fast creation of images
libgpg-error-1.43 Common error values for all GnuPG components
libiconv-1.16 Character set conversion library
liblz4-1.9.3,1 LZ4 compression library, lossless and very fast
libnghttp2-1.46.0 HTTP/2.0 C Library
libpaper-1.1.28 Library providing routines for paper size management
libssh2-1.10.0,3 Library implementing the SSH2 protocol
libunwind-20201110 Generic stack unwinding library
libxml2-2.9.12 XML parser library for GNOME
mod_php74-7.4.27 PHP Scripting Language
mpc-1.2.1 Library of complex numbers with arbitrarily high precision
mpdecimal-2.5.1 C/C++ arbitrary precision decimal floating point libraries
mpfr-4.1.0_1 Library for multiple-precision floating-point computations
mysql80-client-8.0.27 Multithreaded SQL database (client)
mysql80-server-8.0.27 Multithreaded SQL database (server)
oniguruma-6.9.7.1 Regular expressions library compatible with POSIX/GNU/Perl
openldap24-client-2.4.59_4 Open source LDAP client implementation
pcre-8.45 Perl Compatible Regular Expressions library
pcre2-10.39 Perl Compatible Regular Expressions library, version 2
perl5-5.32.1_1 Practical Extraction and Report Language
php74-7.4.27 PHP Scripting Language
php74-ctype-7.4.27 The ctype shared extension for php
php74-curl-7.4.27 The curl shared extension for php
php74-dom-7.4.27 The dom shared extension for php
php74-filter-7.4.27 The filter shared extension for php
php74-gd-7.4.27 The gd shared extension for php
php74-json-7.4.27 The json shared extension for php
php74-mbstring-7.4.27 The mbstring shared extension for php
php74-mysqli-7.4.27 The mysqli shared extension for php
php74-openssl-7.4.27 The openssl shared extension for php
php74-pdo-7.4.27 The pdo shared extension for php
php74-pdo_dblib-7.4.27 The pdo_dblib shared extension for php
php74-pdo_mysql-7.4.27 The pdo_mysql shared extension for php
php74-pdo_sqlite-7.4.27 The pdo_sqlite shared extension for php
php74-pear-1.10.12 PEAR framework for PHP
php74-pear-MDB2-2.5.0.b5 PEAR database abstraction layer
php74-pear-MDB2_Driver_mysqli-1.5.0.b4 PEAR MySQL MDB2 driver
php74-pear-channel-horde-20160602 PEAR channel pear.horde.org
php74-pear-horde-Horde_Util-2.5.9 Horde Utility Libraries
php74-pear-horde-Horde_Xml_Wbxml-2.0.4 Horde API for encoding and decoding WBXML documents
php74-session-7.4.27 The session shared extension for php
php74-simplexml-7.4.27 The simplexml shared extension for php
php74-xml-7.4.27 The xml shared extension for php
php74-xmlreader-7.4.27 The xmlreader shared extension for php
php74-xmlrpc-7.4.27 The xmlrpc shared extension for php
php74-xmlwriter-7.4.27 The xmlwriter shared extension for php
php74-zlib-7.4.27 The zlib shared extension for php
phpfpm_exporter-0.4.1 Export php-fpm metrics in Prometheus format
pkg-1.17.5 Package manager
png-1.6.37_1 Library for manipulating PNG images
protobuf-3.17.3,1 Data interchange format library
psutils-1.17_5 Utilities for manipulating PostScript documents
py38-acme-1.22.0,1 ACME protocol implementation in Python
py38-certbot-1.22.0,1 Let's Encrypt client
py38-certbot-apache-1.22.0 Apache plugin for Certbot
py38-certifi-2021.10.8 Mozilla SSL certificates
py38-cffi-1.15.0 Foreign Function Interface for Python calling C code
py38-chardet-4.0.0,1 Universal encoding detector for Python
py38-configargparse-1.5.3 Drop-in replacement for argparse
py38-configobj-5.0.6_1 Simple but powerful config file reader and writer
py38-cryptography-3.3.2 Cryptographic recipes and primitives for Python developers
py38-distro-1.6.0 Linux OS platform information API
py38-idna-2.10 Internationalized Domain Names in Applications (IDNA)
py38-josepy-1.10.0 JOSE protocol implementation in Python
py38-openssl-20.0.1 Python interface to the OpenSSL library
py38-parsedatetime-2.6 Python module for parsing 'human readable' date/time expressions
py38-pycparser-2.21 C parser in Python
py38-pyrfc3339-1.1 Generate and parse RFC 3339 timestamps
py38-pysocks-1.7.1 Python SOCKS module
py38-python-augeas-1.0.3 Pure python bindings for Augeas
py38-pytz-2021.3,1 World Timezone Definitions for Python
py38-requests-2.25.1 HTTP library written in Python for human beings
py38-requests-toolbelt-0.9.1_1 Utility belt for advanced users of python-requests
py38-setuptools-57.0.0 Python packages installer
py38-six-1.16.0 Python 2 and 3 compatibility utilities
py38-urllib3-1.26.7,1 HTTP library with thread-safe connection pooling, file post, and more
py38-zope.component-4.2.2 Zope Component Architecture
py38-zope.event-4.1.0 Very basic event publishing system
py38-zope.interface-5.3.0 Interfaces for Python
python38-3.8.12_1 Interpreted object-oriented programming language
readline-8.1.1 Library for editing command lines as they are typed
sqlite3-3.35.5_4,1 SQL database engine in a C library
tiff-4.3.0 Tools and library routines for working with TIFF images
uchardet-0.0.7 Universal charset detection library
unixODBC-2.3.9 ODBC library suite for Unix
webp-1.2.1 Google WebP image format conversion tool
zstd-1.5.0 Fast real-time compression algorithm

/etc/rc.confは次のような感じ。

apache24_enable="YES"
clear_tmp_enable="YES"
mysql_enable="YES"
sshd_enable="YES"

Apacheの設定

Apacheは、phpが使えるように普通に設定すればOK。バーチャルホストにするなら/usr/local/etc/apache24/extra/httpd-vhosts.conf を編集する、みたいな感じで普通にやればおっけー。letsencryptでhttps化しているとかあるのであれば、それに合わせてSSLの設定をすればおおけー。
お約束ですがindex.phpが正常に扱われるように↓の設定とかも。

DirectoryIndex index.php index.html
AddType application/x-httpd-php .php

DNSの設定

こちらも、必要に応じて設定すればおk。
私の場合は、kemanai.jpとannex.kemanai.jpという二本建てでやっていますので、それに合わせてアクセスできるようにすればいいってことです。

ディレクトリの設定とwordpressの展開

httpd*.confで設定したとおりにディレクトリを作り、権限を設定して、apacheで設定したweb空間のrootディレクトリにwordpressのtar.gzを展開します。i

まずはhttpdからのアクセス確認

ここまで来たら、ブラウザ経由でwordpressのindex.phpにアクセスでき、初期画面が出るかどうかの確認です。apache内のphpモジュールが正常に動作していないとか、DirectoryIndexの設定出来てないとか、何か問題があればこの段階で正常に初期画面が出てこないはずです。

ここまではそう難しい話じゃないんですが、いよいよ本丸のmysqlの設定などに入りますよ。

MySQLの設定

まずroot権限でmysqlにアクセスします。

# mysql -u root -p

rootのままでは余りにアレなので、wordpressからアクセスするユーザを作成します。これは別にシステムアカウントに登録されているユーザ名と一緒である必要はありません。

[(none)]> create user 'wp_webuser'@'127.0.0.1' ;

作成したユーザにパスワードを設定します。もちろん、「P@ssW0rd」は自分で考えた難しいパスワード文字列にするんですよ。

ALTER USER 'wp_webuser'@'127.0.0.1' IDENTIFIED BY 'P@ssW0rd' ;

そしたら、wordpressで使用するデータベースを作成します。例の中の「wp_DaTaBaSe」は自分なりになにか捻ったデータベース名にしてください。

CREATE DATABASE wp_DaTaBaSe ;

次は、このデータベースに対して「'wp_webuser'@'127.0.0.1'」の操作権限を与えます。本当は細かく設定すべきだと思うんですが、面倒なのでフル権限を与えてみるなど。

grant all on wp_DaTaBaSe.* to 'wp_webuser'@'127.0.0.1' ;

ここまで来れば、後はWordPressの初期設定画面に上記の設定を与えてやれば動く気がしますが、実はこれでは動きません。散々ハマったのですが、これはMySQL側でユーザ認証の暗号化(SHA2)が行われているのに、WordPress側は暗号化しないで認証を取ろうとしているから、という話のようなのですね。

SELECT user, host, plugin FROM mysql.user;

とやると、ユーザデータベースが出てくると思いますが、

ALTER USER 'wp_webuser'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY 'P@ssW0rd';

こんな感じで、mysql_native_passwordでパスワードを設定してやればOK。もう一度

SELECT user, host, plugin FROM mysql.user;

とやれば、WordPressからの接続ユーザがmysql_native_password認証になっているのが確認できるはずです。
ここまで設定すれば、あとはWordPressの初期画面から設定が進められるはずです。お疲れさまでした。

アップロードファイルのサイズ変更

デフォルトでは8MBくらいまでしかファイルのアップロードが出来ないはず。この制限を緩和するのは、php.iniの設定にあります。/usr/local/etc/php.iniを編集し、

post_max_size = 4G
upload_max_filesize = 4G

とかやれば、最大4GBまでのアップロードが可能となります。
こんなもんかな。

以上。

この記事が気に入ったらサポートをしてみませんか?