CentOS7 配置Nginx域名HTTPS Here is the revised guideline for setting up a WordPress blog with Nginx:
Step 1: Install Nginx, MySQL, and PHP (LEMP Stack)
- Install Nginx:
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
- Install MySQL:
sudo yum install mariadb-server mariadb
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation
- Install PHP and PHP-FPM:
sudo yum install epel-release
sudo yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm
sudo yum install yum-utils
sudo yum-config-manager --enable remi-php74
sudo yum install php php-fpm php-mysql php-common php-cli php-json php-zip php-gd php-mbstring php-curl php-xml php-xmlrpc php-soap php-intl
php -v
sudo systemctl start php-fpm
sudo systemctl enable php-fpm
### Step 2: Create a MySQL Database and User
1. **Log in to MySQL:**
```bash
sudo mysql -u root -p
- Create a database for WordPress:
CREATE DATABASE wordpress;
- Create a new MySQL user:
CREATE USER 'wpuser'@'localhost' IDENTIFIED BY 'password';
- Grant privileges to the new user:
GRANT ALL PRIVILEGES ON wordpress.* TO 'wpuser'@'localhost';
FLUSH PRIVILEGES;
EXIT;
Step 3: Download and Configure WordPress
- Download the latest WordPress:
cd /usr/share/nginx/html
sudo wget https://wordpress.org/latest.tar.gz
sudo tar -xzf latest.tar.gz
sudo mv wordpress/* .
sudo rmdir wordpress
sudo rm -f latest.tar.gz
- Set the correct permissions:
sudo chown -R nginx:nginx /usr/share/nginx/html
sudo chmod -R 755 /usr/share/nginx/html
- Configure WordPress:
sudo cp wp-config-sample.php wp-config.php
sudo nano wp-config.php
Edit the following lines to include your database information:
define('DB_NAME', 'wordpress');
define('DB_USER', 'wpuser');
define('DB_PASSWORD', 'password');
define('DB_HOST', 'localhost');
Step 4: Configure Nginx for WordPress
- Create a new Nginx configuration file for your site:
sudo nano /etc/nginx/conf.d/wordpress.conf
- Add the following configuration:
server {
listen 80;
server_name www.xxx.xyz;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
- Test the Nginx configuration:
sudo nginx -t
- Restart Nginx:
sudo systemctl restart nginx
Step 5: Complete the WordPress Installation
- Open your browser and go to:
http://www.xxx.xyz
- Follow the on-screen instructions to complete the installation:
- Choose your language
- Fill in the site title, username, password, and email
- Click “Install WordPress”
Step 6: Configure Google AdSense
- Sign up for Google AdSense:
- Go to Google AdSense
- Sign up with your Google account
- Add your site to AdSense:
- Enter your website URL
- Follow the instructions to verify your site
- Get the AdSense code:
- After your site is verified, get the AdSense code from your AdSense account
- Add AdSense code to WordPress:
- Install a plugin like “Ad Inserter” or “Insert Headers and Footers” in WordPress
- Go to the plugin settings and paste the AdSense code in the appropriate section
- Configure ad placements:
- Use the plugin to choose where you want ads to appear on your site
Step 7: Write and Publish Your Blog Posts
- Log in to WordPress Admin:
http://www.xxx.xyz/wp-admin
- Create a new post:
- Go to Posts -> Add New
- Write your content and publish
- Customize your site:
- Go to Appearance -> Themes to choose a theme
- Use the Customizer to adjust the look and feel of your site
By following these steps, you will have a WordPress blog up and running on your domain with Google AdSense configured to help generate passive income.
/etc/nginx/conf.d/wordpress.conf
server {
if ($host = www.***.xyz) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name www.***.xyz;
return 301 https://$host$request_uri;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name ***.xyz;
ssl_certificate /etc/letsencrypt/live/***.xyz/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/***.xyz/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
return 301 https://www.***.xyz$request_uri;
ssl_certificate /etc/letsencrypt/live/***.xyz/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/***.xyz/privkey.pem; # managed by Certbot
}
server {
listen 443 ssl;
listen [::]:443 ssl;
server_name www.***.xyz;
ssl_certificate /etc/letsencrypt/live/www.***.xyz/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.***.xyz/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
root /usr/share/nginx/html;
index index.html index.htm;
# location / {
# try_files $uri $uri/ =404;
# }
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}