服務端常見服務安裝及配置

凍城發表於2020-10-09

服務端常用服務安裝及配置(持續更新)

本文主要記錄服務端常用服務的安裝及基礎配置,基於的作業系統為centos7,文章持續更新,穩重如有不當,歡迎批評指正

服務安裝及配置

JDK

  • 官網下載JDK安裝包,或使用已有的jdk1.8安裝包
  • 解壓檔案至/usr/local/java
tar -zxf jdk-8u251-linux-x64.tar.gz -C /usr/local/java
  • 修改配置檔案
vim /etc/profile 

#在檔案中追加如下內容
JAVA_HOME=/usr/local/java/jdk1.8.0_231
PATH=$JAVA_HOME/bin:$PATH 
CLASSPATH=$JAVA_HOME/jre/lib/ext:$JAVA_HOME/lib/tools.jar 
export PATH JAVA_HOME CLASSPATH

#使檔案生效
source /etc/profile
  • 驗證jdk是否安裝成功
java -vserion

Mysql服務(版本:5.7)

  • 下載安裝包
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
  • 安裝
sudo rpm -ivh mysql57-community-release-el7-8.noarch.rpm
sudo yum install mysql-server
  • 設定密碼
#檢視臨時密碼
sudo grep 'temporary password' /var/log/mysqld.log
#如果/var/log/mysqld.log為空,執行如下步驟:
#1.刪除原來安裝過的mysql殘留的資料
rm -rf /var/lib/mysql

#2.重啟mysqld服務
systemctl restart mysqld

#3.再去找臨時密碼
sudo grep 'temporary password' /var/log/mysqld.log

#4.配置mysql安裝項
sudo mysql_secure_installation

#輸入剛才的臨時密碼,mysql會提示你輸入新的密碼和一些其他設定,一般情況下我們都會輸入y
#注意:密碼必須至少包含8個字元並且至少包含一個大寫字母,一個小寫字母,一個數字和一個特殊字元

  • 遠端訪問,如果遠端連線不成功,可能原因:當前使用者被mysql伺服器拒絕或者3306埠被防火牆禁掉,無法連線到該埠
  • 解決方式:設定所有主機都可以訪問,關閉防火牆(推薦開放指定埠)

Redis服務-單機(版本:5.0.4)

  • redis官網下載安裝包,或用如下命令直接下載
wget http://download.redis.io/releases/redis-5.0.4.tar.gz
  • 解壓至 /usr/local/redis
tar xzvf redis-5.0.4.tar.gz -C /usr/local/redis
  • 安裝
cd /usr/local/redis/redis-5.0.4
make
cd src
make install PREFIX=/usr/local/redis

#如果此時安裝編譯出錯,執行如下步驟
#安裝gcc
yum -y install cpp binutils glibc glibc-kernheaders glibc-common glibc-devel gcc make
#升級gcc
yum -y install centos-release-scl devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils
scl enable devtoolset-9 bash
#重新編譯
make MALLOC=libc
#重新安裝
make install PREFIX=/usr/local/redis
  • 配置後臺啟動
vi /usr/local/redis/redis-5.0.4/redis.conf
#將daemonize no 改成daemonize yes
  • 設定開機啟動
vi /etc/rc.local
#新增如下內容
/usr/local/redis/bin/redis-server /usr/local/redis/redis-5.0.4/redis.conf
  • 啟動Redis
/usr/local/redis/bin/redis-server /usr/local/redis/redis-5.0.4/redis.conf
  • 將redis-cli,redis-server拷貝到bin下,讓redis-cli指令可以在任意目錄下直接使用(可選,根據需求來)
  • 設定redis密碼
#進入客戶端,如果沒有將命令拷貝到bin,需在用全路徑訪問
/usr/local/redis/bin/redis-cli 
#檢視密碼(可選)
config get requirepass
#設定密碼passward填自己的密碼
config set requirepass passward
#可在本機測試一下
/usr/local/redis/bin/redis-cli  -h 127.0.0.1 -p 6379 -a passward
  • 配置防火牆,使外網能夠訪問redis
