Hướng dẫn cấu hình Nginx với SSL làm Reverse Proxy cho WordPress trên Ubuntu server 18.04 LTS

30/12/2020

Onet đã có bài viết hướng dẫn cấu Nginx làm Reverser Proxy trên CentOS-7.

Hôm nay, bài viết này sẽ hướng dẫn các bạn thực hiện cấu hình Nginx làm Reverse Proxy cho WordPress trên Ubuntu 18.04

Mục lục

Mô hình và IP Planing
1. Cấu hình trên Nginx server
Bước 1: Cài đặt Nginx
Bước 2: Tạo file config site
Bước 3: Thiết lập chứng chỉ Let’s Encrypt
2. Cấu hình trên server cài WordPress

Mô hình và IP Planing

Mô hình

IPPlaning

Thao tác thực hiện trên nginx server và server cài wordpress thực hiện với quyền root hoặc quyền sudo.

1. Cấu hình trên Nginx Server

Bước 1: Cài đặt Nginx

1. Cài đặt nginx

apt uptdate  apt install nginx -y 

2. Cho phép lưu lượng truy cập trên cổng 80.

ufw allow 'Nginx HTTP' 

3. Backup file cấu hình

cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bk

Bước 2: Tạo file config site

1. Trước khi tạo file config cho địa chỉ truy cập, ta cần kiểm tra bản ghi DNS đối với địa chỉ sẽ sử dụng :

nslookup wordpress.dangdohai.xyz

Nếu chưa tạo 1 bản ghi tên miền, hãy truy cập zonedns để tạo 1 bản ghi.

2. Tạo file config site:

Sau khi kiểm tra bản ghi đã tồn tại, ta sẽ tạo file config của tên miền, mỗi site sẽ được khai báo tương ứng với 1 file nằm trong thư mục /etc/nginx/conf.d/.

Tạo file có tên là tên miền của bạn với phần mở rộng là .conf và thêm vào file với nội dung như sau:

vim /etc/nginx/conf.d/wordpress.dangdohai.xyz.conf 

Thêm vào file nội dung sau:

