迁移 vps 服务器,保留原 IP ( Google cloud )

上一个Google cloud vps实例创建的硬盘太大了,费用刷刷的往上涨。vps硬盘只能扩容不能缩小,只能重建实例了,正好将apache服务器换成nginx的体验一下。文章内容为流程和一些问题的参数设置。

1。创建新实例

2。搭建lnmp服务器(https://tl8517.com/lnmp-ubuntu-20-04/

apache设置 https://tl8517.com/ubuntu-lamp/

3。ftp权限问题(apache设置https://tl8517.com/wordpress-ftp/)

网站根目录(存放网站程序的文件夹)使用权限修改成和/etc/nginx/nginx.conf文件中‘user‘一样。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
csjtl@ubuntu:~$ sudo chown -R www-data /usr/share/wordpress/
csjtl@ubuntu:~$ ll
drwxr-xr-x 3 root root 4096 May 22 08:00 ./
drwxr-xr-x 4 root root 4096 May 22 07:32 ../
drwxr-xr-x 6 www-data 952083221 4096 May 23 01:24 wordpress/

csjtl@ubuntu:~$ sudo vi /etc/nginx/nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}

csjtl@ubuntu:~$ sudo systemctl restart nginx

4。文件大小限制(apache设置https://tl8517.com/wordpress-upload-file-size/

两部分:php(php.ini)和nginx(nginx.conf)都有传输大小限制。

修改php.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
csjtl@ubuntu:~$ sudo vi /etc/php/7.4/fpm/php.ini
1、max_execution_time //强制终止脚本前PHP等待脚本执行完毕的时间
修改为:max_execution_time =800

2、file_uploads = On //通常默认为On

3、upload_max_filesize =2M
修改为:upload_max_filesize = 800M

4、post_max_size //进行一次表单提交中PHP所能够接收的最大数据量
修改为:post_max_size =900M

5、max_input_time //通过POST、GET以及PUT方式,接收数据时间限制
修改为:max_input_time =900

6、memory_limit =10M //单个脚本程序可以使用的最大内存容量
修改为:memory_limit =128M

修改nginx.conf(添加http中的代码)

1
2
3
4
csjtl@ubuntu:~$ sudo vi /etc/nginx/nginx.conf
http {
client_max_body_size 800m;
}

5 .解析php

nginx不解析PHP,变成下载文件。两种方式解析PHP:监听9000端口或使用php-fpm。我使用php7.4-fpm。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
csjtl@ubuntu:~$ sudo vi /etc/nginx/sites-available/default

找到此行,添加index.php
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;

将下面注释掉的取消
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
}

6。备份原网站数据

使用Wordpress插件UpdraftPlus(免费版不能迁移网站才有这篇解决方法)

7 。交换新旧IP

在vpc网络选项中的外部IP地址中,选中你要保留的IP,点击后面的更改,将IP重新附加到你新建的实例中。在注意到可以保留IP更换vps的话,就不要释放以前速度快的静态地址了。

8 。申请证书

我使用certbot自动部署证书:

迁移 vps  IP Google cloud

1
2
3
4
5
6
7
8
9
10
11
12
13
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository universe
sudo apt-get update

sudo apt-get install certbot python3-certbot-nginx


sudo certbot --nginx

sudo certbot renew --dry-run

https://www.ssllabs.com/ssltest/

9 。还原数据

nginx没有像.htaccess的目录级配置文件,不具备.htaccess-type的功能,Wordpress不能修改服务器配置文件,只能手动添加伪静态规则。我使用apache时是用文章名来设置固定链接,改为nginx后只能使用朴素形式的。需要在配置文件中添加rewrite伪静态规则。

在/etc/nginx/sites-available/default文件中,找到server{}内的网站路径(root /var/www/wordpress),在下面添加if rewrite代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
csjtl@ubuntu:~$ sudo vi /etc/nginx/sites-available/default

server {
root /var/www/wordpress;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
}

csjtl@ubuntu:~$ sudo systemctl restart nginx

完结,撒花。

参考文档:

https://www.centos.bz/2018/01/nginx%E4%B8%8B%E4%BF%AE%E6%94%B9wordpress%E5%9B%BA%E5%AE%9A%E9%93%BE%E6%8E%A5%E8%AE%BE%E7%BD%AE%E5%90%8E%E6%97%A0%E6%B3%95%E8%AE%BF%E9%97%AE/

https://wordpress.org/support/article/nginx/