CentOS7 環境配置指南

luci發表於2020-05-07

一、免密登入

1)ifconfig 檢視本機ip。

2)建立遠端登入使用者:

  • adduser username 建立使用者
  • passwd username 修改密碼

3)給該使用者設定管理員許可權:(可選)

  • 開啟 /etc/sudoers 檔案,新增 username ALL=(ALL) ALL

4)將公鑰複製到伺服器:

  • ssh-keygen 命令可生成金鑰。如果本機已在其他伺服器設定了免密登入,就不要重新生成(否則你其他伺服器的公鑰就失效了),直接使用 ssh-copy-id 將原有公鑰複製至需要免密登入的伺服器即可。如
ssh-copy-id -i 公鑰路徑 username@remote-server
  • 本機的公鑰路徑可透過 ssh-keygen 命令檢視:
lucMacBook-Pro:~ binhua$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Path/.ssh/id_rsa): 

第三行的括號部分就是金鑰路徑,此時 ctrl+C 退出即可。

  • ssh username@remote-server 測試免密登入,OJBK!

二、防火牆配置

1)關閉selinux。

  • 修改 /etc/selinux/config 檔案:
SELINUX=enforcing  #把 enforcing 改為 disabled

2)配置firewalld。

  • 檢查 firewalld 執行狀態,開啟常用的服務的監聽埠,如SSH、FTP。開啟埠時加 --permanent 引數設定為永久生效。

三、調教Vim

1)vim編輯器的配置檔案:

  • Mac位於:~/.vimrc
  • Centos7位於:/etc/vimrc

2)常用配置:

set nu                      
syntax on
set autoindent
set tabstop=2
set tags=/data/php/test_mysql/tags
set nohlsearch    
set ruler
set history=1000
set showmatch
set cursorline
filetype plugin on
autocmd FileType php set omnifunc=phpcomplete#CompletePHP
set incsearch "輸入搜尋內容時就顯示搜尋結果
set matchtime=5  " 匹配括號高亮的時間(單位是十分之一秒)
"colorscheme molokai
set expandtab "用空格代替tab 

四、FTP服務搭建(以vsftpd為例)

1)檢查是否已安裝ftp工具。

[binhua@centos-7 ~]$ vsftpd -v
vsftpd: version 3.0.2

2)如顯示版本號則已安裝,否則使用 yum -y install vsftpd 安裝。

3)為ftp服務建立專屬使用者。

4)修改ftp配置。檔案位置為 /etc/vsftpd/vsftpd.conf.

anonymous_enable=NO    # 是否允許匿名訪問
local_enable=YES      # 是否允許使用本地帳戶進行 FTP 使用者登入驗證
chroot_local_user=YES   # 是否限定使用者在其主目錄下(NO 表示允許切換到上級目錄)
#chroot_list_enable=YES # 是否啟用限制使用者的名單(註釋掉為禁用)
chroot_list_file=/etc/vsftpd/chroot_list # 使用者列表檔案(一行一個使用者)
allow_writeable_chroot=YES # 如果啟用了限定使用者在其主目錄下需要新增這個配置,解決報錯 500 OOPS: vsftpd: refusing to run with writable root inside chroot()
xferlog_enable=YES     # 啟用上傳和下載的日誌功能,預設開啟。

五、安裝Nginx1.17

1)安裝依賴包。

yum -y install pcre pcre-devel openssl openssl-devel gcc gcc-c++ autoconf automake zlib-devel libxml2 libxml2-dev libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data make  GeoIP-devel GeoIP-update

2)下載nginx 1.17安裝包。

curl -o nginx-1.17.9.tar.gz http://nginx.org/download/nginx-1.17.9.tar.gz

3)建立使用者和組。

groupadd nginx
useradd nginx -g nginx -s /sbin/nologin -M

4)解壓,配置,編譯,安裝。

tar -zxvf nginx-1.17.9.tar.gz
cd nginx-1.17.9

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_geoip_module --with-http_stub_status_module --with-http_sub_module --with-stream --with-stream=dynamic

make && make install

5)新增環境變數。

  • Centos的環境變數在 /etc/profile 中設定。
  • Mac的環境變數在 ~/.bash_profile 中設定。(引申)

6)新增為系統服務。

vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

7)設定開機自啟動。

systemctl enable nginx.service

六、安裝php7.2

1)安裝EPEL包。(注:php高版本的yum源地址,有兩部分,其中一部分是epel-release,另外一部分來自webtatic。如果跳過epel-release直接安裝webtatic的時候,會報錯)

yum install epel-release -y

2)安裝webtatic-release。

rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

3)安裝php豪華套餐包

yum -y install php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml

4)設定開機自啟動。

systemctl enable php-fpm.service

七、安裝Mysql5.7(可選)

