投稿    登录
  《Python3网络爬虫开发实战》赠书活动正在进行中!详情请戳赠书活动!欢迎参与!非常感谢!

WordPress从Apache迁移到Nginx过程

Linux 崔庆才 8636浏览 163评论

之前一直都用Apache服务器,由于网站访问量比较大,另外加上旧服务器快到期了,准备迁移到新的服务器上,所以决定采用Nginx服务器。

迁移过程比较心酸,之前一直用apache,对nginx服务器配置不熟悉,踩了很多坑。下面说一下我的网站从旧主机(配有apache服务器)迁移到新主机(配有nginx服务器)的过程。

代码迁移

这个过程其实也是比较心酸的,查看了一下目录结构占用空间已经足足快1个G了,可想而知里面占用的大部分空间是上传的图片素材。

不过要是迁移全部图片的话工程量实在是巨大。不过,好消息是我从开始就使用了七牛CDN加速,所以,上传的图片会自动存放到七牛,只不过也在主机本地留了备份而已,所以,我可以安心地删掉它们了。

那么对代码进行瘦身之后,这里就有两种方法来迁移了:

1.可以用git上传到github,然后用另一台主机把代码拉下来即可,在此不再赘述。

2.打包上传,然后直接在另一台主机上下载下来,由于我的两台主机在同一局域网内,所以我直接采用了这种方式,传输速度快。

打包

由于代码中含有 .git 目录,所以这部分我们不需要打包,那么压缩时我们就需要排除这个文件夹。

20151113155701

文件夹名叫cqc,那么我们就打包一下,排除.git目录,使用如下命令

运行结束后会出现 cqc.tar.gz 文件,这就是目录压缩包。

然后我们只需要在另一台主机上输入

即可完成下载,速度可是嗖嗖的

然后解压即可,代码便完成了迁移。

数据库迁移

数据库用二者的phpmyadmin导出和上传即可。我导出 .sql 文件,大小为9M,而phpMyAdmin的上传限制大小是2M,怎么办?其实我们可以压缩 .sql 文件为 zip格式,压缩之后就有了1.4M了,分分钟完成上传。要知道 phpMyAdmin 可是支持 .sql.zip 文件的。

接下来是一个比较重要的部分,那就是配置一下站点信息。直接修改数据库的两个URL。

分别是 siteurl 和 home,一定要修改为 http://xxx.xxx.xxx.xxx/cqc 的形式,也就是把原来的域名改成IP加目录的形式,要不然网站是无法访问的,会出现多重循环定向的提示。

好,其他的没什么问题,连接数据库错误的话就修改一下目录的 wp-config.php 文件吧,连接数据库的信息修改正确就好了。

配置vhosts

和 apache 一样,我们多个域名肯定要可以解析到不同的目录吧,nginx 当然也是支持的。

接下来我们需要把新域名解析到 cqc 目录,在 nginx 下怎么做呢?其实还是比较简单的。

在 /etc/nginx 目录下可以新建一个 vhosts文件夹。在这里我们要解析 cqc 目录,那么我就新建一个 cqc.conf 文件。

现在例如我要把 blog.cuiqingcai.com 解析到 cqc 文件夹,配置如下

其中

这一部分是伪静态重写,因为我的博客用的是wordpress,所以伪静态重写是这样的。当然还有其他的重写方式可以尝试。

之后在 /etc/nginx/nginx.conf 中的 http{} 中添加一行

则代表引用了这个文件。

注意,还要把 域名设置一下,添加一条A记录到主机上。

好了,一切大功告成了。

后记

迁移和配置的过程坑实在是太多了,列列吧,警醒世人呐。

(1)代码迁移过程上传git,整个项目差不多1个G,由于数据量太大,导致内存不够无法正常上传。后来删除了图片,发现项目还是很大,结果发现是 .git 目录已经占用了上百兆,后来打包排除这个目录迁移的。

(2)数据库迁移的时候由于phpMyAdmin上传大小限制,修改了一番上传大小结果发现没生效,还倒腾了一下php-fpm,后来发现可以直接上传压缩包,那就分分钟完成了。

(3)配置完之后发现网站首页正常访问了,可是其他页面全部出现了404错误,后来配置了一番伪静态解析发现配置代码直接写在了 localhost server里面,后来发现可以直接新写一个 server,然后配置域名servername,然后配置伪静态重写才成功。

总之,坎坷是多,但是,自己慢慢摸索出来,也是一种不错的体验。

当你成功之后,会觉得世界又是那么美好。

转载请注明:静觅 » WordPress从Apache迁移到Nginx过程

喜欢 (17)or分享 (0)

