パッケージを最新に更新
sudo apt update && sudo apt upgrade -y
Nginx をインストール
sudo apt install nginx -y
Nginx サーバーを起動 & 自動起動を設定
sudo systemctl start nginx
sudo systemctl enable nginx
MySQL をインストール
sudo apt install mysql-server -y
MySQL の設定スクリプトを実行
sudo mysql_secure_installation
WordPress 用のデータベースとユーザーを作成
sudo mysql -u root -p
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
PHP と関連モジュールをインストール
sudo apt install php-fpm php-mysql -y
PHP の設定ファイルを編集
sudo nano /etc/php/8.3/fpm/php.ini
cgi.fix_pathinfo=0
PHP-FPM サービスを再起動
sudo systemctl restart php8.3-fpm
WordPress のダウンロードと設定
-
WordPress をダウンロード
bash cd /tmp wget https://wordpress.org/latest.tar.gz tar -xzvf latest.tar.gz
-
Nginx のウェブルートに配置
bash sudo mv wordpress /var/www/html/
-
WordPress のディレクトリの所有権を
www-data
に変更bash sudo chown -R www-data:www-data /var/www/html/wordpress
Nginx の設定
-
新しい Nginx サーバーブロックを作成
bash sudo nano /etc/nginx/sites-available/wordpress
/etc/nginx/sites-available/wordpress server { listen 80; server_name your_domain_or_ip; root /var/www/html/wordpress; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php{ include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.3-fpm.sock; } location ~ /\.ht { deny all; } }
-
サーバーブロックを有効化
bash sudo ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
-
Nginx の設定をテスト & 再起動
bash sudo nginx -t sudo systemctl reload nginx
WordPress のインストールとセットアップ
- ブラウザで
http://your_domain_or_ip
にアクセスし、WordPress のセットアップ画面に進む - 必要な情報(データベース名、ユーザー名、パスワード)を入力してインストールを完了
SSL の設定(任意)
Let’s Encrypt を使用して SSL 証明書をインストールする場合
sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d your_domain_or_ip
Tips
-
グループにユーザーを追加
sudo usermod -aG www-data ubuntu
-
グループを確認
groups ubuntu
-
グループからユーザーを除外
sudo gpasswd -d ubuntu www-data
-
全ユーザーに書き込み権限を付与(非推奨)
sudo chmod -R o+w /path/to/directory
-
全ユーザーに書き込み権限を解除
sudo chmod -R o-w /path/to/directory
-
PHP-FPM 設定を最適化
/etc/php/8.3/fpm/pool.d/www.conf pm = dynamic pm.max_children = 10 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 10 pm.max_requests = 500
/etc/php/8.3/fpm/php.ini memory_limit = 256M max_execution_time = 60
-
Nginx - Gzip 圧縮を有効化
/etc/nginx/nginx.conf http { ... gzip on; gzip_types text/plain text/css text/xml text/javascript application/javascript application/x-javascript application/xml application/json application/rss+xml application/atom+xml font/ttf font/otf font/woff font/woff2 image/svg+xml; gzip_min_length 1024; ... }
-
Nginx - アップロード可能なファイルサイズの上限数を変更
/etc/nginx/nginx.conf http { ... client_max_body_size 200M; ... }
bash sudo systemctl restart nginx.service
-
PHP-FPM - アップロード可能なファイルサイズの上限数を変更
/etc/php/8.3/fpm/php.ini upload_max_filesize = 200M post_max_size = 200M
bash sudo systemctl restart php8.3-fpm.service
-
WordPress - アップロード可能なファイルサイズの上限数を変更
wp-config.php @ini_set( 'upload_max_size' , '200M' ); @ini_set( 'post_max_size', '200M'); @ini_set( 'max_execution_time', '300' );
-
Nginx - バージョンを非表示
/etc/nginx/nginx.conf http { ... server_tokens off; ... }