

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ssh &#8211; Max的程式語言筆記</title>
	<atom:link href="https://stackoverflow.max-everyday.com/tag/ssh/feed/" rel="self" type="application/rss+xml" />
	<link>https://stackoverflow.max-everyday.com</link>
	<description>我要當一個豬頭，快樂過每一天</description>
	<lastBuildDate>Mon, 15 Mar 2021 23:33:03 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://stackoverflow.max-everyday.com/wp-content/uploads/2017/02/max-stackoverflow-256.png</url>
	<title>ssh &#8211; Max的程式語言筆記</title>
	<link>https://stackoverflow.max-everyday.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>轉換WordPress 到新的伺服器</title>
		<link>https://stackoverflow.max-everyday.com/2021/02/transfer-wordpress-to-new-server/</link>
					<comments>https://stackoverflow.max-everyday.com/2021/02/transfer-wordpress-to-new-server/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 24 Feb 2021 20:57:45 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[ssh]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=3678</guid>

					<description><![CDATA[Max遇到一個問題，就是WordPress 所安...]]></description>
										<content:encoded><![CDATA[
<p>Max遇到一個問題，就是WordPress 所安裝的伺服器的「建立資料庫連線時，發生錯誤」常會掛掉，也找不到是什麼原因（可能是沒有認真去找）。</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="960" height="718" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2021/02/18886.jpg" alt="" class="wp-image-3680" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2021/02/18886.jpg?v=1614176018 960w, https://stackoverflow.max-everyday.com/wp-content/uploads/2021/02/18886-600x449.jpg?v=1614176018 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2021/02/18886-768x574.jpg?v=1614176018 768w" sizes="(max-width: 960px) 100vw, 960px" /></figure>



<hr class="wp-block-separator"/>



<p>先換一台新的伺服器看看，有沒有辦法改善，下面就稱舊的伺服器為A服，新的伺服器為B服。</p>



<p>與 mysql database 連線有關文章：<br>Too many connections<br><a href="https://dev.mysql.com/doc/refman/8.0/en/too-many-connections.html">https://dev.mysql.com/doc/refman/8.0/en/too-many-connections.html</a></p>



<p>新的伺服器，如果RAM 較少，可以先設定 swap:<br>Linux的 VPS 上設定虛擬記憶體 (Swap)<br><a href="https://stackoverflow.max-everyday.com/2016/05/linux-vps-swap/">https://stackoverflow.max-everyday.com/2016/05/linux-vps-swap/</a></p>



<p>linux下有有文件數操作最大限制，超過限制（同時開啟1024個檔案）會導致程式異常，會發生這個問題通常時程式沒寫好，使用靜態方法打開文件時忘記關閉。兩種解決方法，一是設置linux的最大文件打開數量（無法根本解決問題），二是解決程序中的bugs，即消除有問題的代碼。</p>



<p>請先透過下面的檢查方法來設定伺服器：<br>How to change open_files_limit in MySQL<br><a href="https://stackoverflow.max-everyday.com/2021/02/how-to-change-open_files_limit-in-mysql/">https://stackoverflow.max-everyday.com/2021/02/how-to-change-open_files_limit-in-mysql/</a></p>



<p>解決 MySQL 連接出現 Too many connections<br><a href="https://stackoverflow.max-everyday.com/2017/12/mysql-too-many-connections/">https://stackoverflow.max-everyday.com/2017/12/mysql-too-many-connections/</a></p>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">1：登入A服，設定從A服可以ssh 到B服。</h2>



<p>可以參考看看這篇文章：How To Use SSH Keys connect to Linux server<br><a href="https://stackoverflow.max-everyday.com/2017/12/how-to-use-ssh-keys-to-linux-server/">https://stackoverflow.max-everyday.com/2017/12/how-to-use-ssh-keys-to-linux-server/</a></p>



<p><strong>1-1： 在 A服執行：</strong></p>



<pre class="wp-block-preformatted">ssh-keygen -t rsa</pre>



<p>即可產生相關的key，畫面的訊息：</p>



<pre class="wp-block-preformatted">Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.</pre>



<p><strong>1-2：在 A服裡執行：</strong></p>



<pre class="wp-block-preformatted">cat ~/.ssh/id_rsa.pub</pre>



<p>取得public key 並到，再把內容都加入(append)到要被 ssh 連進去的 server 的 ~/.ssh/authorized_keys 檔案裡，就可以讓其他台電腦不用打密碼連進 ssh server 了。</p>



<p>附註：如你是使用DigitalOcean 的服務，而且建立的伺服器較早（於2020年10月），需要先搬家到VPC裡，方法很簡單，先產生 snapshot 後，再透過 snapshot create droplet 這樣就完成了，大約 5分鐘內完成，詳細圖文攻略：<br><a href="https://www.digitalocean.com/docs/networking/vpc/how-to/migrate-resources/">https://www.digitalocean.com/docs/networking/vpc/how-to/migrate-resources/</a></p>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">2：搬資料庫</h2>



<p>安裝mysql</p>



<pre class="wp-block-preformatted">sudo apt-get update
sudo apt-get install mysql-server</pre>



<p>如果你的Ubuntu 版本太舊，無法安裝 mysql 8.0 以後版本，參考看看這篇文章：<br><a href="https://stackoverflow.max-everyday.com/2019/10/ubuntu-18-install-mysql-8/">https://stackoverflow.max-everyday.com/2019/10/ubuntu-18-install-mysql-8/</a></p>



<p>設定安全性和root 密碼的指令：</p>



<pre class="wp-block-preformatted">mysql_secure_installation</pre>



<p>在出現的第二個問題：「選擇是否禁止 root 遠程登入」，建議輸入”Y”，讓 root 只能准許從 localhost 連線，以防從主機外部的連線進來。</p>



<p>在A服匯出 DB:</p>



<pre class="wp-block-preformatted">mysqldump -u root -p --opt [database name] &gt; newdatabase.sql</pre>



<p>在A服複製到 B服：</p>



<pre class="wp-block-preformatted">scp newdatabase.sql root@10.130.0.2:~/</pre>



<p>附註：要指定B服的port, 可以使用 -p 參數，如果不是使用預設的 22 port.</p>



<p>在B服做匯入：</p>



<pre class="wp-block-preformatted">mysql -u root -p newdatabase &lt; /path/to/newdatabase.sql</pre>



<p>搬DB 的相關文章：<br><a href="https://stackoverflow.max-everyday.com/2017/04/mysql-dump-restore/">https://stackoverflow.max-everyday.com/2017/04/mysql-dump-restore/</a></p>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">3：搬需要的檔案</h2>



<p>複製網頁和相關附件檔案</p>



<pre class="wp-block-preformatted">scp -pr /var/www root@10.130.0.2:/var/www</pre>



<p>附註：建議走內網來傳輸檔案，速度會較走外網來的快，如果二台伺服器有辦法在同一個LAN裡。</p>



<p>改變實體檔案擁有者</p>



<pre class="wp-block-preformatted">chown -R www-data:www-data /var/www/*</pre>



<p>複製 Apache web server 的相關站台設定：</p>



<pre class="wp-block-preformatted">scp -pr /etc/apache2/<strong>sites-available</strong> root@10.130.0.2:/etc/apache2</pre>



<p>/etc/apache2/sites-enabled 的目錄，比較麻煩一點，需要先 tar 起來，再解壓縮。</p>



<p>先在 A服 apache2 目錄下「壓縮」執行指令：</p>



<pre class="wp-block-preformatted">tar -czvf enable.tgz sites-enabled</pre>



<p>搬 Apache web server 設定檔案：</p>



<pre id="block-9df13f4c-1bf5-45fc-9efc-acbc50fc9ff7" class="wp-block-preformatted">scp /etc/apache2/<strong>enable.tgz</strong> root@10.130.0.2:/etc/apache2</pre>



<p>先在 B服 apache2 目錄下「解壓縮」執行指令：</p>



<pre id="block-a4ec352f-e571-4038-af17-b630c8accda2" class="wp-block-preformatted">tar -xzvf enable.tgz</pre>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">4：申請與設定SSL憑證</h2>



<p>需要使用 snap 指令，取得 certbot，以取得免費的 SSL 憑證。<br><a href="https://stackoverflow.max-everyday.com/2017/01/lets-encrypt-free-https/">https://stackoverflow.max-everyday.com/2017/01/lets-encrypt-free-https/</a></p>



<p>在申請憑證前，請先到 Cloudflare 關閉快取，改成僅DNS(only DNS).<br><a href="https://dash.cloudflare.com/">https://dash.cloudflare.com/</a></p>



<p>如果上面的第3步，你有使用到之前的 apaches 的 mods-enables 的設定，請先拿掉 ssl.load 和 ssl.conf 這 2個 enable。建議只要把舊的站台的 sites-available 與 sites-enabled 搬過來就好了，mods 不要直接套到新的伺服器，因為有會比較多問題，尤其是 php 的版本在 mods 會不太一樣。</p>



<p>接下來就可以使用 certbot 指令來取得新的憑證檔。</p>



<p>都取得憑證後，希望 php 可以先執行，請修改 mods-available 的 dir.conf 為：</p>



<pre class="wp-block-preformatted"> &lt;IfModule mod_dir.c&gt;
 &nbsp; &nbsp; &nbsp; &nbsp; <strong>DirectoryIndex</strong> index.php index.html index.cgi index.pl index.xhtml index.htm
 &lt;/IfModule&gt; </pre>



<p>說明：把  index.php 調整為第一順位。</p>



<p>接著在 mods-enabled 目錄下執行下列指令，讓網站 redirect 到 ssl 優先：</p>



<pre class="wp-block-preformatted">ln -s ../mods-available/socache_shmcb.load socache_shmcb.load
ln -s ../mods-available/ssl.load ssl.load
ln -s ../mods-available/ssl.conf ssl.conf</pre>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">後記</h2>



<p>雖然上面這樣修改，似乎暫時解決了問題，但實際上系統運行一陣子還是掛掉，掛掉的原因是 out of memory, 是「整個系統」當機，目前測試用的環境已都使用最新的版本：</p>



<ul class="wp-block-list"><li>Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-66-generic x86_64)</li><li>Server version: 8.0.23-0ubuntu0.20.04.1 (Ubuntu)</li><li>PHP 7.4.3 (cli) (built: Oct  6 2020 15:47:56) ( NTS )</li><li>WordPress 5.7</li></ul>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">相關文章</h2>



<p>Linux的 VPS 上設定虛擬記憶體 (Swap)<br><a href="https://stackoverflow.max-everyday.com/2016/05/linux-vps-swap/">https://stackoverflow.max-everyday.com/2016/05/linux-vps-swap/</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2021/02/transfer-wordpress-to-new-server/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
