Phabricator安裝和配置過程記錄
來源 https://cloud.tencent.com/developer/article/1609447
文檔 https://secure.phabricator.com/book/phabricator/
參考 https://support.websoft9.com/docs/phabricator/zh/stack-installation.html#%E5%87%86%E5%A4%87
參考 https://juejin.cn/post/6844904161985691662
參考 https://segmentfault.com/a/1190000011183530
本文介紹Phabricator的安裝和配置過程。
Phabricator的安裝過程比較繁瑣,為了保證得到可靠的過程,步驟在以下公有雲平台:
- 微軟Azure國內
- 阿里雲國內
- AWS首爾
分別做了測試。
安裝環境為:
- Ubuntu 16.04 Server
- Nginx
- PHP7.1
- MySQL
以下分兩個部分:
- 安裝:Phabricator基本安裝、運行和檢驗
- 配置:發郵件、基於SSH的Git倉庫托管以及配置雜項等
安裝
clone Phabricator項目文件
創建目錄:
sudo mkdir /var/www/pha
clone相關項目:
sudo git clone https://github.com/phacility/libphutil.git sudo git clone https://github.com/phacility/arcanist.git sudo git clone https://github.com/phacility/phabricator.git
修改目錄所有者,用nginx進程的用戶,www-data
sudo chown -R www-data:www-data /var/www/pha
安裝Nginx
使用Nginx官方源。命令如下:
sudo apt-get install software-properties-common sudo add-apt-repository ppa:nginx/stable sudo apt-get update sudo apt install -y nginx
安裝后,Nginx應自動啟動,檢查80端口是否正常:
netstat -na | grep 80
應看到類似這樣的結果:
tcp6 0 0 :::80 :::* LISTEN
如能找到,說明Nginx安裝成功,如無法外網訪問,檢查防火牆設置。
有關防火牆的設置
后續安裝配置,需要允許如下端口的外網訪問:
- 22,將用於git ssh使用
- 80,默認http
- 443,https,phabricator正式環境端口
- 2222,ssh登錄端口
安裝PHP環境
Phabricator不支持PHP7.0
版本,需要安裝7.1
版本:
sudo LC_ALL=en_US.UTF-8 add-apt-repository ppa:ondrej/php sudo apt-get update sudo apt-get -y install php7.1 php7.1-mysql php7.1-fpm php7.1-curl php7.1-xml php7.1-mcrypt php7.1-json php7.1-gd php7.1-mbstring
修改fpm的配置:
sudo vim /etc/php/7.1/fpm/pool.d/www.conf
文件內容,在listen = /run/php/php7.1-fpm.sock
之后加入2行:
…
; Note: This value is mandatory. listen = /run/php/php7.1-fpm.sock listen = 9000 # 增加 listen.allowed_clients = 127.0.0.1 # 增加 ...
重啟PHP:
sudo service php7.1-fpm stop sudo service php7.1-fpm start
測試配置是否生效:
netstat -na | grep 9000
如能顯示,說明fpm正常啟動:
tcp6 0 0 :::9000 :::* LISTEN
配置Nginx
域名比如是:p.mydomain.com
,那么創建配置文件:/etc/nginx/conf.d/p.mydomain.com.conf
,內容如下:
server {
server_name p.mydomain.com; # 配置域名 root /var/www/pha/phabricator/webroot; # 配置根目錄 location / { index index.php; rewrite ^/(.*)$ /index.php?__path__=/$1 last; } location /index.php { fastcgi_pass localhost:9000; fastcgi_index index.php; #required if PHP was built with --enable-force-cgi-redirect fastcgi_param REDIRECT_STATUS 200; #variables to make the $_SERVER populate in PHP fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx/$nginx_version; fastcgi_param REMOTE_ADDR $remote_addr; } }
安裝和配置MySQL
安裝:
sudo apt install mysql-server
提示輸入root用戶密碼,在本例中,密碼是:password
在phabricator目錄下(git clone https://github.com/phacility/phabricator.git 的那個目錄)執行命令,將mysql密碼設置到phabricator:
sudo ./bin/config set mysql.pass 'password'
為phabricator創建mysql相關數據表:
sudo ./bin/storage upgrade
設置和重啟Nginx
設置下phabricator的url:
sudo ./bin/config set phabricator.base-uri 'http://p.mydomain.com'
Nginx重新加載:
sudo service nginx reload
設置管理員和認證方式
這時,瀏覽器應該可以訪問Phabricator了:http://p.mydomain.com
作為第一個訪問用戶,可創建管理員賬號。
注意,管理員不是標准用戶。
如果能創建管理員,說明安裝過程成功。
這時可以添加認證方式(Auth Provider)。phabricator提供了多種認證方式,其中最基本的是用戶名/密碼的provider。
作為管理員,訪問Auth
,選擇Add Provider ,然后添加 Username/Password Provider
即可。
安裝過程到此基本結束,但還不能:
- 發送用戶邀請郵件及其他通知郵件
- 創建git repository
下一部分將解決這些問題。
配置
發送郵件的基本配置
發送郵件的功能是必須配置的,否則無法創建用戶,因為需要通過郵件發送邀請通知。
執行如下命令,設置發送郵件郵箱配置:
sudo ./bin/config set metamta.default-address robot@mydomain.com sudo ./bin/config set metamta.domain mydomain.com sudo ./bin/config set metamta.can-send-as-user false sudo ./bin/config set metamta.mail-adapter PhabricatorMailImplementationPHPMailerAdapter sudo ./bin/config set phpmailer.mailer smtp sudo ./bin/config set phpmailer.smtp-host smtp.exmail.qq.com sudo ./bin/config set phpmailer.smtp-port 465 sudo ./bin/config set phpmailer.smtp-user robot@mydomain.com sudo ./bin/config set phpmailer.smtp-password my_password sudo ./bin/config set phpmailer.smtp-protocol SSL
這里使用的是qq企業郵箱配置的。
設置完畢,檢查是否可以發送郵件:
./bin/mail send-test --to myname@qq.com --subject hello <README.md
如果能收到郵件,說明郵箱配置正確。
配置和自啟動守護進程
phabricator有個任務隊列,並運行一個守護進程,執行隊列中的任務。可在 http://p.mydomain.com/daemon 中看到Active Daemons
中還沒有可用的守護進程。
phabricator守護進程,phd
,主要負責:
- git repository相關的操作
- 發送郵件
- 垃圾回收,如舊的日志和緩存
我們可以直接啟動守護進程:
sudo ./bin/phd start
但是有問題:
- 當前用戶權限過大
- 需要設置為自啟動服務
創建phd用戶
創建phd用戶:
sudo adduser phd --home /home/phd
使phd用戶不可遠程登錄:
sudo usermod -p NP phd
創建和啟動phd自啟動服務
創建systemd service文件/tmp/service.file
:
[Unit] Description=phabricator-phd After=syslog.target network.target mysql.service Before=nginx.service [Service] Type=oneshot User=phd Group=phd Enviroment="PATH=/sbin:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin" ExecStart=/var/www/pha/phabricator/bin/phd start ExecStop=/var/www/pha/phabricator/bin/phd stop RemainAfterExit=yes [Install] WantedBy=multi-user.target
將服務加入到systemd目錄:
sudo cp /tmp/service.file /lib/systemd/system/phabricator-phd.service
使phabricator-phd.service可用:
sudo systemctl enable phabricator-phd.service
啟動phabricator-phd.service服務:
sudo systemctl start phabricator-phd
如沒有報錯,訪問:http://p.mydomain.com/daemon/ 可以看到Active Daemons不為空了。
將phd
用戶設置為phd.user
:
sudo ./bin/config set phd.user phd
注:其他用戶如需操作git命令,需要sudo為phd
用戶,上面的設置就是告訴它們需要sudo的用戶名。
phd用戶將守護進程跑起來后,就可以創建新用戶了。通過管理員賬號,選擇people
,添加standard
用戶。會收到邀請郵件,如果phd和郵箱配置都沒問題的話。
用這個standard用戶登錄,並上傳public key,后面要用到。
www-data用戶的配置
如果將來要用到http git時,需要將www-data
用戶設置為可sudo為phd
用戶。
編輯/etc/sudoers,加入:
www-data ALL=(phd) SETENV: NOPASSWD: /usr/lib/git-core/git-http-backend
實際上,我們沒有用到這個,我們用的是SSH Git。
配置SSH Git托管
准備工作
將當前SSH服務轉移到2222
端口,將來運行的Git SSH服務使用22
端口。這是多次配置后,覺得后續比較方便的做法。否則,Git用戶都要自定義端口,給開發/部署帶來不必要的麻煩。
修改文件:
sudo vim /etc/ssh/sshd_config
將Port
改為2222
后重啟ssh服務:
sudo service ssh restart
用2222端口ssh重新登錄服務器:
ssh -p2222 p.mydomain.com
創建git用戶
創建用戶:
sudo adduser git
禁止登錄:
sudo usermod -p NP git
設置git可以sudo為phd,修改/etc/sudoers,加入:
git ALL=(phd) SETENV: NOPASSWD: /usr/bin/git-upload-pack, /usr/bin/git-receive-pack
創建存放git repository的目錄:
sudo mkdir /var/repo
改變目錄所有者為phd:
sudo chown -R phd /var/repo sudo chgrp -R phd /var/repo
phabricator設置git-core路徑:
sudo ./bin/config set environment.append-paths '["/usr/lib/git-core"]'
phabricator設置git用戶:
sudo ./bin/config set diffusion.ssh-user git
創建git ssh hook配置文件
phabricator項目中提供了模版文件,將這個文件復制到需要的地方:
sudo cp /var/www/pha/phabricator/resources/sshd/phabricator-ssh-hook.sh /usr/lib/phabricator-ssh-hook.sh
修改文件權限:
sudo chmod 755 /usr/lib/phabricator-ssh-hook.sh
修改hook文件:
sudo vim /usr/lib/phabricator-ssh-hook.sh
文件內容:
#!/bin/sh # NOTE: Replace this with the username that you expect users to connect with. VCSUSER="git" # 配置 # NOTE: Replace this with the path to your Phabricator directory. ROOT="/var/www/pha/phabricator" # 配置 if [ "$1" != "$VCSUSER" ]; then exit 1 fi exec "$ROOT/bin/ssh-auth" $@
創建git ssh配置文件
phabricator也提供了模版文件,復制到需要的地方:
sudo cp /var/www/pha/phabricator/resources/sshd/sshd_config.phabricator.example /etc/ssh/sshd_config.phabricator
修改sshd_config.phabricator文件:
sudo vim /etc/ssh/sshd_config.phabricator
文件內容:
# NOTE: You must have OpenSSHD 6.2 or newer; support for AuthorizedKeysCommand # was added in this version. # NOTE: Edit these to the correct values for your setup. AuthorizedKeysCommand /usr/lib/phabricator-ssh-hook.sh # 配置 AuthorizedKeysCommandUser git # 配置 AllowUsers git # 配置 # You may need to tweak these options, but mostly they just turn off everything # dangerous. Port 22 # 配置 Protocol 2 PermitRootLogin no AllowAgentForwarding no AllowTcpForwarding no PrintMotd no PrintLastLog no PasswordAuthentication no AuthorizedKeysFile none PidFile /var/run/sshd-phabricator.pid
啟動git ssh及測試
這個步驟,是為了檢查前面2個文件是否正確。正式啟動git ssh需要后面使用systemd
的自啟動服務方式。
啟動git ssh服務:
sudo /usr/sbin/sshd -f /etc/ssh/sshd_config.phabricator
在客戶端終端命令行下:
echo {} | ssh git@p.mydomain.com conduit conduit.ping
如果出現:
{"result":"hello","error_code":null,"error_info":null}
就說明成功了。
可能出現的錯誤:
- 沒有將客戶端的public key上傳到phabricator,或者不匹配
- 各種服務器端配置問題,包括用戶權限問題
針對服務器端配置問題,可這樣啟動git ssh服務,參照debug信息一般能找到問題:
sudo /usr/sbin/sshd -d -d -d -f /etc/ssh/sshd_config.phabricator
或者可閱讀官方文檔Diffusion User Guide: Repository Hosting的Troubleshooting SSH部分。
這時,需要殺掉當前啟動的phd服務,因為后面要設置自動啟動它。
設置git ssh自啟動服務
復制ssh的服務文件,作為git ssh服務的模版:
sudo cp /lib/systemd/system/ssh.service /lib/systemd/system/phabricator-ssh.service
修改phabricator-ssh.service:
[Unit] Description=OpenBSD Secure Shell server After=network.target auditd.service ConditionPathExists=!/etc/ssh/sshd_not_to_be_run [Service] EnvironmentFile=-/etc/default/ssh ExecStart=/usr/sbin/sshd -D $SSHD_OPTS -f /etc/ssh/sshd_config.phabricator # 修改 ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartPreventExitStatus=255 Type=notify [Install] WantedBy=multi-user.target
即,修改1行(見代碼注釋),另外,刪除最后一行Alias=sshd.service
。
然后:
sudo systemctl enable phabricator-ssh
sudo systemctl start phabricator-ssh
再次在客戶端測試,如果沒有問題,基本上就配置好了。
配置雜項
可以在:http://p.mydomain.com/config/issue/ 查看配置上的問題,並根據建議做相應修改。
以下給出一些常用的配置情況。
語法高亮
安裝pigment:
sudo apt install -y python-pygments
phabricator打開pygments功能:
sudo ./bin/config set pygments.enabled true
最大下載文件限制
編輯php配置文件:
sudo vim /etc/php/7.1/fpm/php.ini
找到這行並改為:
post_max_size = 100M
修改Nginx配置文件:
sudo vim /etc/nginx/nginx.conf
在http塊中加入:
client_max_body_size 100m;
因為默認使用mysql存儲,還需要修改對mysql存儲的限制,默認是1M
,執行命令:
sudo ./bin/config set storage.mysql-engine.max-size 104857600
重啟Nginx。
增加郵件地址時的報錯處理
在添加郵件地址時出現了這樣的報錯:
Unhandled Exception ("AphrontQueryException") #1055: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'phabricator_system.system_actionlog.actorIdentity' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
原因是sql_mode的限制,可連接msyql:
mysql -u root -ppassword
然后:
SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));
這一問題,也可能會出現在有類似數據庫操作的地方。
========= End