從 CentOS 7 系統開始,MariaDB 成為 yum 源中預設的資料庫安裝包。在 CentOS 7 及以上的系統中使用 yum 安裝 MySQL 包將無法使用 MySQL。您可以選擇使用完全相容的 MariaDB,或依照本文介紹配置來繼續使用 MySQL。
1)檢查 MariaDB 是否安裝,如果安裝了就解除安裝全部。

yum list installed | grep mariadb   #檢查是否安裝
yum -y remove mariadb*          #解除安裝

2)下載、安裝、檢查 MySQL 的 YUM 源。

cd /data   #進入要下載的路徑
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
rpm -ivh mysql57-community-release-el7-11.noarch.rpm   #安裝
yum repolist enabled | grep "mysql.*-community.*"     #檢查是否成功

如果出現相關mysql的項則表示安裝成功。
3)安裝 MySQL。

yum install mysql-community-server -y

4)設定 MySQL 開機自啟動。

systemctl enable mysqld.service

測試連線Mysql服務

剛安裝的 MySQL 是沒有密碼的,這時如果出現:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

解決方式如下:
a) 停止服務:  systemctl stop mysqld
b) 以不檢查許可權的方式啟動 MySQL:  mysqld --user=root --skip-grant-tables &
c) 再次輸入mysql -u root 或者 mysql 就可以進來了。
d) 更新密碼:

  • 5.7以下版本:
UPDATE mysql.user SET Password=PASSWORD('123456') where USER='root';
  • 5.7及以上版本:
UPDATE mysql.user SET authentication_string=PASSWORD('123456') where USER='root';

e) 重新整理:  flush privileges;

八、安裝MariaDB

1)安裝MariaDB前請確保已經移除Mysql。

rpm -qa |grep -i mysql  #檢視mysql安裝的檔案

2)如果有相關Mysql的檔案返回,請使用 yum remove 逐個解除安裝。
3)使用1)的命令檢查是否解除安裝完成。
4)查詢並刪除Mysql相關檔案。

find / -name mysql
rm -rf mysql目錄名
rm -rf /etc/my.cnf
rm -rf /var/log/mysqld.log #不刪除會導致新安裝的mysql無法生存新密碼,導致無法登陸

5)安裝MairaDB。

yum -y install mariadb mariadb-server

6)開啟MairaDB服務,並設定開機自啟動。

systemctl start mariadb
systemctl enable mariadb

7)簡單的相關配置。

mysql_secure_installation

8)測試登陸。

mysql -uroot -ppassword

九、搭建GitLab服務

安裝並配置必要依賴項

1)安裝相關依賴,並在防火牆中開啟http服務。

yum install -y curl policycoreutils-python openssh-server
systemctl enable sshd  #設定ssh服務開機啟動
systemctl start sshd   #啟動ssh服務
firewall-cmd --permanent --add-service=http  #開啟http服務
firewall-cmd --reload    #重啟防火牆

2)安裝Postfix以傳送郵件。(可選)

yum install postfix
systemctl enable postfix
systemctl start postfix

新增GitLab軟體

1)國內使用者推薦手動下載清華大學映象站的 RPM 安裝包進行安裝,地址是如果透過官方管道的方式安裝,90% 都會報錯:Http error 302

$ curl -LJO https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-8.9.8-ce.0.el7.x86_64.rpm
$ rpm -i gitlab-ce-8.9.8-ce.0.el7.x86_64.rpm

2)配置GitLab。配置檔案是 /etc/gitlab/gitlab.rb.
3)啟動和管理操作

$ sudo gitlab-ctl reconfigure
$ sudo gitlab-ctl status
$ sudo gitlab-ctl stop
$ sudo gitlab-ctl restart
$ sudo ps aux | grep runsvdir

4)禁用自帶nginx。

#設定nginx為false
nginx['enable'] = false

5)重啟nginx,重啟GitLab。
6)訪問會報 502。原本是 nginx 使用者無法訪問 gitlab 使用者的 socket 檔案,使用者許可權配置,因人而異

chmod -R o+x /var/opt/gitlab/gitlab-rails

GitLab漢化指南(可選)

目前的GitLab版本已經到了11.X,所以推薦使用 xhang 專案。
1)根據安裝的版本,檢視xhang專案是否支援該版本漢化。(只要前兩位版本號一致就可以)

cat /opt/gitlab/embedded/service/gitlab-rails/VERSION   #檢視安裝版本
git clone https://gitlab.com/xhang/gitlab.git  #克隆gitlab目錄

OMG……..我的版本實在是太高了,11.11了都,人家的最新版是9.6。。。看來漢化只能先擱置了,以後再補充。
2)打漢化補丁(以11.11.0-ee版本為例)

