Ubuntu VPS×Apache2:5分でWordPressインストール&無料SSLでHTTPS化

本記事ではVPSを起動してから5分後にはWEBサーバーをHTTPS化してWordPressを立ち上げる手順を解説します。

このWEBサイトも同様の手順で構築しています!

目次

構築する環境について

OSはUbuntu Linux 24.04 LTSを使います。

他のOSだとコマンドが異なる場合があるので注意してください!

また、WEBサーバーとしてApache2、データベースサーバーとしてMySQLをインストールします。Apache2ではなくNginxを使いたいという方は本記事は参考になりません。

MySQLではなくMariaDBを使いたいという方は、インストール時のコマンドを少し修正するだけでOKです。

Apache2インストール+HTTPS化+WordPressインストール

まず最初にrootユーザーになってください。「su -」もしくは「sudo su -」でrootになれます。

以下のようにidコマンドを実行したら「root」と表示されていますか?

# id
uid=0(root) gid=0(root) groups=0(root)
#

問題なくrootユーザーになっていますね?

まずPHPのバージョンを決めます。PHPはバージョンに追従する必要があるので、後から切替ができるように配慮しています。

とりあえず最新のPHPバージョンを指定してください

PHP_VER=8.4
echo $PHP_VER

以下のようにインストールしたいPHPバージョンが表示されていますか?問題なければ続けましょう!

# echo $PHP_VER
8.4
#

Apache+MySQL+PHPをインストールする

PHPバージョンを指定してインストールする準備をします。

apt update
apt dist-upgrade -y
apt install ca-certificates apt-transport-https software-properties-common lsb-release -y
add-apt-repository ppa:ondrej/php -y

次にWordPressを動作させるために必要最低限のパッケージをインストールします。

apt update
apt upgrade -y
apt install apache2 -y
apt install mysql-server -y
apt install php${PHP_VER} libapache2-mod-php${PHP_VER} php${PHP_VER}-mysql php${PHP_VER}-mbstring php${PHP_VER}-zip php${PHP_VER}-xml php${PHP_VER}-curl php${PHP_VER}-bcmath php${PHP_VER}-gd php${PHP_VER}-imagick php${PHP_VER}-intl php${PHP_VER}-soap -y

WordPressをインストールする

次にWordPressをインストールします。

