全國(guó)服務熱線:0551-64931480

11
20-01

Phabricator安裝和配置過(guò)程記錄

雲庫科技 768082 0

簡介

本文介紹Phabricator的安裝和配置過(guò)程。

Phabricator的安裝過(guò)程比較繁瑣,爲了保證得到可靠的過(guò)程,步驟在以下公有雲平台:

  • 微軟Azure國(guó)内
  • 阿裡(lǐ)雲國(guó)内
  • AWS首爾

分别做了測試。

安裝環境爲:

  • Ubuntu 16.04 Server
  • Nginx
  • PHP7.1
  • MySQL

以下分兩(liǎng)個部分:

  • 安裝:Phabricator基本安裝、運行和檢驗
  • 配置:發(fā)郵件、基于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進(jìn)程的用戶,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

安裝後(hòu),Nginx應自動啓動,檢查80端口是否正常:

netstat -na | grep 80

應看到類似這(zhè)樣(yàng)的結果:

 tcp6       0      0 :::80                   :::*                    LISTEN

如能(néng)找到,說明Nginx安裝成(chéng)功,如無法外網訪問,檢查防火牆設置。


有關防火牆的設置

後(hòu)續安裝配置,需要允許如下端口的外網訪問:

  • 22,將(jiāng)用于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之後(hòu)加入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

如能(néng)顯示,說明fpm正常啓動:

tcp6       0      0 :::9000                 :::*                    LISTEN


配置Nginx

域名比如是:p.mydomain.com,那麼(me)創建配置文件:/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 的那個目錄)執行命令,將(jiāng)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

設置管理員和認證方式

這(zhè)時,浏覽器應該可以訪問Phabricator了:http://p.mydomain.com

作爲第一個訪問用戶,可創建管理員賬号。

注意,管理員不是标準用戶。

如果能(néng)創建管理員,說明安裝過(guò)程成(chéng)功。

這(zhè)時可以添加認證方式(Auth Provider)。phabricator提供了多種(zhǒng)認證方式,其中最基本的是用戶名/密碼的provider。

作爲管理員,訪問Auth,選擇Add Provider ,然後(hòu)添加 Username/Password Provider即可。

安裝過(guò)程到此基本結束,但還(hái)不能(néng):

  • 發(fā)送用戶邀請郵件及其他通知郵件
  • 創建git repository

下一部分將(jiāng)解決這(zhè)些問題。


配置

發(fā)送郵件的基本配置

發(fā)送郵件的功能(néng)是必須配置的,否則無法創建用戶,因爲需要通過(guò)郵件發(fā)送邀請通知。

執行如下命令,設置發(fā)送郵件郵箱配置:

$ vi ./conf/local/local.json

# 修改内容(注意替換配置中的相關字段):
{
  "mysql.pass": "123456",
  "mysql.user": "root",
  "mysql.host": "localhost",
  "mysql.port": "3306",
  "differential.require-test-plan-field": false,
  "cluster.mailers": [
    {
      "key": "my-mailer",
      "type": "smtp",
      "options": {
        "host": "smtp.exmail.qq.com", //要給别人發(fā)郵件的smtp地址
        "port": 465, // SMTP 協議的端口号
        "user": "auto@domain.com", //要給别人發(fā)郵件的郵箱地址
        "password": "123456", //發(fā)郵件時,smtp 的密碼
        "protocol": "ssl"
      }
    }
  ],
  "metamta.default-address": "auto@domain.com"
}

這(zhè)裡(lǐ)使用的是qq企業郵箱配置的。

設置完畢,檢查是否可以發(fā)送郵件:

./bin/mail send-test --to myname@qq.com --subject hello <README.md

如果能(néng)收到郵件,說明郵箱配置正确。


配置和自啓動守護進(jìn)程

phabricator有個任務隊列,并運行一個守護進(jìn)程,執行隊列中的任務。可在 http://p.mydomain.com/daemon 中看到Active Daemons中還(hái)沒(méi)有可用的守護進(jìn)程。

phabricator守護進(jìn)程,phd,主要負責:

  • git repository相關的操作
  • 發(fā)送郵件
  • 垃圾回收,如舊的日志和緩存

我們可以直接啓動守護進(jìn)程:

sudo ./bin/phd start

