Apache, MySQL, Linux 管理與維運常用指令

Apache, MySQL, Linux 管理與維運常用指令

這篇文章主要列出管理與維運 Apache or MySQL 的常用指令。

 

常用的 Log Query

為什麼要 Query 這些 Log 呢?

因為上線前後我們會檢查是否有一些異常狀況,這時候就可以查詢相關的 Log

PHP, Apache, 可以查詢與建議的 Log 舉例

 

  • tail -f /var/log/d/access_log
  • tail -f /var/log/httpd/error_log
    tail -f /var/log/messages
    tail -f /var/log/httpd/ssl_error_log
    tail -f /var/www/html/dp/application/logs/log-2013-09-03.php
    find / -name log-2013-08-06.php
    /var/www/html/dp.xx/application/logs/
  • tail -f /var/log/httpd/access_log

 

Apache/Linux 系統紀錄檔說明

/etc/httpd/conf/httpd.conf : Apache configuration file
/var/log/secure:記錄登入系統存取資料的檔案,例如 pop3, ssh, telnet, ftp 等都會記錄在此檔案中;
/var/log/wtmp:記錄登入者的訊息資料,由於本檔案已經被編碼過,所以必須使用 last 這個指令來取出檔案的內容;
/var/log/messages:這個檔案相當的重要,幾乎系統發生的錯誤訊息(或者是重要的資訊)都會記錄在這個檔案中;
/var/log/boot.log:記錄開機或者是一些服務啟動的時候,所顯示的啟動或關閉訊息;
/var/log/maillog 或 /var/log/mail/*:紀錄郵件存取或往來( sendmail 與 pop3 )的使用者記錄;
/var/log/cron:這個是用來記錄 crontab 這個例行性服務的內容的!
/var/log/httpd, /var/log/news, /var/log/mysqld.log, /var/log/samba, /var/log/procmail.log: 分別是幾個不同的網路服務的記錄檔

 

產生1M檔案十次

什麼情境會使用這個呢?

當要模擬磁碟存取或是磁碟空間實的效能測試,我們就可以用這個指令,大量產生需要的測試檔案。

  • dd if=/dev/urandom of=hihi.txt bs=1M count=10

Apache狀態相關指令

/etc/init.d/httpd start
/etc/init.d/httpd stop
/etc/init.d/httpd restart
/etc/init.d/httpd status

MySQL 相關指令

service mysql restart

檢查 HTTP service status

ps auxww|grep httpd|wc -l 214

 

檢查 Virtual Memory usage

vmstat

參考資料

http://www.maketecheasier.com/linux-running-too-slow-heres-how-to-find-the-cause/2011/09/29

http://www.tecmint.com/command-line-tools-to-monitor-linux-performance/

http://www.linuxjournal.com/article/9001?page=0,0

列出log 中有 Error 的關鍵字

cat /var/www/html/dp/application/logs/log-2013-08-23.php | grep Error

 

修改 Apache Configuration

主要透過這些設定值的修改,讓 User experiences 更好。

編輯 Apcahe 設定檔案  /etc/httpd/conf/httpd.conf

設定 Apache 連線狀況

KeepAlive On
KeepAliveTimeout 60
MaxKeepAliveRequests 0
<IfModule prefork.c>
StartServers 8
MinSpareServers 5
MaxSpareServers 20
ServerLimit 10240
MaxClients 10240
MaxRequestsPerChild 10240
</IfModule>

設定網頁的壓縮

透過壓縮的處理,讓檔案在網路傳輸時間減少。

DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/x-javascript
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

設定網頁的快取

讓下一次網站讀取時,透過瀏覽器的快取機制,減少存取的需求。
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/x-icon “access plus 1 week”
ExpiresByType image/gif “access plus 1 week”
ExpiresByType image/jpg “access plus 1 week”
ExpiresByType image/jpeg “access plus 1 week”
ExpiresByType image/png “access plus 1 week”
ExpiresByType text/javascript “access plus 3 days”
ExpiresByType text/js “access plus 3 days”
ExpiresByType application/javascript “access plus 3 days”
ExpiresByType application/x-javascript “access plus 3 days”
ExpiresByType text/css “access plus 3 days”
ExpiresByType text/x-json “access plus 3 days”
</IfModule>

 

Apache Log

透過修改 Apache Log 的設定,可以讓我們知道每一個連線的執行時間。

增加 %D 到Apache Configure to 觀察執行時間
vim /etc/httpd/conf/httpd.conf
%h %D %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\” combined

/etc/init.d/httpd restart

 

Linux 網路設定

增加或修改net.ipv4.tcp_tw值:
vim /etc/sysctl.conf
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 5000
修改該 TCP keepAlive Timoout value
echo 2400 > /proc/sys/net/ipv4/tcp_keepalive_time

修改 TCP Timeout Value
more /proc/sys/net/ipv4/tcp_fin_timeout
echo “15” > /proc/sys/net/ipv4/tcp_fin_timeout
/etc/rc.d/init.d/network restart
使內核參數生效:
sysctl -p
/sbin/sysctl -p
/etc/rc.d/init.d/network restart

 

檢查 Linux相關系統狀態

 

檢查 TCP connection 狀態
netstat -nat|awk ‘{print awk $NF}’|sort|uniq -c|sort -n
netstat -n | awk ‘/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}’
netstat -nato

檢查跟 106/107 IP source connection 連線狀態
netstat -n|grep “10.42.226.106:80” |awk -F ‘[:| ]+’ ‘{print $8,$10}’|sort | uniq -c|more
netstat -n|grep “10.42.226.107:80” |awk -F ‘[:| ]+’ ‘{print $8,$10}’|sort | uniq -c|more

 

檢查process狀態

pstree

檢查 io狀態

iostat

 

檢查啟動memcached service狀態

service memcached status
service memcached start

 

修改 php.ini

vim /etc/php.ini
realpath_cache_size = 102400k
realpath_cache_ttl = 1200

 

檢查nfs state

nfsstat

檢查connection total time

tail -f /var/www/html/dp/application/logs/log-2013-09-11.php | grep Total

 

修改 memecroy chace

vim /etc/sysconfig/memcached
CACHESIZE=1024
/etc/init.d/memcached restart
ps aux | grep memca

 

修改檔案開啟的上限數

ulimit -n 1024000
ulimit -u 6000
prctl -n process.max-file-descriptor -t basic -v 1024000 -r -i process $$

 

修改 mySQL settings

MySQL可以透過一些設定值作效能的優化,以下是筆者會檢查的設定值

vim /etc/my.cnf

  • max_connections = 6000
    thread_cache_size = 5500
    table_open_cache = 5000
    open-files-limit = 5000
    sort_buffer_size= from 8M to 2M
    join_buffer_size = from 32M to 2M

設定完之後要重新啟動MySQL服務

service mysql restart

 

MySQL設定建議

筆者推薦這個網站服務。
https://tools.percona.com/wizard

 

Crontab for Apache restart

每五分種檢查 Apache service status,如果偵測到 httpd 服務沒有被啟動的話,就重新啟動。

將ApacheRestart.sh設定為每五分鐘執行一次

crontab -e
*/5 * * * * /etc/ApacheRestart.sh >/dev/null 2>&1

 

ApacheRestart.sh 程式內容

vim /etc/ApacheRestart.sh

RESTART=”/etc/init.d/httpd restart”
PGREP=”/usr/bin/pgrep”
HTTPD=”httpd”
$PGREP ${HTTPD}
if [ $? -ne 0 ]
then
$RESTART
fi

chmod +x ApacheRestart.sh

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *