2023年2月16日 星期四

在CentOS上安裝PHP7.4

1. 先安裝一下epel-release
yum -y install epel-release

2. 在安裝php會用到的函示庫,執行以下指令
yum install -y \
gcc-c++ autoconf libjpeg libjpeg-devel libpng \
libpng-devel freetype freetype-devel \
libpng libpng-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel glib2 \
glib2-devel bzip2 bzip2-devel ncurses curl \
openssl-devel gdbm-devel db4-devel libXpm-devel \
libX11-devel gd-devel gmp-devel readline-devel \
libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel \
libicu-devel libmcrypt-devel libmemcached-devel \
sqlite-devel oniguruma-devel

3. 下載穩定版的php7,解壓縮後,切到此目錄
cd /usr/src/
wget http://tw2.php.net/distributions/php-7.4.33.tar.gz
tar -xzvf php-7.4.33.tar.gz
cd php-7.4.33

4. 設定php編譯參數,如果有錯誤訊息,就要確認相依套件是否有安裝好
./configure --prefix=/usr/local/php \
--with-mysql-sock --with-mysqli --enable-fpm \
--enable-soap --with-openssl --with-mhash \
--enable-inline-optimization --with-zlib --enable-bcmath \
--with-iconv --with-bz2 --enable-calendar \
--with-curl --with-cdb --enable-dom \
--enable-exif --enable-fileinfo --enable-filter \
--enable-ftp --with-openssl-dir --with-zlib-dir \
--enable-gd-jis-conv --with-gettext --with-gmp \
--enable-json --enable-mbstring --enable-mbregex \
--disable-debug --enable-pdo --with-pdo-mysql \
--with-readline --enable-session --enable-shmop \
--enable-simplexml --enable-sockets --enable-sysvmsg \
--enable-sysvsem --enable-sysvshm --with-xsl \
--disable-rpath --with-pear --enable-intl \
--enable-mysqlnd-compression-support

5. 進行編譯跟安裝
make
make install

6. 執行以下建立php的環境變數
ln -sf /usr/local/php/bin/php /usr/local/bin/php

7. 執行以下步驟,將預設的配置檔複製到正常路徑
cp php.ini-development /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp /usr/src/php-7.4.33/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

8. 啟動php-fpm
/etc/init.d/php-fpm restart
/etc/init.d/php-fpm start

9. 修改php.ini,把cgi.fix_pathinfo=0 (為了安全性,避免惡意腳本插入攻擊)
vim /usr/local/php/lib/php.ini

