在example.com部署WordPress并保留静态页面的最佳方案咨询
Hey there! Let’s break this down to get your setup working exactly how you want it—starting with the big question: single server vs. separate deployment, then walking through the step-by-step implementation.
First, let’s weigh your options:
- Single server (recommended): This is simpler, cheaper, and easier to manage long-term. You’ll host both WordPress and your static pages on the same EC2 instance, using web server rules to route requests correctly. No need to juggle two servers or complex DNS tricks.
- Separate deployment: Only worth considering if your static pages have unique needs (like extreme traffic that needs isolated scaling, or strict security requirements that don’t align with WordPress). But for most cases, this adds unnecessary complexity—you’d need to use a reverse proxy or CDN to route paths to different servers, which complicates maintenance.
We’ll focus on the single-server path since it’s the best fit for your requirements.
Nginx is ideal here because it’s flexible for routing requests to different content sources. Here’s how to set it up:
1. Prep your EC2 instance
- Spin up an EC2 instance (a
t3.microworks great for free tier eligibility, scale up later if needed) using Ubuntu/Debian or Amazon Linux. - Install required dependencies: Nginx, PHP (with extensions like
php-mysql,php-curl,php-gd), and MariaDB/MySQL. - Configure your EC2 security group to allow inbound traffic on ports 80 (HTTP) and 443 (HTTPS).
2. Migrate your static pages
- Upload all your existing static files to a directory on EC2, e.g.,
/var/www/static. Match the path structure exactly:example.com/hello→/var/www/static/hello/index.htmlexample.com/hello/canada→/var/www/static/hello/canada/index.html- Repeat this for
/goodbye,/help, and all their subpaths.
3. Install and configure WordPress
- Download WordPress to a separate directory, e.g.,
/var/www/wordpress. - Create a MySQL database and user for WordPress, then update
wp-config.phpwith these credentials. - Critical permalink setup: Log into the WordPress admin dashboard, go to Settings → Permalinks, select "Custom Structure", and enter
/blog/%postname%/. This ensures your blog posts live atexample.com/blog/your-postand category pages useexample.com/category/your-category—exactly what you want. - Set your WordPress homepage to be the default site homepage (no need for a static homepage here, since you want
example.comto be the WordPress site).
4. Configure Nginx routing rules (the magic part)
This is where you tell Nginx to send static page requests to your static files, and everything else to WordPress. Edit your Nginx server block (usually at /etc/nginx/sites-available/example.com) with this config:
server { listen 80; server_name example.com; root /var/www/wordpress; index index.php index.html index.htm; # Route static page paths first (priority over WordPress) location ~ ^/(hello|goodbye|help) { root /var/www/static; try_files $uri $uri/ =404; } # Handle WordPress paths (root, /blog, /category) location / { try_files $uri $uri/ /index.php?$args; } # Process PHP files for WordPress location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; # Adjust to your PHP version (e.g., php7.4-fpm) } # Block access to hidden files for security location ~ /\.ht { deny all; } }
- Test the config with
sudo nginx -t, then reload Nginx withsudo systemctl reload nginx.
5. Add SSL (optional but highly recommended)
Use Let’s Encrypt’s Certbot to get a free SSL certificate and configure Nginx to redirect all HTTP traffic to HTTPS. This keeps your site secure and improves SEO.
6. Update DNS in Hover
- Log into your Hover account, navigate to the DNS settings for
example.com. - Update your A record (and AAAA record if using IPv6) to point to your EC2 instance’s public IP address.
- Remove any old DNS records that point to your previous server (like old A/CNAME records).
- Wait for DNS propagation (usually 5-30 minutes, depending on your TTL settings).
7. Test everything
- Visit
example.comto confirm it loads your WordPress site. - Check all static paths:
example.com/hello,example.com/hello/canada,example.com/goodbye,example.com/help—they should load your original static content. - Publish a test blog post and verify it loads at
example.com/blog/test-post. - Check a category page (e.g.,
example.com/category/test) to ensure it works as expected.
If you must keep static pages on your original server, here’s how:
- Keep your static server running, and configure your EC2 Nginx to proxy requests for
/hello,/goodbye,/help(and subpaths) to your original server’s IP. - Example Nginx location block for proxying:
location ~ ^/(hello|goodbye|help) { proxy_pass http://[your-static-server-ip]; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } - Update Hover DNS to point all traffic to your EC2 instance (since Nginx will handle routing to the static server). This avoids DNS limitations (DNS can’t route specific paths, only entire domains/subdomains).
内容的提问来源于stack exchange,提问作者user1575454

