新裝服務器設置Tips(不定期更新)

 

Table of Contents

++++++++++ 說明 +++++++++++

++++++++++ 有用的命令 +++++++++++

++++++++++ 必要項目 +++++++++++

sshd:

++++++++++ 可選項目 +++++++++++

nginx:

php:

toolbox:

docker:

sftp:  # 配置自帶的sftp

vsftpd:

frp:

git:


++++++++++ 說明 +++++++++++

適用於 centos7, ubuntu18, 注意安裝工具區別 yum(centos) apt(ubuntu) 

++++++++++ 有用的命令 +++++++++++

  • lsb_release -a,列出系統版本信息
  • tail -f /var/log/auth.log or secure  # 查看登陸日誌
  • service --status-all # 查看running的服務

++++++++++ 必要項目 +++++++++++

系統準備:
  useradd ben
  usermod -aG wheel ben #添加到 sudoer以便可以變身root, 需查看/etc/sudoer是哪個被允許的group: "%wheel  ALL=(ALL)       NOPASSWD: ALL"

sshd:

  #編輯 /etc/sshd_config, 配置無需ppk密匙登陸, (注:方便完後關閉該項,服務器是黑森林世界)
  PasswordAuthentication yes

  # 在本地生成一對公匙與私匙, 部署公匙到遠程服務器,私匙用來登陸
    ssh-keygen -t rsa
  # 法1: ssh-copy-id工具部署
    ssh-copy-id -i path/id_rsa.pub ben@服務器ip 
  # 法2:手動append
    echo id_rsa.pub >> 用戶目錄/.ssh/authorized_keys
  # 檢查用戶目錄:
    chmod 755 /home/ben  #注: home/user目錄必須755,如果其他比如775, 都會報認證失敗
  # 設置只允許key登陸,服務器的世界是黑森林
    PasswordAuthentication no


++++++++++ 可選項目 +++++++++++

nginx:

  apt install nginx
  # 配置vhosts:
  # - 拷貝準備好的 vhost配置文件到 /etc/nginx/sites-availables, 建軟鏈 ln -s /etc/nginx/sites-availables/xxx.conf /etc/nginx/sites-availables
  service nginx restart

php:

  apt install php
  # 修復phpfpm監聽tcp 9000端口: 
    vi /etc/php/7.2/fpm/pool.d/www.conf
      listen=127.0.0.1:9000
    service php7.2-fpm restart

toolbox:

  說明: 設置服務器工具箱訪問,包括phpmyadmin / info / phpredisadmin 等
  - 安裝httpd-tools: yum install httpd-tools -y
  - 建立賬號文件: htpasswd -c /data/auth/htpasswd ben
  - 設置虛擬機: toolbox.xxx.com, /nginx/vhost/toolbox.conf,內容:
 