#開放埠
firewall-cmd --zone=public --add-port=6379/tcp --permanent
#重啟防火牆以使配置即時生效
systemctl restart firewalld
#檢視系統所有開放的埠
firewall-cmd --zone=public --list-ports
  • 修改配置檔案/usr/local/redis/redis-5.0.4/redis.conf
#註釋掉bind 127.0.0.1,使服務能夠監聽外網請求
#bind 127.0.0.1

#關掉保護模式
protected-mode no

#passward為上面設定的密碼
requirepass passward
  • 重啟redis服務
#passward為上面設定的密碼
/usr/local/redis/bin/redis-cli -h 127.0.0.1 -p 6379 -a passward  shutdown
/usr/local/redis/bin/redis-server /usr/local/redis/redis-5.0.4/redis.conf

FFmpeg服務

  • 參考文章地址
  • yum install epel-release -y
  • yum update -y
  • sudo rpm --import http://li.nux.ro/download/nux/RPM-GPG-KEY-nux.ro
  • sudo rpm -Uvh http://li.nux.ro/download/nux/dextop/el7/x86_64/nux-dextop-release-0-5.el7.nux.noarch.rpm
  • yum install ffmpeg ffmpeg-devel -y

Nginx服務

  • 安裝依賴包
yum -y install gcc gcc- c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel 
wget http://nginx.org/download/nginx-1.9.9.tar.gz
  • 解壓至/usr/local/etc/nginx
tar -zxvf  nginx-1.9.9.tar.gz -C /usr/local/etc/nginx
  • 編譯
./configure --prefix=/usr/local/nginx/ --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/nginx.pid  --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre
make && make install
  • 切換至/usr/local/nginx/sbin/目錄,檢查編譯項
./nginx -V
  • 根據需要配置/usr/local/nginx/conf/nginx.conf,參考示例:
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       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"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
upstream user {
  server localhost:8083;
}
upstream upload {
  server localhost:8089;
  server localhost2:8089;
}

server {
  listen       80;
  server_name  www.example.com.cn; 

  #https配置示例
  #listen       443 ssl;
  #server_name www.example.com.cn;
  #client_max_body_size 5120M;
  #charset utf-8;
  #證照檔案位置
  #ssl_certificate      /usr/local/nginx/conf/ssl/3377808__example.com.pem;
  #ssl_certificate_key  /usr/local/nginx/conf/ssl/3377808__example.com.key;
  #ssl_session_cache    shared:SSL:1m;
  #ssl_session_timeout  5m;
  #ssl_protocols SSLv2 SSLv3 TLSv1 TLSv1.1 TLSv1.2;
  #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
  #ssl_prefer_server_ciphers  on;

        
    location / {
            #前端服務地址
            proxy_pass http://localhost:3000;
            proxy_set_header Host $host;
            proxy_set_header  X-Real-IP        $remote_addr;
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
    }

    location /user/ {
            #使用者-後端服務地址
            proxy_pass http://user/user/;
            client_body_buffer_size 128k;
            client_max_body_size 10m;
            proxy_set_header Host $host;
            proxy_set_header  X-Real-IP        $remote_addr;
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
    }

    location /upload/ {
            #檔案上傳-後端服務地址
            proxy_pass http://upload/upload/;
            client_body_buffer_size 128k;
            client_max_body_size 10m;
            proxy_set_header Host $host;
            proxy_set_header  X-Real-IP        $remote_addr;
            proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header X-NginX-Proxy true;
    }

    location /imagepool {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        #圖片存放目錄
        root /data/;
    }

     location /videopool {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        #視訊存放目錄
        root /data/;
    }
}
}
  • 檢查配置檔案是否正確
/usr/local/nginx/sbin/nginx  -t 
  • 啟動Nginx
/usr/local/nginx/sbin/nginx 

相關文章