# 進入克隆倉庫得到的gitlab目錄
cd gitlab 
# 檢視tag版本,選擇合適的漢化版本
git tag
# 對比不同,這裡比較的是tag,v9.2.6為英文原版,v9.2.6-zh為漢化版本。diff結果是漢化補丁。
git diff v9.2.6..v9.2.6-zh > /tmp/9.2.6.diff
# 停止gitlab
gitlab-ctl stop
# 應用漢化補丁
cd /opt/gitlab/embedded/service/gitlab-rails
git apply /tmp/9.2.6.diff
# 啟動gitlab
gitlab-ctl start

十、安裝composer

1)下載 composer.phar 檔案。

curl -sS https://getcomposer.org/installer | php

2)移動 composer.phar 檔案到 /usr/local/bin 目錄下 使命令全域性可用。

mv composer.phar /usr/local/bin/composer

3)如需升級為最新的composer版本,請執行composer self-update

十一、安裝nodejs

1)先確認是否已經安裝了 epel-release 包。

yum info epel-release

2)如有輸出相關的安裝資訊說明已安裝,否則執行以下命令安裝。

yum install epel-release

3)安裝nodejs。

yum install nodejs

十二、安裝cnpm

1)安裝cnpm要先安裝nodejs,如果已經安裝,執行以下命令安裝cnpm。

npm install -g cnpm --registry=https://registry.npm.taobao.org

2)升級cnpm。

cnpm install cnpm -g

十三、安裝vue

cnpm install vue    #安裝vue
cnpm install --global vue-cli   #安裝vue-cli

十四、申請https證照

1)安裝 certbot。

yum install epel-release
yum install certbot

2)生成證照,這裡採用 webroot 作為 Let’s Encrypt 的認證方式。

certbot certonly -a webroot --webroot-path=/data -d www.binhua.site -d admin.binhua.site -d api.binhua.site -d beta.binhua.site

webroot-path 就是你的專案路徑,使用 -d 可以新增多個域名。這時證照就已經生成成功了,預設儲存在 /etc/letsencrypt/live/example.com/ 下。證照檔案包括:
cert.pem: 服務端證照
chain.pem: 瀏覽器需要的所有證照但不包括服務端證照,比如根證照和中間證照
fullchain.pem: 包括了cert.pem和chain.pem的內容
privkey.pem: 證照私鑰

【注意⚠️】第二步執行時可能出錯,如果報錯:

ImportError: 'pyOpenSSL' module missing required functionality. Try upgrading to v0.14 or newer.

需要執行:

yum install http://cbs.centos.org/kojifiles/packages/pyOpenSSL/16.2.0/3.el7/noarch/python2-pyOpenSSL-16.2.0-3.el7.noarch.rpm

即可成功。

十五、安裝配置redis

1)macos安裝redis

最簡單粗暴的做法就是 brew install redis.安裝成功後修改配置檔案 redis.config 即可.

2)centos7安裝redis

方法一:原始檔安裝(推薦安裝)

在CentOS和Red Hat系統中,首先新增EPEL倉庫,然後更新yum源:

sudo yum install epel-release
sudo yum update

然後安裝Redis資料庫:

sudo yum -y install redis

安裝好後啟動Redis服務即可:

sudo systemctl start redis

這裡同樣可以使用redis-cli進入Redis命令列模式操作。

另外,為了可以使Redis能被遠端連線,需要修改配置檔案:

vim /etc/redis.conf

首先,註釋這一行:

# bind 127.0.0.1

另外,推薦給Redis設定密碼,取消註釋這一行:

# requirepass foobared

foobared即當前密碼.
然後重啟Redis服務,使用的命令如下:

sudo systemctl restart redis
systemctl start redis.service   #啟動redis伺服器
systemctl stop redis.service   #停止redis伺服器
systemctl restart redis.service   #重新啟動redis伺服器
systemctl status redis.service   #獲取redis伺服器的執行狀態
systemctl enable redis.service   #開機啟動redis伺服器
systemctl disable redis.service   #開機禁用redis伺服器

方法二:壓縮包安裝
1、在官網下載tar.gz的安裝包,或者透過wget的方式下載  

wget http://download.redis.io/releases/redis-4.0.1.tar.gz

2、解壓

tar -zxvf redis-4.0.1.tar.gz

3、編譯
透過make來編譯,make是自動編譯,會根據Makefile中描述的內容來進行編譯。

cd redis-4.0
make

可以看到在src目錄下生成了幾個新的檔案。

4、安裝

make install

實際上,就是將這個幾個檔案加到/usr/local/bin目錄下去。這個目錄在Path下面的話,就可以直接執行這幾個命令了。
可以看到,這幾個檔案就已經被載入到bin目錄下了。

二十五、其他需要安裝的軟體。

  • git
本作品採用《CC 協議》,轉載必須註明作者和本文連結
程式碼是寫給人看的,順便給機器執行一下。

相關文章