我的个人微信公众号,联系我请直接在公众号留言即可~

扫码或搜索:进击的Coder

进击的Coder

微信公众号 扫一扫关注

想结交更多的朋友吗?

来进击的Coder瞧瞧吧

进击的Coder

QQ群号 99350970 立即加入

进击的Coder灌水太多?

这里是纯粹的技术领地

激进的Coder

QQ群号 627725766 立即加入

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请狠狠点击下面的

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(163)个小伙伴在吐槽
  1. The data mentioned inside the write-up are some of the ideal offered
    gucci handbags2019-01-16 05:39 (3小时前)回复
  2. I just wanted to compose a message to be able to appreciate you for the lovely concepts you are sharing at this website. My prolonged internet research has now been paid with wonderful insight to share with my pals. I would assume that many of us visitors actually are unquestionably endowed to dwell in a fine place with so many special professionals with insightful methods. I feel very privileged to have discovered the weblog and look forward to many more entertaining minutes reading here. Thanks once again for all the details.
    golden goose outlet2019-01-16 02:49 (6小时前)回复
  3. Wonderful story, reckoned we could combine a number of unrelated information, nonetheless genuinely really worth taking a search, whoa did one understand about Mid East has got much more problerms at the same time
    нёбо где находится2019-01-15 17:37 (15小时前)回复
  4. I want to express thanks to you for bailing me out of this situation. After looking through the search engines and meeting thoughts which are not helpful, I thought my life was well over. Existing devoid of the approaches to the problems you have solved as a result of your entire site is a crucial case, as well as the kind that could have negatively damaged my career if I had not come across the blog. Your primary mastery and kindness in playing with all the pieces was precious. I'm not sure what I would have done if I hadn't encountered such a step like this. It's possible to at this moment look ahead to my future. Thank you very much for this specialized and amazing help. I won't think twice to endorse the website to anyone who would need guidance on this subject.
    yeezy boost 3502019-01-15 17:13 (16小时前)回复
  5. Every when inside a while we pick out blogs that we read. Listed below are the most current web sites that we decide on
    loan amortization schedule calculator2019-01-15 13:36 (19小时前)回复
  6. I am writing to make you understand of the remarkable discovery my child encountered studying your blog. She noticed a lot of things, most notably what it is like to have a marvelous teaching nature to get certain people easily learn a variety of very confusing topics. You undoubtedly exceeded people's desires. Many thanks for imparting those good, trustworthy, revealing not to mention easy thoughts on the topic to Lizeth.
    adidas ultra boost2019-01-15 08:24 (1天前)回复
  7. The data talked about within the article are a number of the most beneficial accessible
    sweet Sofia cam2019-01-15 03:57 (1天前)回复
  8. I'm writing to let you be aware of of the fine discovery our girl experienced using the blog. She came to find some pieces, which included what it is like to have a very effective helping spirit to have others with no trouble comprehend chosen complicated issues. You really exceeded visitors' expectations. I appreciate you for delivering these practical, healthy, explanatory and in addition easy tips about the topic to Emily.
    kate spade handbags2019-01-15 00:31 (1天前)回复
  9. below youll obtain the link to some websites that we assume you need to visit
    sexy gaming Chat2019-01-14 21:27 (1天前)回复
  10. we like to honor numerous other web sites around the web, even if they arent linked to us, by linking to them. Underneath are some webpages really worth checking out
    sexy Latina duo2019-01-14 21:24 (1天前)回复
  11. Sites of interest we've a link to
    attacco di panico2019-01-14 13:09 (2天前)回复
  12. Thank you so much for giving everyone an extremely brilliant possiblity to read in detail from this blog. It really is so useful plus packed with a lot of fun for me personally and my office peers to search your site more than thrice weekly to study the newest items you will have. And lastly, I'm just certainly fulfilled with the extraordinary principles served by you. Some 1 areas in this posting are in truth the most impressive we've ever had.
    yeezy boost 350 v22019-01-14 06:10 (2天前)回复
  13. I would like to show some appreciation to the writer just for bailing me out of this type of circumstance. As a result of checking throughout the the web and coming across advice that were not beneficial, I figured my life was gone. Existing minus the solutions to the problems you've solved by way of the site is a serious case, as well as ones that would have in a wrong way affected my entire career if I hadn't come across your web page. Your good natural talent and kindness in maneuvering all the pieces was crucial. I'm not sure what I would have done if I had not discovered such a stuff like this. I can at this point look forward to my future. Thanks a lot very much for the skilled and amazing guide. I will not hesitate to recommend the blog to any individual who would need tips on this area.
    reebok outlet2019-01-13 10:30 (3天前)回复
1 9 10 11