a2enmod rewrite
systemctl enable apache2
systemctl enable mysql
wget https://wordpress.org/latest.tar.gz && tar zxf latest.tar.gz
rm -rf /var/www/html/*
mv wordpress/* /var/www/html/
cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
chown -R www-data:www-data /var/www/html

これでWordPressのインストールは完了です。簡単ですね。

WordPressが動作するようにデーベースを設定する

次にMySQLをセキュアな状態にするため次のコマンドを実行します。

本当はMySQL5.7以降であれば不要ですが、念のため実行しています

mysql_secure_installation

コマンドを実行すると色々と聞かれますが、すべて「y」で答えてください。

# mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.
The 'validate_password' component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.

Skipping password set for root as authentication with auth_socket is used by default.
If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.

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!
#

次にWordPressを動作させるためのデータゲースを作成します。ここで、データベース名とユーザー名、パスワードを決めておいてください。

データベース名wordpressこのままでOK
ユーザー名wpuserこのままでOK
パスワードxi7q!LhTnp複雑なものにする

まずはMySQLに接続します。

mysql -u root -p

ここで次のようにパスワードの入力を促されるので「root」と入力してください。

自分が入力した文字は表示されないので注意

Enter password: root

正しいパスワードを入力すると次のようにMySQLへ接続できます。

# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 424
Server version: 8.0.43-0ubuntu0.24.04.1 (Ubuntu)

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

上記の画面になったらSQLを流し込みます。

赤字の箇所は自分で決めたデータベース名、ユーザー名、パスワードに置き換えてくださいね!

CREATE DATABASE wordpress;
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'xi7q!LhTnp';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;

次にWordPressの設定をおこないます。

ここも赤字の箇所は自分で決めたデータベース名、ユーザー名、パスワードに置き換えてくださいね!

sed -i 's/database_name_here/wordpress/' /var/www/html/wp-config.php
sed -i 's/username_here/wpuser/'         /var/www/html/wp-config.php
sed -i 's/password_here/su2n!DgYsw/'     /var/www/html/wp-config.php

ついでにPHPで設定しているアップロードサイズも大きくしておきます。

sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 20M/' /etc/php/${PHP_VER}/apache2/php.ini

Let's EncryptでHTTPS化する

環境変数にドメイン名を設定します。

DOMAIN=vpsninja.net
echo $DOMAIN

ApacheでSSLを有効化して必要なパッケージをインストールします。

a2enmod ssl
apt install certbot -y

次にSSL証明書を発行します。

certbot certonly --webroot -d $DOMAIN -w /var/www/html

上記のコマンドを実行するとSSL証明書の発行が始まります。赤字の箇所が自分で入力する必要がある箇所です。メールアドレスはご自身のメールアドレスを入力してくだい。

# certbot certonly --webroot -d $DOMAIN -w /var/www/html
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Enter email address (used for urgent renewal and security notices)
 (Enter 'c' to cancel): admin@vpsninja.net

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.5-February-24-2025.pdf. You must
agree in order to register with the ACME server. Do you agree?
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: y

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: n
Account registered.
Requesting a certificate for vpsninja.net

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/vpsninja.net/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/vpsninja.net/privkey.pem
This certificate expires on 2025-11-19.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#

証明書が発行されたら設定を修正します。次のコマンドを流せばOKです。

sed -i "s/\/etc\/ssl\/certs\/ssl-cert-snakeoil.pem/\/etc\/letsencrypt\/live\/${DOMAIN}\/fullchain.pem/" /etc/apache2/sites-available/default-ssl.conf
sed -i "s/\/etc\/ssl\/private\/ssl-cert-snakeoil.key/\/etc\/letsencrypt\/live\/${DOMAIN}\/privkey.pem/"  /etc/apache2/sites-available/default-ssl.conf

次にSSL証明書を自動的に更新するようcron設定しておきます。

cat >/etc/cron.d/ssl-renew<<EOF
0 0 1 * * root certbot renew --deploy-hook "systemctl reload apache2"
EOF

さいごにApacheのSSL設定を有効化してプロセスを再起動します。

a2ensite default-ssl.conf
systemctl restart apache2

WordPressでパーマリンク変更時のエラーを防ぐ設定

Apache2の初期設定だと.htaccessの利用が許可されていないため、WordPressのパーマリンクを変更したときページが見つからないというエラーに遭遇します。

その対策として「/etc/apache2/apache2.conf」に設定を追加することをおすすめします。

お好みのエディタで設定ファイルを編集します。

vi /etc/apache2/apache2.conf

設定ファイル内にDirectoryディレクティブがあるはずです。

<Directory />
        Options FollowSymLinks
        AllowOverride None
        Require all denied
</Directory>

<Directory /usr/share>
        AllowOverride None
        Require all granted
</Directory>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
</Directory>

#<Directory /srv/>
#       Options Indexes FollowSymLinks
#       AllowOverride None
#       Require all granted
#</Directory>

このあたりのどこでもよいので以下の内容を追加してください。

<Directory /var/www/html>
        Options Indexes FollowSymLinks
        AllowOverride all
        Require all granted
</Directory>

単に「AllowOverride None」を「AllowOverride all」にしているだけですが、これによって.htaccessファイルの利用が許可されてWordPressのパーマリンク関連のエラーを防ぐことができます。

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

VPS利用歴20年以上!
VPSの使い方やおすすめのVPSを分かりやすくお伝えできるよう、日々努力しています!

コメント

コメントする

目次