ArchLinux 2017.10.01: CMSのWordPressをインストールする

コンテンツマネージメントシステムのWordPressをインストールする手順を記載します。

1 WordPressをインストールする

以下のスクリプトはWordPressをインストールします。

  • MYSQL_PASSWDはMySQLのrootユーザのパスワードです。
  • WORDPRESS_PASSWDはMySQLのwordpressユーザのパスワードです。
#!/bin/sh

set -e

[ -z "${MYSQL_PASSWD}" ] && \
  MYSQL_PASSWD=mysql
[ -z "${WORDPRESS_PASSWD}" ] && \
  WORDPRESS_PASSWD=$(openssl rand -base64 32 | head -c 32)

mysql_install()
{
  sudo pacman -Sy --noconfirm mariadb

  # Install database.
  sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

  sudo systemctl enable mariadb
  sudo systemctl start mariadb

  # Password configuration.
  cat <<EOF | sudo mysql_secure_installation

y
${MYSQL_PASSWD}
${MYSQL_PASSWD}
n
y
y
y
EOF

  cat<<EOF | sudo mysql -uroot -p${MYSQL_PASSWD}
create database wordpress;
grant all privileges on wordpress.* to wordpress@localhost
  identified by '${WORDPRESS_PASSWD}';
flush privileges;
exit
EOF
}

wordpress_install()
{
  sudo pacman -Sy --noconfirm wordpress

  cat <<EOF | sudo tee /usr/share/webapps/wordpress/wp-config.php
<?php
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', '${WORDPRESS_PASSWD}');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
define('AUTH_KEY', '$(openssl rand -base64 64 | head -c 64)');
define('SECURE_AUTH_KEY', '$(openssl rand -base64 64 | head -c 64)');
define('LOGGED_IN_KEY', '$(openssl rand -base64 64 | head -c 64)');
define('NONCE_KEY', '$(openssl rand -base64 64 | head -c 64)');
define('AUTH_SALT', '$(openssl rand -base64 64 | head -c 64)');
define('SECURE_AUTH_SALT', '$(openssl rand -base64 64 | head -c 64)');
define('LOGGED_IN_SALT', '$(openssl rand -base64 64 | head -c 64)');
define('NONCE_SALT', '$(openssl rand -base64 64 | head -c 64)');
\$table_prefix  = 'wp_';
define('WP_CONTENT_DIR', '/usr/share/webapps/wordpress/wp-content');
define('DISALLOW_FILE_MODS', false);
define('AUTOMATIC_UPDATER_DISABLED', false);
define('WP_DEBUG', false);
\$_SERVER['HTTPS'] = 'on';
define('FS_METHOD', 'direct');
if ( !defined('ABSPATH') )
  define('ABSPATH', '/usr/share/webapps/wordpress');
require_once(ABSPATH . 'wp-settings.php');
?>
EOF
}

php_install()
{
  # Enable PHP extension.
  sudo pacman -Sy --noconfirm php
  sudo sed -i /etc/php/php.ini \
       -e 's/^;extension=pdo_mysql.so/extension=pdo_mysql.so/g' \
       -e 's/^;extension=mysqli.so/extension=mysqli.so/g'
}

apache_install()
{
  sudo pacman -Sy --noconfirm apache php-apache
  sudo systemctl enable httpd

  # PHP configuration.
  sudo sed -i /etc/httpd/conf/httpd.conf \
       -e 's/^LoadModule mpm_event_module/#LoadModule mpm_event_module/g' \
       -e 's/^#LoadModule mpm_prefork_module/LoadModule mpm_prefork_module/g'
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
LoadModule php7_module modules/libphp7.so
AddHandler php7-script php
Include conf/extra/php7_module.conf
EOF

  # ssl configuration.
  # Country Name (2 letter code) [AU]:
  # State or Province Name (full name) [Some-State]:
  # Locality Name (eg, city) []:
  # Organization Name (eg, company) [Internet Widgits Pty Ltd]:
  # Organizational Unit Name (eg, section) []:
  # Common Name (e.g. server FQDN or YOUR name) []:
  # Email Address []:
  cat <<EOF | sudo openssl req -new -x509 -nodes -newkey rsa:4096 -days 1095 \
                   -keyout /etc/httpd/conf/server.key \
                   -out /etc/httpd/conf/server.crt
AU
Some-State
city
company
section
${WEBDAV_SERVER_FQDN}

EOF
  sudo sed -i /etc/httpd/conf/httpd.conf \
       -e 's/^#LoadModule ssl_module/LoadModule ssl_module/g' \
       -e 's/^#LoadModule socache_shmcb_module/LoadModule socache_shmcb_module/g'
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/httpd-ssl.conf
EOF

  # rewrite configuration.
  sudo sed -i /etc/httpd/conf/httpd.conf \
       -e 's/^#LoadModule rewrite_module/LoadModule rewrite_module/g'
  cat << EOF | sudo tee /etc/httpd/conf/extra/redirect-to-https.conf
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}
EOF
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/redirect-to-https.conf
EOF

  # wordpress configuration.
  cat <<EOF | sudo tee /etc/httpd/conf/extra/wordpress.conf
Alias /wordpress /usr/share/webapps/wordpress
<Directory /usr/share/webapps/wordpress>
  AllowOverride All
  Options FollowSymlinks
  Require all granted
</Directory>
EOF
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/wordpress.conf
EOF

  sudo systemctl restart httpd
}

wordpress_main()
{
  mysql_install
  php_install
  wordpress_install
  apache_install
}

wordpress_main

2 WordPressへアクセスする

以下のURLにhttps経由でアクセスします。ブラウザでこのページの証明書を許可してください。

https://<server>/wordpress

0001_WordPress.png