Files
pages/2020/01/21/devops.html
2026-01-17 09:36:00 +00:00

331 lines
21 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- Begin Jekyll SEO tag v2.8.0 -->
<title>Mayx的运维笔记 - 应用优化 | Mayx的博客</title>
<meta name="generator" content="Jekyll v3.9.5" />
<meta property="og:title" content="Mayx的运维笔记 - 应用优化" />
<meta name="author" content="mayx" />
<meta property="og:locale" content="zh_CN" />
<meta name="description" content="优化?能不负优化就够好了!" />
<meta property="og:description" content="优化?能不负优化就够好了!" />
<meta property="og:site_name" content="Mayx的博客" />
<meta property="og:type" content="article" />
<meta property="article:published_time" content="2020-01-21T00:00:00+08:00" />
<meta name="twitter:card" content="summary" />
<meta property="twitter:title" content="Mayx的运维笔记 - 应用优化" />
<meta name="google-site-verification" content="huTYdEesm8NaFymixMNqflyCp6Jfvd615j5Wq1i2PHc" />
<meta name="msvalidate.01" content="0ADFCE64B3557DC4DC5F2DC224C5FDDD" />
<meta name="yandex-verification" content="fc0e535abed800be" />
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"BlogPosting","author":{"@type":"Person","name":"mayx"},"dateModified":"2020-01-21T00:00:00+08:00","datePublished":"2020-01-21T00:00:00+08:00","description":"优化?能不负优化就够好了!","headline":"Mayx的运维笔记 - 应用优化","mainEntityOfPage":{"@type":"WebPage","@id":"/2020/01/21/devops.html"},"publisher":{"@type":"Organization","logo":{"@type":"ImageObject","url":"https://avatars0.githubusercontent.com/u/17966333"},"name":"mayx"},"url":"/2020/01/21/devops.html"}</script>
<!-- End Jekyll SEO tag -->
<link rel="canonical" href="https://mabbs.github.io/2020/01/21/devops.html" />
<link type="application/atom+xml" rel="alternate" href="/atom.xml" title="Mayx的博客" />
<link rel="alternate" type="application/rss+xml" title="Mayx的博客(RSS)" href="/rss.xml" />
<link rel="alternate" type="application/json" title="Mayx的博客(JSON Feed)" href="/feed.json" />
<link rel="stylesheet" href="/assets/css/style.css?v=1768642553" />
<!--[if !IE]> -->
<link rel="stylesheet" href="/Live2dHistoire/live2d/css/live2d.css" />
<!-- <![endif]-->
<link rel="search" type="application/opensearchdescription+xml" href="/opensearch.xml" title="Mayx的博客" />
<link rel="webmention" href="https://webmention.io/mabbs.github.io/webmention" />
<link rel="pingback" href="https://webmention.io/mabbs.github.io/xmlrpc" />
<link rel="preconnect" href="https://summary.mayx.eu.org" crossorigin="anonymous" />
<link rel="prefetch" href="https://www.blogsclub.org/badge/mabbs.github.io" as="image" />
<link rel="blogroll" type="text/xml" href="/blogroll.opml" />
<link rel="me" href="https://github.com/Mabbs" />
<script src="/assets/js/jquery.min.js"></script>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-ajaxtransport-xdomainrequest/1.0.3/jquery.xdomainrequest.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script>
var lastUpdated = new Date("Sat, 17 Jan 2026 17:35:53 +0800");
var BlogAPI = "https://summary.mayx.eu.org";
</script>
<script src="/assets/js/main.js"></script>
<!--[if !IE]> -->
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-137710294-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-137710294-1');
</script>
<script src="/assets/js/instant.page.js" type="module"></script>
<!-- <![endif]-->
</head>
<body>
<!--[if !IE]> --><noscript><marquee style="top: -15px; position: relative;"><small>发现当前浏览器没有启用JavaScript这不影响你的浏览但可能会有一些功能无法使用……</small></marquee></noscript><!-- <![endif]-->
<!--[if IE]><marquee style="top: -15px; position: relative;"><small>发现当前浏览器为Internet Explorer这不影响你的浏览但可能会有一些功能无法使用……</small></marquee><![endif]-->
<div class="wrapper">
<header class="h-card">
<h1><a class="u-url u-uid p-name" rel="me" href="/">Mayx的博客</a></h1>
<img src="https://avatars0.githubusercontent.com/u/17966333" fetchpriority="high" class="u-photo" alt="Logo" style="width: 90%; max-width: 300px; max-height: 300px;" />
<p class="p-note">Mayx's Home Page</p>
<form action="/search.html">
<input type="text" name="keyword" id="search-input-all" placeholder="Search blog posts.." />&#160;<input type="submit" value="搜索" />
</form>
<br />
<p class="view"><a class="u-url" href="/Mabbs/">About me</a></p>
<ul class="downloads">
<li style="width: 270px; border-right: none;"><a href="/MayxBlog.tgz">Download <strong>TGZ File</strong></a></li>
</ul>
</header>
<section class="h-entry">
<small><time class="date dt-published" datetime="2020-01-21T00:00:00+08:00">21 January 2020</time> - 字数统计1924 - 阅读大约需要6分钟 - Hits: <span id="/2020/01/21/devops.html" class="visitors">Loading...</span></small>
<h1 class="p-name">Mayx的运维笔记 - 应用优化</h1>
<p class="view">by <a class="p-author h-card" href="//github.com/Mabbs">mayx</a></p>
<div id="outdate" style="display:none;">
<hr /><p>
这是一篇创建于 <span id="outime"></span> 天前的文章,其中的信息可能已经有所发展或是发生改变。
</p>
</div>
<script>
daysold = Math.floor((new Date().getTime() - new Date("Tue, 21 Jan 2020 00:00:00 +0800").getTime()) / (24 * 60 * 60 * 1000));
if (daysold > 90) {
document.getElementById("outdate").style.display = "block";
document.getElementById("outime").innerHTML = daysold;
}
</script>
<hr />
<b>AI摘要</b>
<p id="ai-output">这篇文章记录了作者对Mayx论坛应用进行优化的过程。首先尝试更换了图片处理库从GD库改为ImageMagick库但遇到了图片缩略图显示问题怀疑是数据库问题最终恢复默认设置。然后尽管对Redis缓存有所期待但配置过程遇到困难特别是与/tmp/文件夹权限相关的问题。作者将Redis配置到/run/文件夹后问题得以解决但并未显著提升性能反而因请求量激增而发现优化的必要性。此外还调整了Discuz的权限设置和发帖限制以应对可能存在的恶意操作。作者最后感慨优化过程中的挫折和提升自身技能的需求。</p>
<hr />
<ul><li><a href="#继续优化">继续优化!</a></li><li><a href="#关于图片处理库的优化">关于图片处理库的优化</a></li><li><a href="#关于缓存的优化">关于缓存的优化</a><ul><li><a href="#历经艰辛的redis之旅">历经艰辛的Redis之旅</a></li></ul></li><li><a href="#其他的杂事">其他的杂事</a></li></ul>
<hr />
<main class="post-content e-content" role="main"><p>优化?能不负优化就够好了!<!--more--></p>
<h1 id="继续优化">
<a href="#继续优化"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 继续优化!
</h1>
<p>接着<a href="/2020/01/05/devops.html">上一篇</a>的重建我在上一次做了一些系统环境上的优化不过论坛中用的Discuz系统也算是一个成熟的应用了 <del>(可惜再成熟也不会自己写代码😂)</del> 所以这次我打算在Discuz这个应用上做优化。 </p><p>
不过毕竟以我的PHP水平还做不到直接在源代码上进行修改所以我打算使用Discuz本身有的优化功能上进行优化。</p>
<h1 id="关于图片处理库的优化">
<a href="#关于图片处理库的优化"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 关于图片处理库的优化
</h1>
<p>在Discuz的后台面板中我首先看到的关于性能优化方面的地方就是图片处理库 <del>(尽管它不是最先出现的)</del>。看旁边的介绍中说默认的GD库似乎更加消耗系统资源而相比之下ImageMagick库会好一些既然它都这么说了那我肯定是毫不犹豫的选择了ImageMagick库嘛。 </p><p>
当然事实上我也确实是这么做的然而这个操作貌似发生了一些不可预料的问题。修改了这个选项后我开始逛论坛但是我发现论坛中不少帖子的图片缩略图显示出现了问题这时候我感觉不大对劲首先我确定了一下Vultr中的LEMP肯定是有装ImageMagick库的那问题肯定不是我PHP环境的问题这时候我怀疑是数据库保存的东西可能有问题。不过这个问题就超出了我的解决范围没办法我只好又把选项还原回去了😓反正它这么多年也都过来了应该影响没多大吧。</p>
<h1 id="关于缓存的优化">
<a href="#关于缓存的优化"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 关于缓存的优化
</h1>
<p>在之前的花火学园中我也用了Discuz的缓存引擎是MemCache。事实上在LEMP默认是不提供MemCache的扩展反倒是有Redis的扩展。不过我看着Redis那红色的图标不知道为什么心里总有一种抵触感后来经过了解Redis似乎要比MemCache的性能好的多我也就半信半疑的打算装一下试试。 </p><p>
不过事实上我并不怎么相信这种缓存能提升多少性能只是看着网上说着各种加速到0.005s让人非常的心动。毕竟我的服务器内存并不大总共也就1个G另外还加了Swap。如果缓存占用的内存很多分配来分配去又回到Swap所在的硬盘那我优化岂不是失去了意义不过心里总是被别人的博客搞的很羡慕所以我开始研究怎么样搞Redis缓存。</p>
<h2 id="历经艰辛的redis之旅">
<a href="#历经艰辛的redis之旅"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 历经艰辛的Redis之旅
</h2>
<p>Redis并不是很难安装一句<code class="language-plaintext highlighter-rouge">yum install redis</code>就搞定了,主要难的地方还是在于配置。 </p><p>
我知道这时候只要在Discuz的配置文件里加一句<code class="language-plaintext highlighter-rouge">$_config['memory']['redis']['server'] = '127.0.0.1';</code>然后再以默认的方式启动Redis服务就OK了。但是既然之前我说过在内部的服务中不应该使用TCP/IP sockets那我肯定是不能允许它就用默认的配置工作。 </p><p>
在Redis中倒是有支持UNIX domain sockets的选项也支持关闭监听TCP/IP Port。我按照它上面的说明配置了然后信心满满的在配置文件中设定好了。然而现实告诉我没那么简单我写了一个测试脚本它返回了一个<code class="language-plaintext highlighter-rouge">No such file or directory</code>我反复确认路径保证自己没有手残到把路径写错然后上网搜这个错误查了半天也没有什么有建设性的结果……我甚至把权限都设置为777把php-fpm加到了redis的组里都没有任何用处另外我也用redis-cli确定了那个文件确实是一个socket文件。这时候我实在是无计可施了我打算放弃就这样屈服于TCP/IP sockets然而我的强迫症和其他博客里中说的10ms不允许我放弃没办法我只好仔细思考了。 </p><p>
我觉得这个问题应该还是在于权限只是这个问题过于奇妙让我无法理解。按照默认的配置那个socket文件在/tmp/文件夹下因为PHP在上传文件的时候也用到了这个文件夹所以我也没怎么怀疑它。但是我的mysql同样也用了UNIX domain sockets只是文件在/run/下它也没出问题啊我想了想也许把这个socket文件放到/run/文件夹下就可以正常工作了。没想到还真是这个问题,随着我把配置文件中的路径修改了,它也能正常工作了……我真的是无言以对…… </p><p>
在这之后我把redis服务设置成了开机自启动然后这段优化就到此为止了。</p>
<h1 id="其他的杂事">
<a href="#其他的杂事"><svg class='octicon' viewBox='0 0 16 16' version='1.1' width='16' height='32' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg></a> 其他的杂事
</h1>
<p>其他也没什么特别多可以优化的地方,后来我查了一下,发现花火学园的请求在那天出现问题的时候请求量突然涨了一倍,也许是这个原因把之前的花火学园逼停了,所以优化还是挺重要的嘛。 </p><p>
其他方面就是对于Discuz权限的一些调整不知道为什么总有些人要在试贴区水贴真的是无法理解它们的操作另外就是连发贴的问题我感觉估计有人用发帖机搞我们的论坛所以又提高一点发帖的门槛。 </p><p>
其实这次也没什么大的改动就是这个Redis搞的我真是当场爆炸看来我还是要提高一下我的姿势水平啊23333。</p></main>
<small style="display: block">tags: <a rel="category tag" class="p-category" href="/search.html?keyword=%E8%BF%90%E7%BB%B4"><em>运维</em></a> - <a rel="category tag" class="p-category" href="/search.html?keyword=%E7%AC%94%E8%AE%B0"><em>笔记</em></a> - <a rel="category tag" class="p-category" href="/search.html?keyword=%E4%BC%98%E5%8C%96"><em>优化</em></a> <span style="float: right;"><a href="https://gitlab.com/mayx/mayx.gitlab.io/tree/master/_posts/2020-01-21-devops.md">查看原始文件</a></span></small>
<h4 style="border-bottom: 1px solid #e5e5e5;margin: 2em 0 5px;">推荐文章</h4>
<p id="suggest-container">Loading...</p>
<script>
var suggest = $("#suggest-container");
$.get(BlogAPI + "/suggest?id=/2020/01/21/devops.html&update=" + lastUpdated.valueOf(), function (data) {
if (data.length) {
getSearchJSON(function (search) {
suggest.empty();
var searchMap = {};
for (var i = 0; i < search.length; i++) {
searchMap[search[i].url] = search[i];
}
var tooltip = $('<div class="content-tooltip"></div>').appendTo('body').hide();
for (var j = 0; j < data.length; j++) {
var item = searchMap[data[j].id];
if (item) {
var link = $('<a href="' + item.url + '">' + item.title + '</a>');
var contentPreview = item.content.substring(0, 100);
if (item.content.length > 100) {
contentPreview += "……";
}
link.hover(
function(e) {
tooltip.text($(this).data('content'))
.css({
top: e.pageY + 10,
left: e.pageX + 10
})
.show();
},
function() {
tooltip.hide();
}
).mousemove(function(e) {
tooltip.css({
top: e.pageY + 10,
left: e.pageX + 10
});
}).data('content', contentPreview);
suggest.append(link);
suggest.append(' - ' + item.date + '<br />');
}
}
});
} else {
suggest.html("暂无推荐文章……");
}
});
</script>
<br />
<div class="pagination">
<span class="prev">
<a href="/2020/01/05/devops.html">
上一篇Mayx的运维笔记
</a>
</span>
<br />
<span class="next">
<a href="/2020/02/02/old.html">
下一篇:过去与未来的科技变化
</a>
</span>
</div>
<!--[if !IE]> -->
<link rel="stylesheet" href="/assets/css/gitalk.css">
<script src="/assets/js/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
var gitalk = new Gitalk({
clientID: '36557aec4c3cb04f7ac6',
clientSecret: 'ac32993299751cb5a9ba81cf2b171cca65879cdb',
repo: 'mabbs.github.io',
owner: 'Mabbs',
admin: ['Mabbs'],
id: '/2020/01/21/devops', // Ensure uniqueness and length less than 50
distractionFreeMode: false, // Facebook-like distraction free mode
proxy: "https://cors-anywhere.mayx.eu.org/?https://github.com/login/oauth/access_token"
})
gitalk.render('gitalk-container')
</script>
<!-- <![endif]-->
</section>
<!--[if !IE]> -->
<div id="landlord" style="left:5px;bottom:0px;">
<div class="message" style="opacity:0"></div>
<canvas id="live2d" width="500" height="560" class="live2d"></canvas>
<div class="live_talk_input_body">
<form id="live_talk_input_form">
<div class="live_talk_input_name_body" >
<input type="checkbox" id="load_this" />
<input type="hidden" id="post_id" value="/2020/01/21/devops.html" />
<label for="load_this">
<span style="font-size: 11px; color: #fff;">&#160;想问这篇文章</span>
</label>
</div>
<div class="live_talk_input_text_body">
<input name="talk" type="text" class="live_talk_talk white_input" id="AIuserText" autocomplete="off" placeholder="要和我聊什么呀?" />
<button type="submit" class="live_talk_send_btn" id="talk_send">发送</button>
</div>
</form>
</div>
<input name="live_talk" id="live_talk" value="1" type="hidden" />
<div class="live_ico_box" style="display:none;">
<div class="live_ico_item type_info" id="showInfoBtn"></div>
<div class="live_ico_item type_talk" id="showTalkBtn"></div>
<div class="live_ico_item type_music" id="musicButton"></div>
<div class="live_ico_item type_youdu" id="youduButton"></div>
<div class="live_ico_item type_quit" id="hideButton"></div>
<input name="live_statu_val" id="live_statu_val" value="0" type="hidden" />
<audio src="" style="display:none;" id="live2d_bgm" data-bgm="0" preload="none"></audio>
<input id="duType" value="douqilai" type="hidden" />
</div>
</div>
<div id="open_live2d">召唤伊斯特瓦尔</div>
<!-- <![endif]-->
<footer>
<p>
<small>Made with ❤ by Mayx<br />Last updated at 2026-01-17 17:35:53<br /> 总字数614622 - 文章数178 - <a href="/atom.xml" >Atom</a> - <a href="/README.html" >About</a></small>
</p>
</footer>
</div>
<script src="/assets/js/scale.fix.js"></script>
<!--[if !IE]> -->
<script src="/assets/js/main_new.js"></script>
<script src="/Live2dHistoire/live2d/js/live2d.js"></script>
<script src="/Live2dHistoire/live2d/js/message.js"></script>
<!-- <![endif]-->
</body>
</html>