10. 打開nginx的設定檔,加入PHP的設定,執行 vim /etc/nginx/conf.d/default.conf 在index部分加入 index.php 加入以下設定
location / {
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
}
location  ~ \.php(/|$) {
    root /usr/share/nginx/html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

11. 在跟目錄就可以新增一個php檔案來測試,編輯vim /usr/share/nginx/html/info.php
<?php
    phpinfo();
?>

12. 重新啟動nginx
systemctl restart nginx

13. 打開網址http://xx.xx.xx.xx/info.php

2022年12月15日 星期四

在CentOS上安裝PHP8

1. 先安裝一下epel-release
yum -y install epel-release

2. 在安裝php會用到的函示庫,執行以下指令
yum install -y \
gcc-c++ autoconf libjpeg libjpeg-devel libpng \
libpng-devel freetype freetype-devel \
libpng libpng-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel glib2 \
glib2-devel bzip2 bzip2-devel ncurses curl \
openssl-devel gdbm-devel db4-devel libXpm-devel \
libX11-devel gd-devel gmp-devel readline-devel \
libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel \
libicu-devel libmcrypt-devel libmemcached-devel \
sqlite-devel oniguruma-devel

3. 下載穩定版的php8,解壓縮後,切到此目錄
cd /usr/src/
wget https://www.php.net/distributions/php-8.1.13.tar.gz
tar -xzvf php-8.1.13.tar.gz
cd php-8.1.13

4. 設定php編譯參數,如果有錯誤訊息,就要確認相依套件是否有安裝好
./configure --prefix=/usr/local/php \
--with-mysql-sock --with-mysqli --enable-fpm \
--enable-soap --with-openssl --with-mhash \
--with-zlib --enable-bcmath --with-iconv \
--with-bz2 --enable-calendar --with-curl \
--with-cdb --enable-dom --enable-exif \
--enable-fileinfo --enable-filter --enable-ftp \
--with-openssl-dir --with-zlib-dir --enable-gd-jis-conv \
--with-gettext --with-gmp --enable-mbstring \
--enable-mbregex --disable-debug --enable-pdo \
--with-pdo-mysql --with-readline --enable-session \
--enable-shmop --enable-simplexml --enable-sockets \
--enable-sysvmsg --enable-sysvsem --enable-sysvshm \
--with-xsl --disable-rpath --with-pear --enable-intl \
--enable-mysqlnd-compression-support

5. 進行編譯跟安裝
make
make install

6. 執行以下建立php的環境變數
ln -sf /usr/local/php/bin/php /usr/local/bin/php

7. 執行以下步驟,將預設的配置檔複製到正常路徑
cp php.ini-development /usr/local/php/lib/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp /usr/src/php-8.1.13/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

8. 啟動php-fpm
/etc/init.d/php-fpm restart
/etc/init.d/php-fpm start

9. 修改php.ini,把cgi.fix_pathinfo=0 (為了安全性,避免惡意腳本插入攻擊)
vim /usr/local/php/lib/php.ini

10. 打開nginx的設定檔,加入PHP的設定,執行 vim /etc/nginx/conf.d/default.conf 在index部分加入 index.php 加入以下設定
location / {
    root /usr/share/nginx/html;
    index index.php index.html index.htm;
}
location  ~ \.php(/|$) {
    fastcgi_index index.php;
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    root /usr/share/nginx/html;
}

11. 在跟目錄就可以新增一個php檔案來測試,編輯vim /usr/share/nginx/html/info.php
<?php
    phpinfo();
?>

12. 重新啟動nginx
systemctl restart nginx

13. 打開網址http://xx.xx.xx.xx/info.php

2022年11月2日 星期三

安裝Nexus Repository

1. 安裝wget & java
yum install wget -y
yum install -y java-1.8.0-openjdk.x86_64

2. 下載nexus並解壓縮
wget -O nexus.tar.gz https://download.sonatype.com/nexus/3/latest-unix.tar.gz
tar -xvf nexus.tar.gz -C /opt
mv /opt/nexus-3* /opt/nexus

3. 建立使用者nexus
sudo adduser nexus
sudo chown -R nexus:nexus /opt/nexus
sudo chown -R nexus:nexus /opt/sonatype-work

4. 修改檔案
vi /opt/nexus/bin/nexus.rc
run_as_user="nexus"

vi /etc/security/limits.conf
nexus - nofile 65536

以下依情況調整
vi /opt/nexus/bin/nexus.vmoptions

5. 設定nexus服務
vi /etc/systemd/system/nexus.service
[Unit]
Description=nexus service
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
User=nexus
Group=nexus
Restart=on-abort

[Install]
WantedBy=multi-user.target

6. 啟動服務
sudo systemctl daemon-reload
sudo systemctl enable nexus
sudo systemctl start nexus

7. 確認服務,稍微要等一下服務啟動
sudo systemctl status nexus
ss -ntlp

8. 連線 http:/nexus_ip:8081/
密碼在 cat /opt/sonatype-work/nexus3/admin.password

2022年10月30日 星期日

安裝sonar-scanner

1. 下載sonar-scanner並且解壓縮
wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.7.0.2747-linux.zip
unzip sonar-scanner-cli-4.7.0.2747-linux.zip -d /opt

2. 將sonar-scanner加入PATH路徑
vi /etc/profile
export SONAR_SCANNER_HOME=/opt/sonar-scanner-4.7.0.2747-linux
export PATH=$PATH:$SONAR_SCANNER_HOME/bin
source /etc/profile

3. 測試
sonar-scanner -v

4. 下載一個spring boot的範例程式碼,並且利用maven進行編譯
git clone https://gitlab.abow.online/abowspy/spring-boot-demo.git
cd spring-boot-demo/
mvn clean package

6. 進行程式碼掃描
sonar-scanner \
-Dsonar.host.url=http://192.168.1.124:9000 \
-Dsonar.projectKey=spring-boot-demo \
-Dsonar.projectName=spring-boot-demo \
-Dsonar.projectVersion=1.0 \
-Dsonar.login=admin \
-Dsonar.password=1q2w3e4r \
-Dsonar.ws.timeout=30 \
-Dsonar.projectDescription="my first project" \
-Dsonar.links.homepage=http://www.google.com \
-Dsonar.sources=src \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.java.binaries=target/classes \
-Dsonar.java.test.binaries=target/test-classes \
-Dsonar.java.surefire.report=target/surefire-reports

7. 最後成功的話,網頁就會看見如下:


2021年6月2日 星期三

freeradius配合AD+google-authenticator進行雙因子驗證

前置作業

0.關閉selinux
setenforce 0
sed -i 's/=enforcing/=disabled/g' /etc/selinux/config

1.先關閉防火牆
systemctl stop firewalld
systemctl disable firewalld

2.修改hostname
hostnamectl set-hostname radius999.fm.local

2.安裝freeradius跟google-authenticator
yum -y install freeradius freeradius-utils
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y google-authenticator

3.安裝AD相關工具
yum install -y sssd realmd samba-common-tools oddjob oddjob-mkhomedir adcli
#---------------- samba-commontools (這個可以不用安裝)

--------------------------------------------------------------------------------------------

4.修改radiusd.conf
vim /etc/raddb/radiusd.conf
修改
user = root
group = root

5.修改/etc/raddb/sites-enabled/default
vim /etc/raddb/sites-enabled/default
找到
# pam
取消註解如下
        pam

6.freeradius啟用pam模組
ln -s /etc/raddb/mods-available/pam /etc/raddb/mods-enabled/pam

7.修改/etc/raddb/users,使用者預設使用PAM驗證
vim /etc/raddb/users
DEFAULT        Auth-Type := PAM

8.修改/etc/raddb/clients.conf,設定要驗證的來源主機
vim /etc/raddb/clients.conf

client fortivpn {
        ipaddr = 192.168.1.0/24
        secret = test123
        require_message_authenticator = no
}

--------------------------------------------------------------------------------------------

9.加入AD
realm join -v ad.local -U bhchen
realm permit --all

10.編輯sssd檔案(讓user登入的時候,可以不用帶入@ad.name 或者 ad/)
vim /etc/sssd/sssd.conf
    use_fully_qualified_names = False
systemctl restart sssd

11.列出目前設定
realm list

12.列出AD帳號的資訊(如果看到user資料 代表成功連上AD)
id bhchen

--------------------------------------------------------------------------------------------

13.修改PAM設定(修改成如下)
vim /etc/pam.d/radiusd
auth requisite pam_google_authenticator.so forward_pass
auth required pam_sss.so use_first_pass
account required pam_nologin.so
account include password-auth
session include password-auth

14.重新啟動freeradius
systemctl enable radiusd
systemctl restart radiusd

15.登入一個AD使用者來進行測試,並且幫使用者套上google驗證
ssh -l bhchen@fm.local localhost
google-authenticator
預設回答都是y

google-authenticator -t -f -d -l bhchen@ad.local -i SHENYU.ME -r 3 -R 30 -W
google-authenticator -t -f -d -r 3 -R 30 -W

----------

PS.如果log沒有顯示完整
可以修改
vim /etc/raddb/radiusd.conf
log {
        auth = no  改成 yes
}


之後用這個來進行驗證的時候,密碼就必須使用 AD密碼+opt密碼









2020年11月6日 星期五

安裝Sonarqube

1. 關閉防火牆
systemctl stop firewalld
systemctl disable firewalld

2. 安裝常用軟體
yum install -y vim wget unzip

3. 關閉selinux
setenforce 0
vim /etc/sysconfig/selinux

4. 修改sysctl.conf
vim /etc/sysctl.conf
vm.max_map_count=262144
fs.file-max=65536

套用設定
sysctl -p

5. 安裝java
yum install -y java-11-openjdk

6. 安裝 PostgreSQL 12
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql12-server
/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl enable postgresql-12
systemctl start postgresql-12

7. 修改 pg_hba.conf,不然會帳號無法驗證
vim /var/lib/pgsql/12/data/pg_hba.conf
# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

8. 重新啟動PostgreSQL
sudo systemctl restart postgresql-12

9. 設定PostgreSQL 修改postgres的預設密碼
sudo passwd postgres

切換到postgres帳號
su - postgres

新增 postgres使用帳號 => sonar
createuser sonar

切換到PostgreSQL shell模式
psql

設定sonar的db密碼 & 建立db給sonar使用者
ALTER USER sonar WITH ENCRYPTED password 'mypassword';
CREATE DATABASE sonar OWNER sonar;

離開PostgreSQL shell模式
\q

離開user
exit

10. 下載sonarqube並且解壓縮
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.7.0.61563.zip
unzip sonarqube-9.7.0.61563.zip -d /opt
mv /opt/sonarqube-9.7.0.61563 /opt/sonarqube

11. 修改設定檔sonar.properties
vim /opt/sonarqube/conf/sonar.properties

sonar.jdbc.username=sonar
sonar.jdbc.password=mypassword
sonar.path.data=/var/sonarqube/data
sonar.path.temp=/var/sonarqube/temp
sonar.jdbc.url=jdbc:postgresql://localhost/sonar

##以下部分視情況修改
sonar.web.host=127.0.0.1
sonar.web.port=9000
sonar.web.javaOpts=-server -Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError
sonar.search.javaOpts=-server -Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError

12. 建立sonar的使用者帳號
useradd sonar
passwd sonar

13. 建立相關資料夾並且修改權限
mkdir -p /var/sonarqube/data
mkdir -p /var/sonarqube/temp
chown -R sonar:sonar /var/sonarqube
chown -R sonar:sonar /opt/sonarqube

##以下部分視情況修改
vim /etc/security/limits.conf
sonar   -   nofile   131072
sonar   -   nproc    8192

14. 設定Sonarqube service
vim /etc/systemd/system/sonarqube.service
[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=forking
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
LimitNOFILE=131072
LimitNPROC=8192
User=sonar
Group=sonar
Restart=on-failure

[Install]
WantedBy=multi-user.target

15. 啟動服務
systemctl daemon-reload
systemctl enable sonarqube
systemctl start sonarqube
systemctl status sonarqube

# 查修用的指令
tail -f /opt/sonarqube/logs/sonar.log
tail -f /opt/sonarqube/logs/web.log
tail -f /opt/sonarqube/logs/es.log
tail -f /opt/sonarqube/logs/ce.log
ss -ntlp

# 額外設定 nginx reverse proxy (此步驟可以跳過)
sudo yum -y install epel-release
yum install -y nginx
systemctl start nginx
systemctl enable nginx

修改nginx設定檔,新增以下設定
location / {
    proxy_pass "http://127.0.0.1:9000";
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection 'upgrade';
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
}

確認設定檔是否正確
nginx -t

重新啟動
systemctl restart nginx

登入帳密為 admin admin

2020年9月8日 星期二

常用的awk語法



awk '{print $0}' file

~匹配正則 !~不匹配正則

|| && ! 或且非

awk '{if($2!~/Rudy/) print $0}' content

awk '{if($1=="001" && $2~/^Ru/) print $0}' content