server {  server_name wordpress.dangdohai.xyz;      location / {          proxy_set_header   X-Real-IP             $remote_addr;          proxy_set_header   X-Forwarded-For     $proxy_add_x_forwarded_for;           proxy_set_header X-Forwarded-Proto https;          proxy_pass http://10.10.34.163;      }  } 

3. Kiểm tra file cấu hình và khởi động lại nginx

Kiểm tra file cấu hình

[root@nginx ~]# nginx -t  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok  nginx: configuration file /etc/nginx/nginx.conf test is successful 

Restart nginx:

systemctl restart nginx 

4. Kiểm tra cài đặt

Vào trình duyệt web và truy cập bằng tên miền để kiểm tra. Ở đây mình sẽ truy cập bằng địa chỉ wordpress.dangdohai.xyz

5. Cài đặt chuyển hướng về tên miền

Tuy đã truy cập thành công bằng tên miền, nhưng khi ta truy cập bằng IP thì thanh URL vẫn hiển thị là địa chỉ IP. Để chuyển về tên miền ta thực hiện các bước sau

  • Truy cập và đăng nhập vào Side Admin, vào tab Settings và chọn General .
  • Sau đó sửa địa chỉ ip mặc định thành tên miền tại WordPress Address và Site Address (Ở bài này là https://wordpress.dangdohai.xyz ) -> click Save Changes

Sau khi đã lưu, ta trở lại Nginx server để tiếp tục cấu hình SLL cho site.

Bước 3: Thiết lập chứng chỉ Let’s Encrypt

1. Cài đặt Certbot

apt install python-certbot-nginx -y 

2. Sinh SSL bằng Let’s Encrypt cho site wordpress.dangdohai.xyz

certbot --nginx -d wordpress.dangdohai.xyz

OUTPUT

Saving debug log to /var/log/letsencrypt/letsencrypt.log  Plugins selected: Authenticator nginx, Installer nginx  Enter email address (used for urgent renewal and security notices) (Enter 'c' to cancel): [email protected]   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  Please read the Terms of Service at  https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must agree in order to register with the ACME server at  https://acme-v02.api.letsencrypt.org/directory  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  (A)gree/(C)ancel: a   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  Would you be willing 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: y  Obtaining a new certificate  Performing the following challenges:  http-01 challenge for wordpress.dangdohai.xyz  Waiting for verification...  Cleaning up challenges  Deploying Certificate to VirtualHost /etc/nginx/conf.d/wordpress.dangdohai.xyz.conf   Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  1: No redirect - Make no further changes to the webserver configuration. 2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for new sites, or if you're confident your site works on HTTPS. You can undo this change by editing your web server's configuration.  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2  Redirecting all traffic on port 80 to ssl in /etc/nginx/conf.d/wordpress.dangdohai.xyz.conf   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  Congratulations! You have successfully enabled https://wordpress.dangdohai.xyz   You should test your configuration at: https://www.ssllabs.com/ssltest/analyze.html?d=wordpress.dangdohai.xyz  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   IMPORTANT NOTES:  - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/wordpress.dangdohai.xyz/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/wordpress.dangdohai.xyz/privkey.pem Your cert will expire on 2020-07-01. To obtain a new or tweaked version of this certificate in the future, simply run certbot again with the "certonly" option. To non-interactively renew *all* of your certificates, run "certbot renew"  - Your account credentials have been saved in your Certbot configuration directory at /etc/letsencrypt. You should make a secure backup of this folder now. This configuration directory will also contain certificates and private keys obtained by Certbot so making regular backups of this folder is ideal.  - 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 

3. Kiểm tra lại kết quả trong file config site

Sau khi sinh ssl cho site, ta kiểm tra lại file config để thấy sự thay đổi.

cat /etc/nginx/conf.d/wordpress.dangdohai.xyz.conf 
server {  server_name wordpress.dangdohai.xyz;      location / {          proxy_set_header   X-Real-IP             $remote_addr;          proxy_set_header   X-Forwarded-For       $proxy_add_x_forwarded_for;          proxy_set_header X-Forwarded-Proto https;          proxy_pass http://10.10.34.163;      }       listen 443 ssl; # managed by Certbot      ssl_certificate /etc/letsencrypt/live/wordpress.dangdohai.xyz/fullchain.pem; # managed by Certbot      ssl_certificate_key /etc/letsencrypt/live/wordpress.dangdohai.xyz/privkey.pem; # managed by Certbot      include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot      ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot }   server {      if ($host = wordpress.dangdohai.xyz) {          return 301 https://$host$request_uri; } # managed by Certbot  server_name wordpress.dangdohai.xyz;      listen 80;      return 404; # managed by Certbot  } 

4. Khởi động lại Nginx

systemctl restart nginx 

2. Cấu hình trên webserver cài WordPress

1. Sửa file wp-config.php

Tiến hành vào file wp-config.php tại /var/www/html/wp-config.php

vi /var/www/html/wp-config.php 

Thêm vào những dòng sau:

if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https'){ $_SERVER['HTTPS']='on'; } if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { $_SERVER['HTTP_HOST'] = $_SERVER['HTTP_X_FORWARDED_HOST']; } 

File /var/www/html/wp-config.php

2. Khởi động lại dịch vụ http

systemctl restart httpd 

3. Truy cập trang web bằng địa chỉ https://wordpress.dangdohai.xyz/ đã có SSL.

Kết luận

Trên đây là hướng dẫn của Onet về cách cấu hình Nginx làm Reverse Proxy cho WordPress trên Ubuntu server 18.04 LTS.

Chúc các bạn thành công.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Hướng dẫn kết nối SSH trên Windows

Bài viết này sẽ hướng dẫn bạn cách kết nối với máy chủ bằng giao thức SSH trên máy Window. ...
30/12/2020

[DirectAdmin] Hướng dẫn upload website lên Direct Admin (phần 3)

Direct Admin (DA) là công cụ để quản trị hosting tuyệt vời, trong các phần giới thiệu trước đây...
30/12/2020

Hướng dẫn cài đặt LEMP stack trên Ubuntu 20.04 LTS

LEMP là một nhóm các phần mềm có thể dùng để phục vụ các trang web động và các ứng dụng...
30/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024