server {
        listen       80;
        listen       443;
        listen       [::]:80;
        server_name  toolbox.xxx.com;
        
        set $host_path "/data/tools/toolbox";

        access_log   /data/logs/toolbox.log main;
        error_log    /data/logs/toolbox.error.log;
        
        charset utf-8;
        
        root   $host_path;
        index  index.html index.htm index.php;
        
        location / {
            auth_basic  "Authorize Site";
            auth_basic_user_file /data/auth/htpasswd;
            autoindex on;
        }
        
        location ~ \.php$ {
            try_files $uri =404;
            #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
     
        #include agent_deny.def;
  }


  

docker:

  # 使用官方腳本自動安裝
    curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
  # 常用命令
    docker ps  // 列出運行中的容器服務
    docker stop {container id}  // 停止容器服務
    docker image ls  // 列出已安裝的鏡像包
  # 鏡像製作與發佈例子
      前置動作:
        - 申請 dockerhub 賬號      

     例一:
       發佈服務器:
         1 去項目發佈目錄, 整理出Dockerfile
         2 docker build -t bennybi/php-hello .
         3 docker run -d -p 8080:8080 bennybi/php-hello # 創建一個新容器,並後臺運行,映射宿主端口 8080 => 容器端口 8080
         4 docker push bennybi/php-hello
       部署服務器:
         docker pull bennybi/php-hello
         docker run -d -p 8080:8080 bennybi/php-hello

      例二:
       發佈服務器:
         1 去項目發佈目錄, 整理出Dockerfile
         2 docker build -t bennybi/python-hello .
         3 docker run bennybi/python-hello # 直接運行
         4 docker push bennybi/python-hello
       部署服務器:
         docker pull bennybi/python-hello
         docker run bennybi/python-hello # 直接運行

sftp:  # 配置自帶的sftp

  groupadd sftp
  usermod -G sftp user1
  
  #sftp需要
  chown root:sfp /home/user1
  
  # 編輯 /etc/sshd_config, 配置用戶只能在指定目錄尋覓
  Match Group sftp
  X11Forwarding no
  AllowTcpForwarding no
  ChrootDirectory %h
  ForceCommand internal-sftp

vsftpd:

   使用yum安裝 vsftpd: yum install -y vsftpd 或者 apt-get install vsftpd

  #編輯 /etc/vsftpd/vsftpd.conf
    user_config_dir=/etc/vsftpd/vconf #用戶配置
    virtual_use_local_privs=YES
    anonymous_enable=NO #禁用匿名用戶 YES 改爲NO,
    chroot_local_user=YES # 禁止切換根目錄 刪除或#
    allow_writeable_chroot=YES
    file_open_mode=0775
  #創建虛擬賬號
    #創建宿主user
      useradd -g root -M -d /home/vsftpd -s /sbin/nologin ftpuser
      chown -R ftpuser.root /home/vsftpd
    #建立虛擬用戶清單 (第一行賬號,第二行密碼,注意:不能使用root做用戶名,系統保留)
       touch /etc/vsftpd/virtual_users
       #內容如:
         ftp1
         kljoeioouj
         ftp2
         092rjoiu08
    #生成數據文件
      db_load -T -t hash -f /etc/vsftpd/vuser_passwd /etc/vsftpd/virtual_users.db
      chmod 600 /etc/vsftpd/virtual_users.db
    #單獨用戶配置
      mkdir /etc/vsftpd/vconf  # 建立虛擬用戶個人vsftp的配置文件
      cd /etc/vsftpd/vconf     # 進入目錄
      touch ftp1 ftp2
     #內容
       local_root=/data/Projects/EShop/live/prj
       write_enable=YES
       anon_world_readable_only=NO
       anon_upload_enable=YES
       anon_mkdir_write_enable=YES
       anon_other_write_enable=YES
     #服務
       systemctl restart vsftpd.service  # 重啓服務
       systemctl start vsftpd.service    # 啓動服務
       systemctl status vsftpd.service   # 服務狀態查看

frp:

  服務器端:
    #檢查服務器硬件結構類型, 以下包適用於 AMD64/x86-64
      arch 
      wget --no-check-certificate https://github.com/fatedier/frp/releases/download/v0.29.1/frp_0.29.1_linux_amd64.tar.gz
      tar -zxvf frp_0.29.1_linux_amd64.tar.gz
      mv frp_0.29.1_linux_amd64 frp
      cp ./frp/frps /usr/bin
      cp ./frp/frps.ini /etc/frp
    # 自啓動
      cp /usr/local/frp/systemd/frps.service /usr/lib/systemd/system
      systemctl enable frps
       systemctl start frps
      systemctl status frps
  客戶端(nas):
    - 在nas的任務計劃中添加開機自啓腳本 /etc/rc.local/frpc.sh
    - frpc.ini參考配置:     

[common]
      server_addr = nas-001.{domain}
      server_port = 7000
      auth_token = {hashkey}
      pool_count = 1
      log_file = ./log.frpc
      log_level = info
      log_max_days = 3
      
      [ssh]
      type = tcp
      local_ip = 192.168.31.132
      local_port = 22
      remote_port = 6000
      
      [nas]
      type = http
      local_ip = 192.168.31.132
      local_port = 5000
      remote_port = 5000
      subdomain = nas-001
      
      [web]
      type = http
      local_ip = 192.168.31.132
      local_port = 80
      subdomain = nas-001-web
      
      [range:tcp_port]
      type = tcp
      local_ip = 192.168.31.132
      local_port = 80,443,1900,5001-5006,5353,6001-6010,6060-6080,6690,9000,9900,9901,9025-9040,50001
      remote_port = 80,443,1900,5001-5006,5353,6001-6010,6060-6080,6690,9000,9900,9901,9025-9040,50001
      use_encryption = false
      use_compression = false
      
      [range:udp_port]
      type = udp
      local_ip = 192.168.31.132
      local_port = 1900,5001,5353,6001-6006,50001,50002
      #local_port = 1900,5000,5001,5353,6001-6006,50001,50002
      remote_port = 1900,5001,5353,6001-6006,50001,50002
      #remote_port = 1900,5000,5001,5353,6001-6006,50001,50002
      use_encryption = false
      use_compression = false

  # 放行防火牆端口
        firewall-cmd --zone=public --add-port=7000/tcp --permanent
        firewall-cmd --zone=public --add-port=5000/tcp --permanent
        firewall-cmd --zone=public --add-port=7500/tcp --permanent
        firewall-cmd --zone=public --add-port=6000/tcp --permanent
        firewall-cmd --reload

# 多客戶端穿透例子
client 1:

[common]
server_addr = ip
server_port = 7000
token = xx1
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000

client2:

[common]
server_addr = ip
server_port = 7000
token = xx2
[ssh2]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6001

配置不同的remote port就可

       
git:

  服務器端:
    映射域名 git.xxx.com
    安裝git:

      $ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
      $ yum install git

    git用戶:     

$ groupadd git
$ useradd git -g git

    拷貝用戶公匙到 /home/git/.ssh/authorized_keys
    初始化git倉庫 ([email protected]:/data/gitrespo/t1.git):    

$ mkdir /data/gitrepo
$ chown git:git /data/gitrepo
$ cd /data/gitrepo
$ git init --bare t1.git
$ chown -R git:git t1.git

  客戶端 (TortoiseGit):
    - clone [email protected]:/mnt/gitrepo/t1.git
    - Settings->Git->Remote putty key 選擇用戶私匙
    - Settings->Network->SSH Client 選擇TortoiseGit\bin\TortoiseGitPlink.exe, 否則拉取時不會自動load key登陸
      
 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章