Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save momoseijin/bc67db1d4cbfbf3a6186c3e3aa81c002 to your computer and use it in GitHub Desktop.
Save momoseijin/bc67db1d4cbfbf3a6186c3e3aa81c002 to your computer and use it in GitHub Desktop.
How to install WordPress with Nginx and MariaDB on Ubuntu 22.04
# Ubuntu 22.04 はインストール完了しておく
# Nginx のインストール
sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# MariaDB のインストール
sudo apt install mariadb-server
sudo apt install mariadb-client
sudo systemctl start mariadb
sudo systemctl enable mariadb
# MariaDB のセットアップ
sudo mysql_secure_installation
If you've just installed MariaDB, and haven't set the root password yet, you should just press enter here.
Enter current password for root (enter for none): PRESS ENTER
Switch to unix_socket authentication [Y/n] n
Change the root password? [Y/n] n
Remove anonymous users? [Y/n] y
Disallow root login remotely? [Y/n] y
Remove test database and access to it? [Y/n] y
Reload privilege tables now? [Y/n] y
All done!
# データベースを作成する
sudo mysql -u root -p
CREATE DATABASE database_name;
CREATE USER 'wpdbuser'@'localhost' IDENTIFIED BY 'new_password';
CREATE USER 'wpdbuser'@'localhost' IDENTIFIED BY 'new_password';
GRANT ALL ON wpdb.* TO 'db_user_name'@'localhost' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EXIT;
# FPM のインストール
sudo apt install php-fpm php-common php-mysql cdphp-gmp php-curl php-intl php-mbstring php-xmlrpc php-gd php-xml php-cli php-zip
# インストールされた FPM のバージョンを確認
/etc/php の中に 8.1 などのバージョンディレクトリがある
sudo systemctl start php8.1-fpm
sudo systemctl enable php8.1-fpm
# FPM の設定ファイルの変更(以下のファイルの中の項目)
sudo nano /etc/php/8.1/fpm/php.ini
file_uploads = On
allow_url_fopen = On
short_open_tag = On
memory_limit = 256M
cgi.fix_pathinfo = 0
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 360
# wp-cli のインストール
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
php wp-cli.phar --info
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp cli version
# WordPress のダウンロード(pathなどは各自設定)
wp core download --locale=ja --path=/var/www/wordpress
wp core config --dbname=database_name --dbuser=db_user_name --dbpass=database_pass
wp core install --url=http://example.com/ --title=wordpress_title --admin_user=wordpress_user --admin_password=password --admin_email=test@example.com
# WordPress をインストールしたディレクトリに権限をつける
sudo chown -R www-data:www-data /var/www/wordpress/
sudo chmod -R 755 /var/www/wordpress/
# Nginx の設定ファイルの作成
sudo nano /etc/nginx/sites-available/wordpress.conf
# 中身はこんな感じ
server {
listen 80;
listen [::]:80;
root /var/www/wordpress;
index index.php index.html index.htm;
server_name example.com www.example.com;
client_max_body_size 100M;
autoindex off;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 作成したら、site-enabled にリンクを張る
sudo ln -s /etc/nginx/sites-available/wordpress.conf /etc/nginx/sites-enabled/
sudo systemctl restart nginx
# ここまでで、新規の WordPress が立ち上がる
# 古いサーバからデータを dump
mysqldump -u db_user_name -p database_name --single-transaction > dump.sql
移行先でデータベース名を変更する場合は、書き出した dump.sql 内のデータベース名を変更する
# 新しいサーバで dump データを読み込む
mysql -u db_user_name -p database_name < dump.sql
# SSL(https) にするために Certbot をインストールする(今回は Certbot の公式にあるsnapで)
sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --nginx
# 成功すると、先程作成した wordpress.conf にSSLの設定が書き込まれ、しばらくすると https で接続できるようになる
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment