『壹』 python logging.conf是什麼類型文件
下面的函數用於配置logging模塊,它們位於logging.config模塊中。你可以使用這些函數來配置,也可以在logging或是logging.handlers中聲明它們來配置。
logging.config.dictConfig(config)
從dictionary中獲取logging配置
logging.config.fileConfig(fname, defaults=None, disable_existing_loggers=True)
從指定的fname的配置文件中讀取logging配置文件
該函數可以在應用程序中多次調用
logging.config.listen(port=DEFAULT_LOGGING_CONFIG_PORT)
在指定埠啟動socket server並偵聽新配置
logging.config.stopListening()
關閉先前正在偵聽的server
Configuration file format
被fileConfiguration()所理解的配置文件格式基於configparser功能。配置文件必須包含[loggers], [handlers]和[formatters],它們分別代表日誌文件中定義的每種類型的實體。對這3種實體,後面有一個單獨的section來定義該實體如何配置。
因此,[loggers]節中名為log01的logger,相關的配置文件細節在[logger_log01]節中定義。類似地,[handlers]節中名為
hand01的handler將在[handler_hand01]節中聲明,[formatters]節中的form01將在[formatter_form01]聲明。root logger配置必須在[logger_root]節聲明。
注意:fileConfig() API比dictConfig()舊,並不包含logging某些方面的功能。建議以後盡量使用dictConfig API。
配置文件的例子如下:
[loggers]
keys=root,log02,log03,log04,log05,log06,log07
[handlers]
keys=hand01,hand02,hand03,hand04,hand05,hand06,hand07,hand08,hand09
[formatters]
keys=form01,form02,form03,form04,form05,form06,form07,form08,form09
root logger必須指定一個級別和handlers列表。示例如下:
[logger_root]
level=NOTSET
handlers=hand01
其中level可以是DEBUG, INFO, WARNING, ERROR, CRITICAL or NOTSET之一,僅對root logger來說,NOTSET意味著所有的log message
都會記錄。對非root的logger,強制要求一些額外信息,比如
[logger_parser]
level=DEBUG
handlers=hand01
propagate=1
qualname=compiler.parser
當一個非root的logger中level被配置為NOSET,它將通過上一級的logger來確定當前logger的有效級別。propagete為1表示message必須傳播到上一級logger中,為0表示不傳。qualname表示該logger的層級channel名稱,這就是說,應用程序使用該名稱可以得到該logger對象。
handler類型很多,主要有StreamHandler,FileHandler,NullHandler,SysLogHandler,HTTPHandler等
handler節對應的配置示例如下:
[handler_hand01]
class=StreamHandler
level=NOTSET
formatter=form01
args=(sys.stdout,)
class表示該handler在logging包命名空間中的類名,level表示logger的級別,NONSET表示要記錄所有日誌。
formatter表示該handler的formatter的鍵名,假如空白的話,就使用默認值logging._defaultFormatter。假如formatter指定了該名字,必須在對應的section聲明。args欄位是handler類的構造函數的變數列表,參考相關handler構造函數,或者下面的例子,去觀察通常的元素是如何構造的。比如:
[handler_hand02]
class=FileHandler
level=DEBUG
formatter=form02
args=('python.log', 'w')
下面是formatter的配置
[formatter_form01]
format=F1 %(asctime)s %(levelname)s %(message)s
datefmt=
class=logging.Formatter
format欄位是全局格式字元串,datefmt是strftime()兼容的date/time格式字元串,為空時使用默認的ISO8601格式,比如2003-01-23 00:29:50,411,class欄位表示formatter的類名,
日誌級別如下:
Level Numeric value
CRITICAL 50
ERROR 40
WARNING 30
INFO 20
DEBUG 10
NOTSET 0
logging.handlers解讀
logging模塊中定義了這3個handler:StreamHandler, FileHandler and NullHandler
其它的handler都在logging.handler中定義,一並說明如下:
StreamHandler
該類位於logging包,將logging output輸出到流中,比如sys.stdout,sys.stderr或任何支持write()和flush()方法的類文件對象
class logging.StreamHandler(stream=None)
假如指定了stream名稱,日誌將輸出到流實例中,否則,日誌輸出到sys.stderr
FileHandler
該類位於logging包,將logging output輸出到磁碟文件中,文件默認無限增長
class logging.FileHandler(filename, mode='a', encoding=None, delay=False)
打開指定的文件並記錄日誌,假如mode沒有設置,默認使用'a'表示追加日誌到文件中。
NullHandler
該對象什麼也不處理
WatchedFileHandler
一個FileHandler實例,監視日誌文件的變化,假如文件變化了,它會關閉並重新打開,不建議在Windows下使用
文件的變化可以發生,當應用程序使用newsyslog和logrotate來實現日誌文件的回滾時。這個handle是在Unix/linux下面,監視文件是否改變。(一個文件認為改變了,假如它的device厚實inode已經改變),將舊的文件關閉,這個流打開。
class logging.handlers.WatchedFileHandler(filename[, mode[, encoding[, delay]]])
指定的文件被打開,用來記錄日誌,假如mode未指示,默認使用a
RotatingFileHandler
支持磁碟文件的回滾
class logging.handlers.RotatingFileHandler(filename, mode='a', maxBytes=0, backupCount=0, encoding=None, delay=0)
你可以使用 maxBytes和backupCount值去讓日誌文件在預設大小時回滾。只要日誌文件在長度上接近maxBytes時,就會關閉舊日誌文件,打開一個新的日誌文件,實現回滾。假如maxBytes或backupCount為0,回滾不會發生。假如backupCount非零,系統會備份舊文件,文件名後加『.1』, 『.2』 。比如,日誌文件名為app.log,backupCount為5,將會得到app.log, app.log.1, app.log.2, 直到app.log.5這6個文件。寫入日誌的文件總是app.log,當這個文件填滿時,就關閉它並重命名為app.log.1, 假如還存在app.log.1, app.log.2等文件,就逐一改名為app.log.2, app.log.3等等。
TimedRotatingFileHandler
支持在指定時間段內回滾日誌
class logging.handlers.TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False)
回滾基於when和interval設置,when指定interval的類型,參見下表,大小寫不敏感,默認按小時回滾
Value Type of interval
'S' Seconds
'M' Minutes
'H' Hours
'D' Days
'W0'-'W6' Weekday (0=Monday)
'midnight' Roll over at midnight
回滾擴展名使用strftime format %Y-%m-%d_%H-%M-%S或其頭部子字元串,當使用基於weekday的回滾時,W0表示周一,……,W6表示周日,interval的值不會用到
backupCount表示備份數,當日誌很多時,新日誌會覆蓋舊日誌,刪除邏輯使用interval值去決定刪除哪些日誌文件
utc為true,表示使用UTC時間,否則使用本地時間
SocketHandler
通過網路套接字輸出日誌,SocketHandler類的基類使用TCP socket
class logging.handlers.SocketHandler(host, port)
向指定地址和埠的遠程主機發送日誌
DatagramHandler
繼承自基類SocketHandler類,使用UDP socket發送日誌message
class logging.handlers.DatagramHandler(host, port)
SysLogHandler
發送日誌到遠程或是本地unix syslog
class logging.handlers.SysLogHandler(address=('localhost', SYSLOG_UDP_PORT), facility=LOG_USER, socktype=socket.SOCK_DGRAM)
NTEventLogHandler
發送日誌消息到本地Windows NT, Windows 2000 or Windows XP event log
class logging.handlers.NTEventLogHandler(appname, dllname=None, logtype='Application')
SMTPHandler
通過SMTP將日誌消息發送到email address
MemoryHandler
支持將日誌message緩存到內存中,周期性刷新日誌到target handler
class logging.handlers.BufferingHandler(capacity)
class logging.handlers.MemoryHandler(capacity, flushLevel=ERROR, target=None)
HTTPHandler
使用GET或是POST,將日誌message發送到web server
class logging.handlers.HTTPHandler(host, url, method='GET')
『貳』 CentOS系統怎樣安裝GitLab客戶端
環境
Requirements
軟體
版本
CentOS 6.6
Python 2.6
Ruby 2.1.5
Git 1.7.10+
Redis 2.0+
MySQL
GitLab 7-8-stable
GitLab Shell v2.6.0
yum源
為了提高軟體安裝速度,將yum源設置為阿里雲開源鏡像
$ cd /etc/yum.repos.d
$ wget -O CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo
必要軟體包
$ yum -y install libicu-devel patch gcc-c++ readline-devel zlib-devel libffi-devel openssl-devel make autoconf automake libtool bison libxml2-devel libxslt-devel libyaml-devel zlib-devel openssl-devel cpio expat-devel gettext-devel curl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker
安裝Git
// 查看當前git版本
$ git --version
// 如果小於1.7.10則先卸載
$ yum remove git
// 下載最新的git並安裝
$ wget -O git-src.zip https://github.com/git/git/archive/master.zip
$ unzip git-src.zip
$ cd git-src
$ make prefix=/usr/local all
$ make prefix=/usr/local install
$ ln -fs /usr/local/bin/git* /usr/bin/
安裝Ruby環境
$ mkdir /tmp/ruby && cd /tmp/ruby
$ curl --progress ftp://ftp.ruby-lang.org/pub/ruby/ruby-2.1.5.tar.gz | tar xz
$ cd ruby-2.1.5
$ ./configure --disable-install-rdoc
$ make && make install
$ ln -s /usr/local/bin/ruby /usr/bin/ruby
$ ln -s /usr/local/bin/gem /usr/bin/gem
$ ln -s /usr/local/bin/bundle /usr/bin/bundle
// 設置ruby gem源為淘寶
$ gem source -r https://rubygems.org/
$ gem source -a http://ruby.taobao.org/
$ gem install bundler --no-ri --no-rdoc
安裝MySQL及初始化GitLab庫
$ yum install mysql mysql-devel mysql-server -y
$ /etc/init.d/mysqld start
$ chkconfig mysqld on
// 登錄mysql創建gitab的帳號和資料庫
mysql> CREATE USER 'gitlab'@'localhost' IDENTIFIED BY 'gitlab';
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_proction` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER ON `gitlabhq_proction`.* TO 'gitlab'@'localhost';
//測試是否可以用git帳號登錄資料庫
sudo -u git -H mysql -u gitlab -p -D gitlabhq_proction
安裝Redis
$ yum -y install redis
$ /etc/init.d/redis start
$ chkconfig redis on
添加git帳號並允許sudo
$ useradd --comment 'GitLab' git
$ echo "git ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
安裝GitLab
$ /home/git
$ sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-ce.git -b 7-8-stable gitlab
$ cd /home/git/gitlab
$ sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
// 編輯git路徑, gitlab的host:port
$ vim config/gitlab.yml
// bin_path: /usr/local/bin/git
// host: localhost
// port: 80
// 給文件夾添加相應的許可權
$ chown -R git log/
$ chown -R git tmp/
$ chmod -R u+rwX log/
$ chmod -R u+rwX tmp/
// 創建必要的文件夾,以及復制配置文件
$ sudo -u git -H mkdir /home/git/gitlab-satellites
$ sudo -u git -H mkdir tmp/pids/
$ sudo -u git -H mkdir tmp/sockets/
$ sudo chmod -R u+rwX tmp/pids/
$ sudo chmod -R u+rwX tmp/sockets/
$ sudo -u git -H mkdir public/uploads
$ sudo chmod -R u+rwX public/uploads
$ sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
$ sudo -u git -H cp config/initializers/rack_attack.rb.example
config/initializers/rack_attack.rb
// 配置資料庫連接信息
$ sudo -u git cp config/database.yml.mysql config/database.yml
$ sudo -u git -H vim config/database.yml
$ vim config/database.yml
// proction:
// username: gitlab
// password: "gitlab"
安裝GitLab-Shell
$ cd /home/git
$ sudo -u git -H git clone https://gitlab.com/gitlab-org/gitlab-shell.git -b v2.6.0
$ cd gitlab-shell/
$ sudo -u git -H cp config.yml.example config.yml
// 編輯配置文件, 設置gitlab_url, redis-cli, log-level...
$ vim config.yml
// gitlab_url: "http://localhost/"
// /usr/bin/redis-cli
// 安裝git-shell
$ sudo -u git -H ./bin/install
安裝需要ruby的gems
$ cd /home/git/gitlab
$ sudo -u git -H bundle install --deployment --without development test postgres aws
初始化資料庫(創建GitLab相關表)
$ sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=proction
安裝啟動文件以及日誌切割文件
cp lib/support/init.d/gitlab /etc/init.d/gitlab
cp lib/support/init.d/gitlab.default.example /etc/default/gitlab
cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
設置git帳號信息
$ sudo -u git -H git config --global user.name "Troy Zhang"
$ sudo -u git -H git config --global user.email "[email protected]"
$ sudo -u git -H git config --global core.autocrlf input
安裝Nginx
$ yum -y install nginx
$ vim /etc/nginx/nginx.conf
user root git;
worker_processes 2;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# GITLAB
# Maintainer: @randx
# App Version: 5.0
upstream gitlab {
server unix:/home/git/gitlab/tmp/sockets/gitlab.socket;
}
server {
listen *:80 default_server; # e.g., listen 192.168.1.1:80; In most cases *:80 is a good idea
server_name YOUR_SERVER_FQDN; # e.g., server_name source.example.com;
server_tokens off; # don't show the version number, a security best practice
root /home/git/gitlab/public;
# Set value of client_max_body_size to at least the value of git.max_size in gitlab.yml
client_max_body_size 5m;
# indivial nginx logs for this gitlab vhost
access_log /var/log/nginx/gitlab_access.log;
error_log /var/log/nginx/gitlab_error.log;
location / {
# serve static files from defined root folder;.
# @gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html @gitlab;
}
# if a file, which is not found in the root folder is requested,
# then the proxy pass the request to the upsteam (gitlab unicorn)
location @gitlab {
proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://gitlab;
}
}
}
更改許可權,啟動nginx
$ nginx -t
$ chown -R git:git /var/lib/nginx/
$ /etc/init.d/nginx start
檢測當前環境
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=proction
拉取gitlab靜態資源文件
$ sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=proction
啟動gitlab
$ /etc/init.d/gitlab start
檢測各個組件是否正常工作
$ sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=proction
『叄』 樹莓派(raspbian)安裝php和sqlite出現疑似依賴包方面問題
Raspbian是專門用於ARM卡片式計算機RaspberryPi的操作系統,Raspbian系統是Debian7.0/wheezy的定製版本,得益於Debian從7.0/wheezy開始引入的"帶硬體浮點加速的ARM架構(armhf)",Debian7.0在樹莓派上的運行性能有了很大提升,Raspbian默認使用LXDE桌面,內置C和Python
『肆』 如何在nginx上防止惡意的ddos抓取
自己寫的網站,不知道為啥總是有很多惡意訪問的ip,根本不是正常的用戶訪問之前也有想過是否可能是運營商的代理伺服器出口ip,但是看起來好像也不像,先不管,ban掉再說
那如何來ban呢,規則比較簡單,就是超過一個閾值之後,就用下面的iptables命令
iptables -I INPUT -s -j DROP
service iptables save
service iptables restart
那如何統計nginx的訪問ip呢,目前來說比較簡單就是讀取nginx的access.log文件,然後分析一下,如果一天超過閾值,我們就執行上面的命令,ban掉ip。
有個問題是我們需要的是一直監視access.log 文件,而不是讀一次,當然可以在每天快結束的時候事後來封住ip,但是這樣不能給爬蟲囂張的氣焰,所以
我覺得立即封掉是比較OK的。
之前寫過一個類似tail讀取一個文件的功能,就是打開的時候先定位到文件大小的位置,然後就開始循環讀行來處理,這次的處理有點不一樣,我是直接使用tail 把那個文件的流定位到我的程序的 sys.stdin ,這樣就簡單的可以讀到所有的流了
tail /var/log/nginx/access.log | python .py
但是nginx的access.log每天都會做一次logrotate,它是怎麼做的呢,nginx官網的推薦方式
mv access.log access.log.0
kill -USR1 `cat master.nginx.pid`
sleep 1
gzip access.log.0 # do something with access.log.0
我的vps上面使用的是logrotated來處理的,可以在/etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
這里表示的create表示文件會重新創建。其實這樣老的文件就沒有新數據了,但是因為使用的是tail,也沒有eof, 這樣直接讀的時候會發現 file.readline() 的函數會卡住,導致程序假死,這樣程序就不能主動退出了,後面選擇了用select來處理,加了一個10秒的超時,從目前的流量來看,基本上每秒都好多的請求,10秒都沒有數據就是出問題了。
while sys.stdin in select.select([sys.stdin], [], [], 10)[0]:
然後如果沒有select上的話,說明基本上是nginx log文件rotate掉了,所以遇到這樣的情況,我就直接跳出程序,然後把程序終結掉。終結掉怎麼辦呢,本來想在程序裡面重新啟動一下程序,但是感覺可能不妥,所以使用crontab來處理了,每隔2分鍾檢查一下,為了防止多次啟動就用了flock來防止程序重新啟動
*/2 * * * * flock -xn /dev/shm/blocker.lock -c "sh /srv/www/beauty/daemon/nginx_ip_blocker.sh"
這里附上代碼
#coding=utf-8
import sys
import re
import os
import urllib
import urllib2
import datetime
import time, os
import logging
import json
import select
logging.basicConfig(level=logging.DEBUG, datefmt='%Y%m%d %H:%M:%S', format='[%(asctime)s] %(message)s')
"""
iptables -I INPUT -s -j DROP
service iptables save
service iptables restart
本腳本主要是用來把惡意訪問nginx的ip ban掉的腳本
"""
MAX_IP=7000
def get_date():
return time.strftime("%Y%m%d")
def ban_one_ip(ip):
os.system("iptables -I INPUT -s %s -j DROP ; service iptables save ; service iptables restart"%ip)
def find_ip_and_ban():
for ip in ip_map:
if ip_map.get(ip)>MAX_IP:
print "ban ip, count %s:%s"%(ip_map.get(ip),ip)
ip_map[ip] = 0
ban_one_ip(ip)
today = None
ip_map = {}
def process_log(lines):
global ip_map;
global today;
now = get_date()
if now != today:
today = now
ip_map = {}
for line in lines:
ip = line.split(" ")[0]
count = ip_map.get(ip,0)
count += 1
ip_map[ip]=count;
find_ip_and_ban()
COUNT=50
def main():
brk=False
while True:
tmp = 0;
lines =[]
brk=False
while sys.stdin in select.select([sys.stdin], [], [], 10)[0]:
line = sys.stdin.readline()
if not line:
brk=True
print 'read to eof'
break
lines.append(line)
tmp+=1
if tmp>COUNT:
break
else:
brk=True
print 'read time out'
break
process_log(lines)
print "read lines:%s"%len(lines)
if brk:
break
if brk:
print "break out"
if __name__ == "__main__":
main()
『伍』 ubuntu logrotate 定時執行時間在哪
先來說下場景,需要寫一個爬蟲,定時從某個介面地址獲取數據,然後存入本地資料庫。
因此就需要一個可定時執行的東西,之前在the5fire的知識體系中除了可以在資料庫中定時執行某個存儲過程或者sql語句,如何在系統中定時執行某個任務還沒有概念。
於是,請教了同事。知道在ubuntu中,准確的說是在linux中,有這么東西——crontab,專門用來定製執行任務。簡單解說一番,就會用了。
雖然這個東西比較簡單,但為了保證知識體系的完整,還是要查查crontab是個什麼。
crontab單詞的意思是:定時任務。
crontab命令常見於Unix和類Unix的操作系統之中,用於設置周期性被執行的指令。該命令從標准輸入設備讀取指令,並將其存放於「crontab」文件中,以供之後讀取和執行。該詞來源於希臘語 chronos(χρόνος),原意是時間。
通常,crontab儲存的指令被守護進程激活, crond常常在後台運行,每一分鍾檢查是否有預定的作業需要執行。這類作業一般稱為cron jobs。
有了上面的概念,再來看crontab的使用就會清晰些。
既然是系統每分鍾都要檢查一下,那麼必然要有一個檢查的依據,如配置文件或者什麼的。
還是來看看網路:
crontab文件包含送交cron守護進程的一系列作業和指令。每個用戶可以擁有自己的crontab文件;同時,操作系統保存一個針對整個系統的crontab文件,該文件通常存放於/etc或者/etc之下的子目錄中,而這個文件只能由系統管理員來修改。
crontab文件的每一行均遵守特定的格式,由空格或tab分隔為數個領域,每個領域可以放置單一或多個數值。
好了,開始使用了。估計有些人從定義就知道他要怎麼用了。不過我還是想記錄下。
使用步驟:
1、終端運行crontab -e [解釋:編輯配置文件]
2、選擇你要用的編輯器,一般人會選擇vi。
3、此時配置文件已打開,只需要按照他的格式寫配置即可。
好吧,簡單到我都覺得。。
舉個例子:
在我的home目錄下有一個python腳本,helloworld.py
#coding:utf-8
print 'hello world by crontab!'
我想要這個腳本在每天的早上7點30執行。
因此這個 任務的crontab配置文件就是:
# m h dom mon dow command
30 7 * * * python /home/the5fire/testcrontab.py >>/home/the5fire/testcrontab.log 2>&1
簡單解釋下,這個配置的意思就是在每天的7:30用python運行我的家目錄下的testcrontab.py文件,並將輸出內容輸出到testcrontab.log中,後面那個2>&1的意思是把錯誤的輸出也輸出到標准輸出(2表示錯誤,2>表示錯誤輸出,&表示等同於,1表示正確),因此如果運行出錯也會把錯誤輸出到之前定義的log中。
另外關於合適執行命令還有些要說。
上面只是定時幾點執行,那麼我怎麼設置它按照某一頻率執行。比如每分鍾執行依次。
對應的配置就是
# m h dom mon dow command
*/1 * * * * python /home/the5fire/testcrontab.py >>/home/the5fire/testcrontab.log 2>&1
再來一個場景,我想在每天的早上六點到八點之間,每隔3分鍾執行一次的配置怎麼寫:
# m h dom mon dow command
*/3 6-8 * * * python /home/the5fire/testcrontab.py >>/home/the5fire/testcrontab.log 2>&1
到此應該都會使用了吧,五個星號表示不同的執行單位(分、時、日、月、年),而那個反斜線表示頻率。
『陸』 iptables 是否可以監控性能
用下面的iptables命令
iptables -I INPUT -s <ip-to-ban> -j DROP
service iptables save
service iptables restart
那如何統計nginx的訪問ip呢,目前來說比較簡單就是讀取nginx的access.log文件,然後分析一下,如果一天超過閾值,我們就執行上面的命令,ban掉ip。
有個問題是我們需要的是一直監視access.log 文件,而不是讀一次,當然可以在每天快結束的時候事後來封住ip,但是這樣不能給爬蟲囂張的氣焰,所以
我覺得立即封掉是比較OK的。
之前寫過一個類似tail讀取一個文件的功能,就是打開的時候先定位到文件大小的位置,然後就開始循環讀行來處理,這次的處理有點不一樣,我是直接使用tail 把那個文件的流定位到我的程序的 sys.stdin ,這樣就簡單的可以讀到所有的流了
tail /var/log/nginx/access.log | python <your_python_script>.py
但是nginx的access.log每天都會做一次logrotate,它是怎麼做的呢,nginx官網的推薦方式
mv access.log access.log.0
kill -USR1 `cat master.nginx.pid`
sleep 1
gzip access.log.0 # do something with access.log.0
我的vps上面使用的是logrotated來處理的,可以在/etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 52
compress
delaycompress
notifempty
create 640 nginx adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
endscript
}
『柒』 什麼是CGI,FastCGI,PHP-CGI,PHP-FPM,Spawn-FCGI
什麼是CGI
CGI全稱是「公共網關介面」(Common Gateway Interface),HTTP伺服器與你的或其它機器上的程序進行「交談」的一種工具,其程序須運行在網路伺服器上。
CGI可以用任何一種語言編寫,只要這種語言具有標准輸入、輸出和環境變數。如php,perl,tcl等
什麼是FastCGI
FastCGI像是一個常駐(long-live)型的CGI,它可以一直執行著,只要激活後,不會每次都要花費時間去fork一次(這是CGI最為人詬病的fork-and-execute 模式)。它還支持分布式的運算, 即 FastCGI 程序可以在網站伺服器以外的主機上執行並且接受來自其它網站伺服器來的請求。
FastCGI是語言無關的、可伸縮架構的CGI開放擴展,其主要行為是將CGI解釋器進程保持在內存中並因此獲得較高的性能。眾所周知,CGI解釋器的反復載入是CGI性能低下的主要原因,如果CGI解釋器保持在內存中並接受FastCGI進程管理器調度,則可以提供良好的性能、伸縮性、Fail- Over特性等等。
FastCGI與CGI特點
1、如CGI,FastCGI也具有語言無關性.
2、如CGI, FastCGI在進程中的應用程序,獨立於核心web伺服器運行,提供了一個比API更安全的環境。(APIs把應用程序的代碼與核心的web伺服器鏈接在一起,這意味著在一個錯誤的API的應用程序可能會損壞其他應用程序或核心伺服器; 惡意的API的應用程序代碼甚至可以竊取另一個應用程序或核心伺服器的密鑰。)
3、FastCGI技術目前支持語言有:C/C++、Java、Perl、Tcl、Python、SmallTalk、Ruby等。相關模塊在Apache, ISS, Lighttpd等流行的伺服器上也是可用的。
4、如CGI,FastCGI的不依賴於任何Web伺服器的內部架構,因此即使伺服器技術的變化, FastCGI依然穩定不變。
FastCGI的工作原理
1、Web Server啟動時載入FastCGI進程管理器(IIS ISAPI或Apache Mole)
2、FastCGI進程管理器自身初始化,啟動多個CGI解釋器進程(可見多個php-cgi)並等待來自Web Server的連接。
3、當客戶端請求到達Web Server時,FastCGI進程管理器選擇並連接到一個CGI解釋器。Web server將CGI環境變數和標准輸入發送到FastCGI子進程php-cgi。
4、FastCGI子進程完成處理後將標准輸出和錯誤信息從同一連接返回Web Server。當FastCGI子進程關閉連接時,請求便告處理完成。FastCGI子進程接著等待並處理來自FastCGI進程管理器(運行在Web Server中)的下一個連接。 在CGI模式中,php-cgi在此便退出了。
在上述情況中,你可以想像CGI通常有多慢。每一個Web請求PHP都必須重新解析php.ini、重新載入全部擴展並重初始化全部數據結構。使用FastCGI,所有這些都只在進程啟動時發生一次。一個額外的好處是,持續資料庫連接(Persistent database connection)可以工作。
FastCGI的不足
因為是多進程,所以比CGI多線程消耗更多的伺服器內存,PHP-CGI解釋器每進程消耗7至25兆內存,將這個數字乘以50或100就是很大的內存數。
Nginx 0.8.46+PHP 5.2.14(FastCGI)伺服器在3萬並發連接下,開啟的10個Nginx進程消耗150M內存(15M*10=150M),開啟的64個php-cgi進程消耗1280M內存(20M*64=1280M),加上系統自身消耗的內存,總共消耗不到2GB內存。如果伺服器內存較小,完全可以只開啟25個php-cgi進程,這樣php-cgi消耗的總內存數才500M。
什麼是PHP-CGI
PHP-CGI是PHP自帶的FastCGI管理器。
啟動PHP-CGI,使用如下命令:
1
php-cgi -b 127.0.0.1:9000
PHP-CGI的不足
1、php-cgi變更php.ini配置後需重啟php-cgi才能讓新的php-ini生效,不可以平滑重啟
2、直接殺死php-cgi進程,php就不能運行了。(PHP-FPM和Spawn-FCGI就沒有這個問題,守護進程會平滑從新生成新的子進程。)
什麼是PHP-FPM
PHP-FPM是一個PHP FastCGI管理器,是只用於PHP的,可以在 http://php-fpm.org/download下載得到.
PHP-FPM其實是PHP源代碼的一個補丁,旨在將FastCGI進程管理整合進PHP包中。必須將它patch到你的PHP源代碼中,在編譯安裝PHP後才可以使用。
現在我們可以在最新的PHP 5.3.2的源碼樹里下載得到直接整合了PHP-FPM的分支,據說下個版本會融合進PHP的主分支去。相對Spawn-FCGI,PHP-FPM在CPU和內存方面的控制都更勝一籌,而且前者很容易崩潰,必須用crontab進行監控,而PHP-FPM則沒有這種煩惱。
PHP5.3.3已經集成php-fpm了,不再是第三方的包了。PHP-FPM提供了更好的PHP進程管理方式,可以有效控制內存和進程、可以平滑重載PHP配置,比spawn-fcgi具有更多有點,所以被PHP官方收錄了。在./configure的時候帶 –enable-fpm參數即可開啟PHP-FPM。
使用PHP-FPM來控制PHP-CGI的FastCGI進程
1
2
3
4
5
6
7
8
/usr/local/php/sbin/php-fpm{start|stop|quit|restart|reload|logrotate}
--start 啟動php的fastcgi進程
--stop 強制終止php的fastcgi進程
--quit 平滑終止php的fastcgi進程
--restart 重啟php的fastcgi進程
--reload 重新平滑載入php的php.ini
--logrotate 重新啟用log文件
什麼是Spawn-FCGI
Spawn-FCGI是一個通用的FastCGI管理伺服器,它是lighttpd中的一部份,很多人都用Lighttpd的Spawn-FCGI進行FastCGI模式下的管理工作,不過有不少缺點。而PHP-FPM的出現多少緩解了一些問題,但PHP-FPM有個缺點就是要重新編譯,這對於一些已經運行的環境可能有不小的風險(refer),在php 5.3.3中可以直接使用PHP-FPM了。
Spawn-FCGI目前已經獨成為一個項目,更加穩定一些,也給很多Web 站點的配置帶來便利。已經有不少站點將它與nginx搭配來解決動態網頁。
最新的lighttpd也沒有包含這一塊了(http://www.lighttpd.net/search?q=Spawn-FCGI),但可以在以前版本中找到它。在lighttpd-1.4.15版本中就包含了(http://www.lighttpd.net/download/lighttpd-1.4.15.tar.gz)
目前Spawn-FCGI的下載地址是http://redmine.lighttpd.net/projects/spawn-fcgi,最新版本是http://www.lighttpd.net/download/spawn-fcgi-1.6.3.tar.gz
註:最新的Spawn-FCGI可以到lighttpd.net網站搜索「Spawn-FCGI」找到它的最新版本發布地址
下面我們就可以使用Spawn-FCGI來控制php-CGI的FastCGI進程了
1
/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -u www-data -g www-data -f /usr/bin/php-CGI
參數含義如下:
-f 指定調用FastCGI的進程的執行程序位置,根據系統上所裝的PHP的情況具體設置
-a 綁定到地址addr
-p 綁定到埠port
-s 綁定到unix socket的路徑path
-C 指定產生的FastCGI的進程數,默認為5(僅用於PHP)
-P 指定產生的進程的PID文件路徑
-u和-g FastCGI使用什麼身份(-u 用戶 -g 用戶組)運行,Ubuntu下可以使用www-data,其他的根據情況配置,如nobody、apache等
PHP-FPM與spawn-CGI對比測試
PHP-FPM的使用非常方便,配置都是在PHP-FPM.ini的文件內,而啟動、重啟都可以從php/sbin/PHP-FPM中進行。更方便的是修改php.ini後可以直接使用PHP-FPM reload進行載入,無需殺掉進程就可以完成php.ini的修改載入
結果顯示使用PHP-FPM可以使php有不小的性能提升。PHP-FPM控制的進程cpu回收的速度比較慢,內存分配的很均勻。
Spawn-FCGI控制的進程CPU下降的很快,而內存分配的比較不均勻。有很多進程似乎��分配到,而另外一些卻佔用很高。可能是由於進程任務分配的不均勻導致的.而這也導致了總體響應速度的下降。而PHP-FPM合理的分配,導致總體響應的提到以及任務的平均。
PHP-FPM與Spawn-FCGI功能比較
http://php-fpm.org/about/
PHP-FPM、Spawn-FCGI都是守護php-cgi的進程管理器。
『捌』 linux centos6 最小化安裝有什麼包
Centos6.464位最小化安裝共計325個包,詳情如下:
安裝setup-2.8.14-20.el6.noarch
安裝filesystem-2.4.30-3.el6.x86_64
安裝xml-common-0.6.3-32.el6.noarch
安裝cjkuni-fonts-common-0.2.20080216.1-35.el6.noarch
安裝iso-codes-3.16-2.el6.noarch
安裝basesystem-10.0-4.el6.noarch
安裝dmz-cursor-themes-0.4-4.el6.noarch
安裝libX11-common-1.5.0-4.el6.noarch
安裝ca-certificates-2010.63-3.el6_1.5.noarch
安裝ncurses-base-5.7-3.20090208.el6.x86_64
安裝tzdata-2012j-1.el6.noarch
安裝glibc-common-2.12-1.107.el6.x86_64
安裝nss-softokn-freebl-3.12.9-11.el6.x86_64
安裝glibc-2.12-1.107.el6.x86_64
安裝ncurses-libs-5.7-3.20090208.el6.x86_64
安裝bash-4.1.2-14.el6.x86_64
安裝libattr-2.4.44-7.el6.x86_64
安裝libcap-2.16-5.5.el6.x86_64
安裝zlib-1.2.3-29.el6.x86_64
安裝dbus-libs-1.2.24-7.el6_3.x86_64
安裝freetype-2.3.11-6.el6_2.9.x86_64
安裝info-4.13a-8.el6.x86_64
安裝audit-libs-2.2-2.el6.x86_64
安裝popt-1.13-7.el6.x86_64
安裝chkconfig-1.3.49.3-2.el6.x86_64
安裝libxml2-2.7.6-8.el6_3.4.x86_64
安裝libcom_err-1.41.12-14.el6.x86_64
安裝libacl-2.2.49-6.el6.x86_64
安裝db4-4.7.25-17.el6.x86_64
安裝bzip2-libs-1.0.5-7.el6_0.x86_64
安裝nspr-4.9.2-1.el6.x86_64
安裝libstdc++-4.4.7-3.el6.x86_64
安裝libICE-1.0.6-1.el6.x86_64
安裝nss-util-3.14.0.0-2.el6.x86_64
安裝libsepol-2.0.41-4.el6.x86_64
安裝libselinux-2.0.94-5.3.el6.x86_64
安裝glib2-2.22.5-7.el6.x86_64
安裝gamin-0.1.10-9.el6.x86_64
安裝atk-1.28.0-2.el6.x86_64
安裝sed-4.2.1-10.el6.x86_64
安裝shadow-utils-4.1.4.2-13.el6.x86_64
安裝expat-2.0.1-11.el6_2.x86_64
安裝dbus-glib-0.86-5.el6.x86_64
安裝libogg-1.1.4-2.1.el6.x86_64
安裝gawk-3.1.7-10.el6.x86_64
安裝readline-6.0-4.el6.x86_64
安裝sqlite-3.6.20-1.el6.x86_64
安裝file-libs-5.04-15.el6.x86_64
安裝libpng-1.2.49-1.el6_2.x86_64
安裝libuuid-2.17.2-12.9.el6.x86_64
安裝libSM-1.2.1-2.el6.x86_64
安裝libgpg-error-1.7-4.el6.x86_64
安裝libjpeg-turbo-1.2.1-1.el6.x86_64
安裝libart_lgpl-2.3.20-5.1.el6.x86_64
安裝pkgconfig-0.23-9.1.el6.x86_64
安裝libgcrypt-1.4.5-9.el6_2.2.x86_64
安裝libblkid-2.17.2-12.9.el6.x86_64
安裝lua-5.1.4-4.1.el6.x86_64
安裝libvorbis-1.2.3-4.el6_2.1.x86_64
安裝pcre-7.8-6.el6.x86_64
安裝grep-2.6.3-3.el6.x86_64
安裝cyrus-sasl-lib-2.1.23-13.el6_3.1.x86_64
安裝libidn-1.18-2.el6.x86_64
安裝which-2.19-6.el6.x86_64
安裝avahi-libs-0.6.25-12.el6.x86_64
安裝tcp_wrappers-libs-7.6-57.el6.x86_64
安裝xz-libs-4.999.9-0.3.beta.20091007git.el6.x86_64
安裝elfutils-libelf-0.152-1.el6.x86_64
安裝libXau-1.0.6-4.el6.x86_64
安裝libxcb-1.8.1-1.el6.x86_64
安裝libX11-1.5.0-4.el6.x86_64
安裝libXext-1.3.1-2.el6.x86_64
安裝libXrender-0.9.7-2.el6.x86_64
安裝libXi-1.6.1-3.el6.x86_64
安裝libXfixes-5.0-3.el6.x86_64
安裝libXcursor-1.1.13-2.el6.x86_64
安裝libXrandr-1.4.0-1.el6.x86_64
安裝libXinerama-1.1.2-2.el6.x86_64
安裝libXt-1.1.3-1.el6.x86_64
安裝libXmu-1.1.1-2.el6.x86_64
安裝libtiff-3.9.4-9.el6_3.x86_64
安裝libutempter-1.1.5-4.1.el6.x86_64
安裝psmisc-22.6-15.el6_0.1.x86_64
安裝fintils-4.4.2-6.el6.x86_64
安裝libselinux-utils-2.0.94-5.3.el6.x86_64
安裝checkpolicy-2.0.22-1.el6.x86_64
安裝bzip2-1.0.5-7.el6_0.x86_64
安裝cpio-2.10-11.el6_3.x86_64
安裝libcap-ng-0.6.4-3.el6_0.1.x86_64
安裝dbus-1.2.24-7.el6_3.x86_64
安裝ibus-libs-1.3.4-6.el6.x86_64
安裝sysvinit-tools-2.87-4.dsf.el6.x86_64
安裝pth-2.0.7-9.3.el6.x86_64
安裝libtasn1-2.3-3.el6_2.1.x86_64
安裝libtdb-1.2.10-1.el6.x86_64
安裝libtool-ltdl-2.2.6-15.5.el6.x86_64
安裝gnutls-2.8.5-10.el6.x86_64
安裝dbus-x11-1.2.24-7.el6_3.x86_64
安裝hal-libs-0.5.14-11.el6.x86_64
安裝xorg-x11-xauth-1.0.2-7.1.el6.x86_64
安裝libXdamage-1.1.3-4.el6.x86_64
安裝libXtst-1.2.1-2.el6.x86_64
安裝libXres-1.0.6-2.el6.x86_64
安裝libXxf86vm-1.1.2-2.el6.x86_64
安裝libXxf86misc-1.0.3-4.el6.x86_64
安裝libXcomposite-0.4.3-4.el6.x86_64
安裝xcb-util-0.3.6-1.el6.x86_64
安裝startup-notification-0.10-2.1.el6.x86_64
安裝avahi-glib-0.6.25-12.el6.x86_64
安裝gstreamer-tools-0.10.29-1.el6.x86_64
安裝gstreamer-0.10.29-1.el6.x86_64
安裝grubby-7.0.15-3.el6.x86_64
安裝shared-mime-info-0.70-4.el6.x86_64
安裝jasper-libs-1.900.1-15.el6_1.1.x86_64
安裝file-5.04-15.el6.x86_64
安裝nss-softokn-3.12.9-11.el6.x86_64
安裝flac-1.2.1-6.1.el6.x86_64
安裝eggdbus-0.6-3.el6.x86_64
安裝MAKEDEV-3.24-6.el6.x86_64
安裝desktop-file-utils-0.15-9.el6.x86_64
安裝libcroco-0.6.2-5.el6.x86_64
安裝libIDL-0.8.13-2.1.el6.x86_64
安裝ORBit2-2.14.17-3.2.el6_3.x86_64
安裝libbonobo-2.24.2-5.el6.x86_64
安裝vim-minimal-7.2.411-1.8.el6.x86_64
安裝procps-3.2.8-25.el6.x86_64
安裝net-tools-1.60-110.el6_2.x86_64
安裝tar-1.23-11.el6.x86_64
安裝gmp-4.3.1-7.el6_2.2.x86_64
安裝libusb-0.1.12-23.el6.x86_64
安裝db4-utils-4.7.25-17.el6.x86_64
安裝libss-1.41.12-14.el6.x86_64
安裝e2fsprogs-libs-1.41.12-14.el6.x86_64
安裝pinentry-0.7.6-6.el6.x86_64
安裝diffutils-2.8.1-28.el6.x86_64
安裝binutils-2.20.51.0.2-5.36.el6.x86_64
安裝m4-1.4.13-5.el6.x86_64
安裝libnih-1.0.1-7.el6.x86_64
安裝upstart-0.6.5-12.el6.x86_64
安裝dash-0.5.5.1-4.el6.x86_64
安裝ncurses-5.7-3.20090208.el6.x86_64
安裝groff-1.18.1.4-21.el6.x86_64
安裝less-436-10.el6.x86_64
安裝coreutils-libs-8.4-19.el6.x86_64
安裝gzip-1.3.12-18.el6.x86_64
安裝cracklib-2.8.16-4.el6.x86_64
安裝cracklib-dicts-2.8.16-4.el6.x86_64
安裝coreutils-8.4-19.el6.x86_64
安裝pam-1.1.1-13.el6.x86_64
安裝fontconfig-2.8.0-3.el6.x86_64
安裝mole-init-tools-3.9-21.el6.x86_64
安裝hwdata-0.233-7.9.el6.noarch
安裝ConsoleKit-libs-0.4.1-3.el6.x86_64
安裝polkit-0.96-2.el6_0.1.x86_64
安裝ConsoleKit-0.4.1-3.el6.x86_64
安裝redhat-logos-60.0.14-12.el6.centos.noarch
安裝plymouth-scripts-0.8.3-27.el6.centos.x86_64
安裝hicolor-icon-theme-0.11-1.1.el6.noarch
安裝alsa-lib-1.0.22-3.el6.x86_64
安裝libsndfile-1.0.20-5.el6.x86_64
安裝ConsoleKit-x11-0.4.1-3.el6.x86_64
安裝libpciaccess-0.13.1-2.el6.x86_64
安裝libXft-2.3.1-2.el6.x86_64
安裝logrotate-3.7.8-16.el6.x86_64
安裝nss-3.14.0.0-12.el6.x86_64
安裝nss-sysinit-3.14.0.0-12.el6.x86_64
安裝nss-tools-3.14.0.0-12.el6.x86_64
安裝openldap-2.4.23-31.el6.x86_64
安裝libuser-0.56.13-5.el6.x86_64
安裝sgml-common-0.6.3-32.el6.noarch
安裝GConf2-2.28.0-6.el6.x86_64
安裝libgsf-1.14.15-5.el6.x86_64
安裝sound-theme-freedesktop-0.7-3.el6.noarch
安裝pciutils-libs-3.1.10-2.el6.x86_64
安裝ethtool-3.5-1.el6.x86_64
安裝mingetty-1.08-5.el6.x86_64
安裝keyutils-libs-1.4-4.el6.x86_64
安裝krb5-libs-1.10.3-10.el6.x86_64
安裝openssl-1.0.0-27.el6.x86_64
安裝gnome-vfs2-2.24.2-6.el6.x86_64
安裝libssh2-1.4.2-1.el6.x86_64
安裝libcurl-7.19.7-35.el6.x86_64
安裝curl-7.19.7-35.el6.x86_64
安裝rpm-libs-4.8.0-32.el6.x86_64
安裝rpm-4.8.0-32.el6.x86_64
安裝gnupg2-2.0.14-4.el6.x86_64
安裝gpgme-1.1.8-3.el6.x86_64
安裝mysql-libs-5.1.66-2.el6_3.x86_64
安裝fipscheck-lib-1.2.0-7.el6.x86_64
安裝fipscheck-1.2.0-7.el6.x86_64
安裝cups-libs-1.4.2-48.el6_3.3.x86_64
安裝ustr-1.0.4-9.1.el6.x86_64
安裝libsemanage-2.0.43-4.2.el6.x86_64
安裝gdbm-1.8.0-36.el6.x86_64
安裝libffi-3.0.5-3.2.el6.x86_64
安裝python-libs-2.6.6-36.el6.x86_64
『玖』 如何在ubuntu server 14.04下安裝gitlab中文版
安裝步驟有:
1. 依賴包
2. ruby
3. 用戶創建
4. 資料庫初始化
5. redis
6. gitlab源碼
7. apache
本文可能會用到VPN連接,在命令行下創建VPN連接的方法如下:
sudo apt-get install pptp-linux
sudo pptpsetup --create VPN名稱 --server 伺服器地址 --username 用戶名 --password 密碼 [--encrypt] --start
以上命令會創建一個設備,如果沒有其他的撥號設備,這個設備會是ppp0,用ifconfig可以看到,「--encrypt」選項可選,因為有的伺服器不支持,會提示MPPE required but peer negotiation failed,創建時帶上「--start」選項可以看到連接的情況。
pptpsetup創建的VPN連接重啟後會失效,如果想在不重啟的情況下刪除VPN連接:
sudo pptpsetup --delete VPN名稱
創建VPN連接後還需要使能,並且添加到路由列表:
sudo pon VPN名稱
sudo route add default dev ppp0
禁用VPN連接的方法是:
sudo poff VPN名稱
下面開始正文:
1. 依賴包
我裝的是ubuntu server 14.04,安裝的時候可以選是否安裝LAMP(Linux+Apache+MySQL+PHP),如果沒裝,可以用下面的命令裝:
sudo tasksel install lamp-server
安裝的時候會提示輸入MySQL的root密碼,下面要用。
接下來是依賴關系:
sudo apt-get install flex bison ruby build-essential zlib1g-dev libyaml-dev libssl-dev libgdbm-dev libreadline-dev libncurses5-dev libffi-dev curl openssh-server redis-server checkinstall libxml2-dev libxslt-dev libcurl4-openssl-dev libicu-dev logrotate python-docutils pkg-config cmake libkrb5-dev nodejs git-core
注意:這裡面裝了ruby,應該是1.9.1版本的,gitlab需要2.0以上的ruby,但是安裝2.0以上的ruby需要低版本的ruby,所以我們先裝上,一會兒卸掉。另外,ubuntu 14.04的軟體庫里有ruby 2.0,但是實測不能用,所以還是從源碼裝ruby吧。
2. ruby
下載,編譯,安裝(安裝前刪掉ruby 1.9.1):
curl -L --progress http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.5.tar.gz | tar xz
cd ruby-2.1.5
./configure --prefix=/usr --disable-install-rdoc
make
sudo apt-get autoremove ruby
sudo make install
然後安裝bundler:
sudo gem install bundler --no-ri --no-rdoc
3. 用戶創建
為gitlab創建一個git用戶:
sudo adser --disabled-login --gecos 'GitLab' git
4. 資料庫初始化
官方指南用的是PostgreSQL,不過官方也有MySQL的說明:
http://doc.gitlab.com/ce/install/database_mysql.html
sudo apt-get install -y mysql-server mysql-client libmysqlclient-dev
mysql -u root -p
輸入MySQL的root密碼登陸,然後:
mysql> CREATE USER 'git'@'localhost' IDENTIFIED BY '$password';
記得把上面這句的"$password"換成實際的密碼,然後:
mysql> SET storage_engine=INNODB;
mysql> CREATE DATABASE IF NOT EXISTS `gitlabhq_proction` DEFAULT CHARACTER SET `utf8` COLLATE `utf8_unicode_ci`;
mysql> GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, LOCK TABLES ON `gitlabhq_proction`.* TO 'git'@'localhost';
mysql> \q
5. Redis
Redis貌似是用來存key的資料庫吧,不知道,反正按步驟安裝:
sudo apt-get install redis-server
sudo cp /etc/redis/redis.conf /etc/redis/redis.conf.orig
sed 's/^port .*/port 0/' /etc/redis/redis.conf.orig | sudo tee /etc/redis/redis.conf
echo 'unixsocket /var/run/redis/redis.sock' | sudo tee -a /etc/redis/redis.conf
echo 'unixsocketperm 770' | sudo tee -a /etc/redis/redis.conf
sudo service redis-server restart
sudo usermod -aG redis git
6. gitlab源碼
gitlab源碼默認是安裝在/home/git/gitlab,如果要更改,請參照官方手冊。獲取中文版源碼的過程如下:
cd /home/git
sudo -u git -H git clone https://gitlab.com/larryli/gitlab.git
不知道是因為GFW還是什麼,git clone這個版本庫可慢可慢了,我用了VPN,速度會快點。
git clone完以後可以checkout你想要的版本,在本文編寫的時間(2015年4月22日),可以:
sudo -u git -H git checkout 7-7-zh
然後配置源碼
cd /home/git/gitlab
sudo -u git -H cp config/gitlab.yml.example config/gitlab.yml
sudo chown -R git log/
sudo chown -R git tmp/
sudo chmod -R u+rwX,go-w log/
sudo chmod -R u+rwX tmp/
sudo -u git -H mkdir /home/git/gitlab-satellites
sudo chmod u+rwx,g=rx,o-rwx /home/git/gitlab-satellites
sudo chmod -R u+rwX tmp/pids/
sudo chmod -R u+rwX tmp/sockets/
sudo chmod -R u+rwX public/uploads
sudo -u git -H cp config/unicorn.rb.example config/unicorn.rb
sudo -u git -H cp config/initializers/rack_attack.rb.example config/initializers/rack_attack.rb
sudo -u git -H git config --global user.name "GitLab"
sudo -u git -H git config --global user.email "[email protected]"
sudo -u git -H git config --global core.autocrlf input
sudo -u git -H cp config/resque.yml.example config/resque.yml
當然,你可以把[email protected]改成你的email;上面的那些yml文件裡面的配置基本都不用更改;然後,配置資料庫用戶名和密碼:
sudo -u git cp config/database.yml.mysql config/database.yml
sudo -u git -H editor config/database.yml
sudo -u git -H chmod o-rwx config/database.yml
然後,安裝gems:
sudo -u git -H bundle install --deployment --without development test postgres aws
這一步必須使用VPN,因為萬惡的GFW屏蔽了rubygems.org,所以會提示connection reset by peer之類的,這部時間也會比較久,如果你的機器是多核的,也可以加上和make相同-jN參數,N等於核數。
(2015年5月2日追加)
發現淘寶做了個rubygrems.org的國內鏡像,http://ruby.taobao.org/,可以參照上面的內容設置,加快gems安裝速度。
再接下來安裝gitlab shell:
sudo -u git -H bundle exec rake gitlab:shell:install[v2.6.0] REDIS_URL=unix:/var/run/redis/redis.sock RAILS_ENV=proction
sudo -u git -H editor /home/git/gitlab-shell/config.yml
把gitlab的url改成http://localhost/或者你的域名。
在接下來初始化資料庫:
sudo -u git -H bundle exec rake gitlab:setup RAILS_ENV=proction
加入啟動項:
sudo cp lib/support/init.d/gitlab /etc/init.d/gitlab
sudo update-rc.d gitlab defaults 21
初始化日誌:
sudo cp lib/support/logrotate/gitlab /etc/logrotate.d/gitlab
檢查應用狀態:
sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=proction
編譯附件:
sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=proction
啟動gitlab服務:
sudo service gitlab start
7. apache
下載apache的配置文件:https://gitlab.com/gitlab-org/gitlab-recipes/blob/master/web-server/apache/gitlab-apache2.4.conf
將其中的「ProxyPassReverse
http://gitlab.example.com/」改成「ProxyPassReverse
http://localhost/」或者你的域名,將其中的「/var/log/httpd/logs/」改為「/var/log/apache2/」。
用命令來說:
wget https://gitlab.com/gitlab-org/gitlab-recipes/raw/master/web-server/apache/gitlab-apache2.4.conf
mv gitlab-apache2.4.conf 001-gitlab.conf
editor 001-gitlab.conf (進行上述修改)
sudo cp 001-gitlab.conf /etc/apache2/sites-available
cd /etc/apache2/sites-enabled
sudo rm 000-default.conf
sudo ln -s ../sites-available/001-gitlab.conf 001-gitlab.conf
另外,還需要使能代理模塊以實現反向代理功能,不然會提示「ProxyPassReverse」無效之類的。
cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/proxy.conf proxy.conf
sudo ln -s ../mods-available/proxy.load proxy.load
sudo ln -s ../mods-available/proxy_connect.load proxy_connect.load
sudo ln -s ../mods-available/proxy_http.load proxy_http.load
sudo ln -s ../mods-available/rewrite.load rewrite.load
重啟apache:
sudo service apache2 restart
然後,檢查下配置是否正確:
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=proction
如果都是綠色的結果,那就可以訪問了。登陸用戶名:root,初始密碼:5iveL!fe
最後配置一下郵件,修改/home/git/gitlab/config/gitlab.yml,裡面的「email_from:」為一個有效的email地址;修改/home/git/gitlab/config/environments/proction.rb,裡面的 「config.action_mailer.delivery_method=」為「:smtp」;最後:
cd /home/git/gitlab/config/initializers
sudo -u git -H cp smtp_settings.rb.sample smtp_settings.rb
然後把文件的里的內容修改正確了,再重啟一下gitlab服務就行了:
sudo service gitlab restart