但是有問題:

  • 當前用戶權限過(guò)大
  • 需要設置爲自啓動服務

創建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

將(jiāng)服務加入到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

如沒(méi)有報錯,訪問:http://p.mydomain.com/daemon/ 可以看到Active Daemons不爲空了。

將(jiāng)phd用戶設置爲phd.user:

sudo ./bin/config set phd.user phd

注:其他用戶如需操作git命令,需要sudo爲phd用戶,上面(miàn)的設置就是告訴它們需要sudo的用戶名。

phd用戶將(jiāng)守護進(jìn)程跑起(qǐ)來後(hòu),就可以創建新用戶了。通過(guò)管理員賬号,選擇people,添加standard用戶。會(huì)收到邀請郵件,如果phd和郵箱配置都(dōu)沒(méi)問題的話。

用這(zhè)個standard用戶登錄,并上傳public key,後(hòu)面(miàn)要用到。

www-data用戶的配置

如果將(jiāng)來要用到http git時,需要將(jiāng)www-data用戶設置爲可sudo爲phd用戶。

編輯/etc/sudoers,加入:

www-data ALL=(phd) SETENV: NOPASSWD: /usr/lib/git-core/git-http-backend

實際上,我們沒(méi)有用到這(zhè)個,我們用的是SSH Git。


配置SSH Git托管

準備工作

將(jiāng)當前SSH服務轉移到2222端口,將(jiāng)來運行的Git SSH服務使用22端口。這(zhè)是多次配置後(hòu),覺得後(hòu)續比較方便的做法。否則,Git用戶都(dōu)要自定義端口,給開(kāi)發(fā)/部署帶來不必要的麻煩。

修改文件:

sudo vim /etc/ssh/sshd_config

將(jiāng)Port改爲2222後(hòu)重啓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項目中提供了模版文件,將(jiāng)這(zhè)個文件複制到需要的地方:

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及測試

這(zhè)個步驟,是爲了檢查前面(miàn)2個文件是否正确。正式啓動git ssh需要後(hòu)面(miàn)使用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}

就說明成(chéng)功了。

可能(néng)出現的錯誤:

  • 沒(méi)有將(jiāng)客戶端的public key上傳到phabricator,或者不匹配
  • 各種(zhǒng)服務器端配置問題,包括用戶權限問題

針對(duì)服務器端配置問題,可這(zhè)樣(yàng)啓動git ssh服務,參照debug信息一般能(néng)找到問題:

sudo /usr/sbin/sshd -d -d -d -f /etc/ssh/sshd_config.phabricator

或者可閱讀官方文檔Diffusion User Guide: Repository Hosting的Troubleshooting SSH部分。

這(zhè)時,需要殺掉當前啓動的phd服務,因爲後(hòu)面(miàn)要設置自動啓動它。


設置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行(見代碼注釋),另外,删除最後(hòu)一行Alias=sshd.service

然後(hòu):

sudo systemctl enable phabricator-ssh
sudo systemctl start phabricator-ssh

再次在客戶端測試,如果沒(méi)有問題,基本上就配置好(hǎo)了。


配置雜項

可以在:http://p.mydomain.com/config/issue/ 查看配置上的問題,并根據建議做相應修改。

以下給出一些常用的配置情況。

語法高亮

安裝pigment:

sudo apt install  -y python-pygments

phabricator打開(kāi)pygments功能(néng):

sudo ./bin/config set pygments.enabled true


最大下載文件限制

編輯php配置文件:

sudo vim /etc/php/7.1/fpm/php.ini

找到這(zhè)行并改爲:

post_max_size = 100M

修改Nginx配置文件:

sudo vim /etc/nginx/nginx.conf

在http塊中加入:

client_max_body_size 100m;

因爲默認使用mysql存儲,還(hái)需要修改對(duì)mysql存儲的限制,默認是1M,執行命令:

sudo ./bin/config set storage.mysql-engine.max-size 104857600

重啓Nginx。


增加郵件地址時的報錯處理

在添加郵件地址時出現了這(zhè)樣(yàng)的報錯:

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

然後(hòu):

SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

這(zhè)一問題,也可能(néng)會(huì)出現在有類似數據庫操作的地方。

評論列表(0)
暫無評論

發(fā)表評論 取消回複