Centos7 編譯安裝 Nginx Mariadb Asp.net Core2 (實測 筆記 Centos 7.7 + Openssl 1.1.1d + Mariadb 10.3.7 + Nginx

環境:html

系統硬件:vmware vsphere (CPU:2*4核,內存2G,雙網卡)python

系統版本:CentOS-7-x86_64-Minimal-1611.isomysql

 

安裝步驟:linux

1.準備nginx

1.0 查看硬件信息c++

查看物理cpu個數
 grep 'physical id' /proc/cpuinfo | sort -u | wc -lgit

查看核心數量
 grep 'core id' /proc/cpuinfo | sort -u | wc -lgithub

查看線程數
 grep 'processor' /proc/cpuinfo | sort -u | wc -lweb

查看硬盤空間佔用算法

du -h -x --max-depth=1  

df -h

查看目錄空間佔用

 du -msh /usr/local/src

查看物理內存,以及swap使用狀況

free -h

******************************************

把 /usr/local/src 目錄,轉到 /data 下

mkdir -p /data

 mv /usr/local/src /data

 ln -s /data/src /usr/local/src

******************************************

安裝基本軟件包

yum install vim wget lsof gcc gcc-c++ bzip2 firewalld openssl-devel mlocate -y

 

1.1 主機名設置

當前主機名查看

hostname

主機名設置

 hostnamectl --static set-hostname tCentos

systemctl restart network

hostname

tCentos

 

修改目錄日期顯示格式

  vim ~/.bash_profile

最後添加如下內容

export TIME_STYLE='+%Y%m%d %H:%M'

保存退出
 source ~/.bash_profile

 

1.2 設置靜態IP、DNS地址(網絡設備名稱有可能不同,這裏是eno16780032,如使用DHCP獲取動態IP,可忽略)

vi /etc/sysconfig/network-scripts/ifcfg-eno16780032

找到BOOTPROTO,而且修改(設爲靜態網址)

BOOTPROTO="static"

在最後添加三行內容(添加本機IP,子網掩碼,網關)

IPADDR="192.168.1.10"
NETMASK="255.255.255.0"
GATEWAY="192.168.1.1"

:wq 保存退出

shutdown -r now

添加如下幾個DNS地址

 

cat >> /etc/resolv.conf << EOF
nameserver 114.114.114.114
nameserver 192.168.1.1
nameserver 8.8.8.8
EOF

systemctl restart network

ip addr|grep inet

inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
inet 192.168.1.10/24 brd 192.168.1.255 scope global eno16780032
inet6 fe80::250:56ff:feb0:30f2/64 scope link

 

1.3 更新時間,設置定時同步時間

yum install -y ntpdate

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

手動設置系統時間(測試用)

date -s "2000-1-1"

/usr/sbin/ntpdate us.pool.ntp.org

******************************************************************

同步時間站點:

us.pool.ntp.org

time.nist.gov

******************************************************************

查看當前系統時間

 systemctl stop ntpd && systemctl disable ntpd

date

設置定時任務,自動執行

mkdir -p /data/crond

添加如下內容 (或者crontab -e 打開後添加)
 
echo "00 02 * * * /usr/sbin/ntpdate us.pool.ntp.org 1>>/data/crond/ntpdate.log 2>&1" >> /var/spool/cron/root

天天 02:00同步一次,而且日誌記錄到 /data/crond/ntpdate.log

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

更新系統,顯示系統版本(使用阿里雲源)

 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

 yum clean all && yum makecache

yum update -y

更新完成後重啓,查看內核版本

shutdown -r now

cat /etc/redhat-release

CentOS Linux release 7.7.1908 (Core)

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

1.4 配置Vim顯示格式

 vim /etc/vimrc

在末尾添加如下內容

set nocompatible
set number
filetype on
set history=1000
set background=dark
syntax on
set autoindent
set smartindent
set tabstop=2
set shiftwidth=2
set showmatch
set guioptions-=T
set vb t_vb=
set ruler
set nohls
set incsearch
if has("vms")
set nobackup
else
set backup
endif

:wq 保存退出 

VIM格式化使用方法(打開文件後,輸入如下命令,第二個G是shift+g)

gg=G

 

打開selinux ( centos 7.3某個版本設置爲enforcing會致使無法重啓)

vim /etc/selinux/config

找到這一行,去掉前面的#註釋

SELINUX=enforcing

保存,退出


重啓後,查詢是否關閉(顯示Enforcing表示啓用,Disabled則表示關閉)

shutdown -r now

getenforce

Enforcing

 

1.5 升級 gcc

1..1 下載 gcc,解壓

cd /usr/local/src

wget ftp://ftp.mirrorservice.org/sites/sourceware.org/pub/gcc/releases/gcc-9.2.0/gcc-9.2.0.tar.gz

tar zvxf gcc-9.2.0.tar.gz && cd gcc-9.2.0

1.5.2 下載依賴文件,而且刷新 (能夠事先下載複製進去)

**************************************************************************************************************

cp ../isl-0.18.tar.bz2 ./
cp ../mpc-1.0.3.tar.gz ./
cp ../mpfr-3.1.4.tar.bz2 ./
cp ../gmp-6.1.0.tar.bz2 ./

**************************************************************************************************************

./contrib/download_prerequisites && ldconfig

1.5.3 建立編輯目錄

mkdir gcc-build && cd gcc-build


1.5.4 編譯 
../configure --enable-languages=c,c++ --disable-multilib --enable-checking=release --prefix=/opt/gcc

# --prefix=/opt/gcc 指定安裝目錄(如指定/usr,則覆蓋原默認目錄,編譯後不須要重建軟鏈接)
# --enable-languages,說明你要讓你的gcc支持那些語言
# --disable-multilib不生成編譯爲其餘平臺可執行代碼的交叉編譯器
# --disable-checking生成的編譯器在編譯過程當中不作額外檢查,也可使用
# --enable-checking=xxx來增長一些檢查


1.5.5 編譯、安裝
make && make install
**************************************************************************************************************
ldconfig

如提示「ldconfig: /opt/gcc/lib64/libstdc++.so.6.0.27-gdb.py is not an ELF file」

  mv  /opt/gcc/lib64/libstdc++.so.6.0.27-gdb.py   /opt/gcc/lib64/bak.libstdc++.so.6.0.27-gdb.py
**************************************************************************************************************

echo '/opt/gcc/lib64' > /etc/ld.so.conf.d/local-lib64.conf

ldconfig -v

mv /usr/bin/gcc /usr/bin/gcc.bak

mv /usr/bin/g++ /usr/bin/g++.bak

ln -s /opt/gcc/bin/gcc /usr/bin/gcc

ln -s /opt/gcc/bin/g++ /usr/bin/g++

update-alternatives --install /usr/bin/gcc gcc /opt/gcc/bin/gcc 999

 

1.5.6 重啓,查看版本,檢查是否成功更新
shutdown -r now

gcc --version

g++ --version

 

1.6 更新內核 (4.9 版本及以上的內核均支持全新的 Google BBR TCP 擁塞控制算法)

1.6.1 源碼編譯內核 
yum install vim wget gcc gcc-c++ xz bc ncurses-devel hmaccalc zlib-devel binutils-devel elfutils-libelf-devel bison flex

cd /usr/local/src/

wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.3.6.tar.xz --no-check-certificate

tar -vxf linux-5.3.6.tar.xz && cd linux-5.3.6

uname -r

3.10.0-1062.1.2.el7.x86_64 

cp /boot/config-3.10.0-1062.1.2.el7.x86_64 .config
導入當前配置

make menuconfig

打開菜單後,加入 BBR 模塊
Networkingsupport -> Networking options -> TCP:advanced congestion control -> BBR TCP 按M

保存後,退出

sudo sh -c 'yes "" | make oldconfig'
以原配置文件產生新的配置文件,默認回答爲YES方式

make && make modules_install install

查看可用內核,設置默認啓動內核

[root@centos ~] cat /boot/grub2/grub.cfg |grep menuentry

[root@centos ~] grub2-set-default 'CentOS Linux (5.3.6) 7 (Core)'

[root@centos ~] grub2-editenv list

shutdown -r now

開啓 TCP BBR

[root@centos ~]  echo 'net.core.default_qdisc=fq' | sudo tee -a /etc/sysctl.conf
[root@centos ~]  echo 'net.ipv4.tcp_congestion_control=bbr' | sudo tee -a /etc/sysctl.conf
[root@centos ~]  sudo sysctl -p

[root@centos ~]  cat /etc/sysctl.conf

查看是否開成功

[root@centos ~]  sysctl net.ipv4.tcp_available_congestion_control
[root@centos ~]  sysctl -n net.ipv4.tcp_congestion_control
[root@centos ~]  lsmod | grep bbr

有相似的輸出,表未成功開啓BBR
net.ipv4.tcp_available_congestion_control = bbr cubic reno
bbr
tcp_bbr 16384 0

1.6.2 刪除舊內核
rpm -qa | grep kernel

yum -y remove kernel kernel-devel kernel-tools kernel-tools-libs

1.6.3 手工清理及卸載內核

刪除/lib/modules/目錄下不須要的內核庫文件
刪除/boot目錄下啓動的內核和內核映像文件
更改grub的配置文件,刪除不須要的內核啓動列表

1.6.4 若是不想升級內核及系統版本,則在執行 yum update以前

在 /etc/yum.conf 的 [main] 後面添加如下配置
exclude=kernel* 
exclude=centos-release*

 

1.7 設置PUTTY遠程登陸時,不使用密碼,使用密鑰文件登陸(如不須要,可忽略)

1.7.1 服務器上建立目錄

mkdir -p /root/.ssh

 

1.7.2 在"客戶機"生成對稱密鑰,把客戶機上的公鑰複製到服務器(公鑰文件:id_rsa.pub)

[root@centos ~] ssh-keygen -m PEM -t rsa -b 4096 

根據提示操做,生成公鑰

上傳到服務器指定目錄(*** 或使用軟件遠程複製id_rsa.pub到服務器/root/.ssh中。)

[root@centos ~] scp id_rsa.pub root@192.168.1.10/root/.ssh

 

1.7.3 查看服務器上,公鑰是否已經存在

cd /root/.ssh

ll
-rw-r--r-- 1 root root 394 12月 5 09:33 id_rsa.pub

導入密鑰到authorized_keys

cat id_rsa.pub >> authorized_keys

ll /root/.ssh
-rw-r--r-- 1 root root 394 12月 5 09:37 authorized_keys
-rw-r--r-- 1 root root 394 12月 5 09:33 id_rsa.pub

導入後,刪除公鑰文件

rm id_rsa.pub

設置目錄和文件讀取權限

 chmod 700 /root/.ssh

 chmod 600 /root/.ssh/authorized_keys


1.7.4 設置sshd配置文件

vim /etc/ssh/sshd_config

找到GSSAPICleanupCredentials,而且修改成如下內容

GSSAPICleanupCredentials yes

:wq 保存退出


重啓sshd服務,讓其生效

systemctl restart sshd


1.7.5 客戶端設置PUTTY,進行遠程登陸

打開軟件 PuTTYgen

點擊load 選擇以前客戶機生成私鑰文件id_rsa, 點擊save private key 生成 pKey.ppk文件

打開軟件 PuTTY

點擊Session,在HostName(or IP address)輸入服務器地址

點擊Connection下的DATA,在Auto-login username中輸入登陸帳號(當前帳號爲root)

點擊Connection下的SSH下的Auth,點擊Browse 選擇以前生成 pKeyppk文件

點擊Session,在Saved Sessions中,輸入須要保存的Session名稱,點擊保存


1.7.6 設置完成後,便可以遠程鏈接到服務器

打開軟件 PuTTY

點擊Session,在"Default Settings"下,找到以前已經保存的Session,雙擊打開鏈接

若是顯示 Authenticating with public key "xxxxx-xxxx"時,即表未成功

 


1.8 設置新用戶,而且使用密碼和證書雙重認證遠程登陸。同時禁止root遠程登陸  (如不須要,可忽略)

1.8.1 root登陸後,修改root密碼 (安全建議:密碼爲15位,大小字母+數字+特殊字符)

passwd

 

1.8.2 添加新用戶,而且設置密碼

adduser vicowong

passwd vicowong

 

1.8.3 建立目錄,複製密鑰相關文件到用戶目錄,而且設置權限

 mkdir /home/vicowong/.ssh -p

 cp /root/.ssh/authorized_keys /home/vicowong/.ssh

 chmod 700 /home/vicowong/.ssh

 chmod 600 /home/vicowong/.ssh/authorized_keys

 chown vicowong:vicowong /home/vicowong/.ssh -R

 

1.8.4 設置防火牆,設置遠程鏈接端口(這裏是26322)

systemctl enable firewalld && systemctl start firewalld 

 firewall-cmd --zone=public --add-port=26322/tcp --permanent 

 firewall-cmd --reload && iptables -L --line-numbers|grep ACCEPT

 

1.8.5 安裝semanage(用於設置selinux策略) 

 yum install -y policycoreutils-python selinux-policy selinux-policy-targeted 

查看當前 selinux 是否啓用 即 Enforcing 狀態 (不然有可能設置 selinux 策略不成功)

 getenforce

查看當前 selinux 關於遠程ssh鏈接端口的設置

 semanage port -l | grep ssh

ssh_port_t                     tcp      22

添加新端口
 semanage port -a -t ssh_port_t -p tcp 26322

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

移除端口
semanage port -d -t ssh_port_t -p tcp 26322

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

1.8.6 設置sshd配置文件

vim /etc/ssh/sshd_config

找到如下內容,而且進行修改

Port 26322

Protocol 2

ServerKeyBits 1024

PermitRootLogin no

AllowUsers vicowong

StrictModes yes

RSAAuthentication yes

PubkeyAuthentication yes

AuthorizedKeysFile  .ssh/authorized_keys

PermitEmptyPasswords no

PasswordAuthentication yes

AuthenticationMethods publickey,password

X11Forwarding no

MaxStartups 10:30:60

:wq 保存退出


重啓sshd服務,讓其生效

systemctl restart sshd

 

1.8.7 使用新用戶登陸(從新打開一個新終端,原來的終端先不關,避免因設置不當致使無法鏈接遠程)

打開軟件 PuTTY,點擊以前保存的Sessions,點擊Load讀取以前的配置

在Port框輸入端口(當前帳號爲26322)

點擊Connection下的DATA,在Auto-login username中輸入登陸帳號(當前帳號爲vicowong)

點擊Session  點擊Save。保存當前修改。

點擊Open,打開終端。

 

1.8.8 設置後,必須遠程將進行密碼和證書雙重認證。

遠程登陸會以vicowong這個帳號進行登陸。安裝維護須要root權限時,可使用su實現

 su root 

 

1.9 開機自動禁止被ping (設置爲 1:禁止,0:容許)

 chmod +x /etc/rc.d/rc.local

vim /etc/rc.d/rc.local 

打開後,在最後增長一行

echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all

:wq 保存退出

cat /proc/sys/net/ipv4/icmp_echo_ignore_all

1

 

1.10 更新安裝openssl (依賴zlib庫)

cd /usr/local/src/

wget http://zlib.net/zlib-1.2.11.tar.gz

tar zvxf zlib-1.2.11.tar.gz && cd zlib-1.2.11

./configure && make && make install


openssl version

cd /usr/local/src/

wget https://www.openssl.org/source/openssl-1.1.1d.tar.gz

tar zvxf openssl-1.1.1d.tar.gz && cd openssl-1.1.1d

./config shared zlib --prefix=/usr && make && make install

*****************************************************************************************************************
#更新軟鏈接 (如編譯指定不是/usr目錄則須要添加軟鏈接,如指定/ --prefix=/opt/openssl )

mv /usr/bin/openssl /usr/bin/openssl_bak

mv /usr/include/openssl/ /usr/include/openssl_bak

ln -s /opt/openssl/bin/openssl /usr/bin/openssl

ln -s /opt/openssl/include/openssl/ /usr/include/openssl

echo "/opt/openssl/lib" >> /etc/ld.so.conf

*****************************************************************************************************************

#查看最新版本

ldconfig -v | grep ssl

openssl version

 

1.11 安裝jemalloc(須要 bzip2 庫解壓)

cd /usr/local/src/

 wget https://github.com/jemalloc/jemalloc/releases/download/5.2.1/jemalloc-5.2.1.tar.bz2

tar xjf jemalloc-5.2.1.tar.bz2 && cd jemalloc-5.2.1

./configure && make && make install

echo '/usr/local/lib' > /etc/ld.so.conf.d/local.conf

ldconfig

 


2.安裝mariadb

2.1 安裝依賴

yum install ncurses-devel zlib-devel openssl-devel  bzip2 m4 -y

 

2.2 安裝cmake

cd /usr/local/src/

wget https://cmake.org/files/v3.15/cmake-3.15.3.tar.gz

tar zvxf cmake-3.15.3.tar.gz && cd cmake-3.15.3

./bootstrap && make && make install

 

2.2 安裝bison(須要 m4 庫)

cd /usr/local/src/

wget http://ftp.gnu.org/gnu/bison/bison-3.4.tar.gz

tar zvxf bison-3.4.tar.gz && cd bison-3.4

./configure && make && make install

 

2.3 安裝libevent(依賴openssl庫)

***********************************

yum reinstall openssl-devel  -y

ldconfig

cd /usr/local/src

wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz

tar zvxf libevent-2.1.8-stable.tar.gz && cd libevent-2.1.8-stable

 ./configure --prefix=/usr && make && make install

 ll /usr/lib | grep libevent

updatedb && locate libevent

若是libevent的lib目錄不在LD_LIBRARY_PATH裏,可使用如下命令加入

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr

 

2.4 建立mysql須要的目錄、配置用戶和用戶組

groupadd mysql

useradd -g mysql mysql -s /sbin/nologin -M

mkdir -p /data/mysql

chown -R mysql:mysql /data/mysql


2.5 編譯mariadb(須要 cmake ncurses-devel bison 庫)

cd /usr/local/src/

wget https://mirrors.shu.edu.cn/mariadb//mariadb-10.3.17/source/mariadb-10.3.17.tar.gz

tar zvxf mariadb-10.3.17.tar.gz && cd mariadb-10.3.17

cmake \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_INSTALL_PREFIX=/opt/mysql \
-DINSTALL_DOCDIR=share/doc/mariadb \
-DINSTALL_DOCREADMEDIR=share/doc/mariadb \
-DINSTALL_MANDIR=share/man \
-DINSTALL_MYSQLSHAREDIR=share/mysql \
-DINSTALL_MYSQLTESTDIR=share/mysql/test \
-DINSTALL_PLUGINDIR=lib/mysql/plugin \
-DINSTALL_SBINDIR=sbin \
-DINSTALL_SCRIPTDIR=bin \
-DINSTALL_SQLBENCHDIR=share/mysql/bench \
-DINSTALL_SUPPORTFILESDIR=share/mysql \
-DMYSQL_DATADIR=/data/mysql \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DWITH_EXTRA_CHARSETS=complex \
-DWITH_EMBEDDED_SERVER=ON \
-DTOKUDB_OK=0 \
-DCMAKE_EXE_LINKER_FLAGS="-ljemalloc" \
-DWITH_SAFEMALLOC=OFF \

make && make install

**************************************************************

若是編譯不帶 -DCMAKE_EXE_LINKER_FLAGS="-ljemalloc" -DWITH_SAFEMALLOC=OFF 參數

也能夠編輯 /opt/mysql/bin/mysqld_saf 文件,來實現加載 jemalloc

 sed -i 's@executing mysqld_safe@executing mysqld_safe\n export LD_PRELOAD=/usr/local/lib/libjemalloc.so@' /opt/mysql/bin/mysqld_safe

**************************************************************

2.6 建立軟鏈接

ln -s /opt/mysql/lib/lib* /usr/lib/

ln -s /opt/mysql/bin/mysql /bin

ln -s /opt/mysql/bin/mysqldump /bin

ln -s /opt/mysql/bin/mysqlbinlog /bin

 

2.7 修改配置文件

vim /etc/my.cnf

刪除原來的內容,直接替換掉如下代碼

[client]
port = 3306
default-character-set = utf8mb4
socket = /tmp/mysql.sock

[mysqld]
port = 3306
datadir = /data/mysql
max_connections=1000
character-set-server = utf8mb4
ssl
socket = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 256M
max_allowed_packet = 1M
table_open_cache = 256
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 4M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size= 16M
thread_concurrency = 8

log-bin=mysql-bin

binlog_format=mixed

server-id = 1

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

:wq 保存退出


2.8 初始化數據庫

cd /opt/mysql

./bin/mysql_install_db --basedir=/opt/mysql --datadir=/data/mysql --user=mysql

./bin/mysqld_safe --datadir=/data/mysql

確認運行後,按能夠按CTRL+Z結束

ps -ef|grep mysqld

lsof -n | grep jemalloc


2.9 設置數據庫ROOT密碼,移除刪除臨時用戶,刪除測試數據庫等(根據提示操做)

./bin/mysql_secure_installation

輸入當前密碼。(默認爲空,回車便可)

Enter current password for root (enter for none):

是否設置root密碼,輸入Y,後,輸入兩次密碼

Set root password? [Y/n] y

New password:

Re-enter new password:

是否移除匿名用戶,輸入Y

Remove anonymous users? [Y/n] y

是否禁止root遠程登陸,輸入Y

Disallow root login remotely? [Y/n] y

是否刪除測試數據庫,輸入Y

Remove test database and access to it? [Y/n] y

是否從新讀取權限表數據

Reload privilege tables now? [Y/n] y

 

2.10 登陸數據庫,查看數據庫狀態

mysql -u root -p

MariaDB [(none)]> status;

MariaDB [(none)]> show engines;

MariaDB [(none)]> SHOW VARIABLES LIKE '%have%ssl%';

MariaDB [(none)]> exit;


2.11 設置mysql開機自動啓動服務

vim /etc/systemd/system/mysqld.service

錄入如下內容

[Unit]
Description=MySQL Community Server
After=network.target
After=syslog.target

[Install]
WantedBy=multi-user.target
Alias=mysql.service

[Service]
User=mysql
Group=mysql
LimitNOFILE=65535
LimitNPROC=65535

# Execute pre and post scripts as root
PermissionsStartOnly=true

# Needed to create system tables etc.

# Start main service
ExecStart=/opt/mysql/bin/mysqld_safe

# Don't signal startup success before a ping works

# Give up if ping don't get an answer
TimeoutSec=30

Restart=always
PrivateTmp=false


:wq 保存

systemctl enable mysqld.service

systemctl list-unit-files|grep enabled|grep mysql

systemctl daemon-reload


2.12 重啓,確認是否已自動啓動服務

shutdown -r now


2.13 增長遠程訪問用戶,而且打開防火牆3306端口(不遠程鏈接數據,可忽略)

mysql -u root -p

MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

MariaDB [(none)]> FLUSH PRIVILEGES;

MariaDB [(none)]> exit;

(root是用戶名,%是主機名或IP地址,這裏的%表明任意主機或IP地址,也可指定惟一的IP地址;密碼是MyPassword )


2.14 防火牆添加3306端口(不遠程鏈接數據,可忽略)

firewall-cmd --zone=public --add-port=3306/tcp --permanent

 firewall-cmd --reload && iptables -L --line-numbers|grep ACCEPT

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

最大鏈接數
MariaDB [(none)]> show global variables like 'max_connections';

MariaDB啓動後的累計鏈接數
MariaDB [(none)]> show global status like 'Connections';

mariaDB啓動後的最大同時鏈接數
MariaDB [(none)]> show global status like 'Max_used_connections';

MariaDB線程信息
MariaDB [(none)]> show global status like 'Thread_%'

MariaDB備用的線程信息
MariaDB [(none)]> show global variables like "thread_cache_size";

Threads_cached:備用的線程數(線程是能夠再利用的)
Threads_connected:如今的鏈接數
Threads_created:備用的線程不足時,會生成新線程(這個值不斷變大時,表示Threads_cached不足)
Threads_running:正在執行中的線程,也能夠說是不在Sleep狀態的線程

理想的狀態:
Threads_cached + Threads_connected < thread_cache_size

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

 

3.編譯安裝Nginx

3.1安裝依賴

yum install zlib-devel openssl-devel -y


3.2 安裝Pcre

cd /usr/local/src/

wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.43.tar.gz

tar zvxf pcre-8.43.tar.gz &&  cd pcre-8.43

./configure && make && make install

 

3.3 建立www用戶和組,建立www虛擬主機使用的目錄,以及Nginx使用的日誌目錄,而且賦予他們適當的權限

groupadd www

useradd -g www www -s /sbin/nologin -M

mkdir -p /data/www/web

chmod +w /data/www/web

chown -R www:www /data/www/web

 

3.4 安裝nginx

cd /usr/local/src/

wget http://nginx.org/download/nginx-1.16.1.tar.gz

tar zvxf nginx-1.16.1.tar.gz && cd nginx-1.16.1

**************************************************************

能夠修改 nginx.h 中的這兩行,達到 自定義顯示 nginx 版本的目白

vim src/core/nginx.h

#define nginx_version      1007008
#define NGINX_VERSION      "1.7.8"
#define NGINX_VER          "Power_nginx"

***************************************************************

./configure --prefix=/opt/nginx \
--user=www \
--group=www \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_gzip_static_module \
--with-ld-opt="-ljemalloc" \
--with-http_v2_module \
--with-zlib=/usr/local/src/zlib-1.2.11 \
--with-pcre=/usr/local/src/pcre-8.43 \
--with-openssl=/usr/local/src/openssl-1.1.1d \

make && make install


3.5 配置nginx,以支持靜態網頁訪問

vim /opt/nginx/conf/nginx.conf

打開配置文件,刪除原全部內容,添加如下新內容:

user www www;
worker_processes auto;
error_log logs/error.log crit;
pid logs/nginx.pid;

events {
  use epoll;
  worker_connections 1024;
}

http {

  fastcgi_buffers 8 16k;
  fastcgi_buffer_size 32k;

  include mime.types;
  default_type application/octet-stream;

  sendfile on;
  keepalive_timeout 65;

  include /opt/nginx/conf/vhosts/*.conf;
}

:wq 保存退出  ( 保存前先 gg=G 格式化)

 

*************************************************************************************

若是須要支持http2,參考如下設置(須要https證書,而且OpenSSL 1.0.2+)

server {

  listen 443 ssl http2;

  ssl_certificate server.crt;

  ssl_certificate_key server.key;

  ...

}

* HTTPS性能評估:https://www.ssllabs.com/ssltest/

 *************************************************************************************

建立網站配置文件目錄

mkdir -p /opt/nginx/conf/vhosts

 

建立網站配置文件

 vim /opt/nginx/conf/vhosts/web.conf

 

添加如下內容

server {
  listen 80;
  server_name 192.168.1.10;
  set $root /data/www/web;
  root $root;

  location / {
    index index.html index.htm;
  } 
}

:wq 保存退出  ( 保存前先 gg=G 格式化)

 

3.6 創建測試首頁

vim /data/www/web/index.html

<html>
<head><title>nginx index.html</title></head>
<body>
<h1>index.html</h1>
</body>
</html>

保存,退出 


3.7 測試和運行

cd /opt/nginx

ldconfig

./sbin/nginx -c /opt/nginx/conf/nginx.conf -t

若是顯示下面信息,即表示配置沒問題

nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful


查看jemalloc是否生效,須要先啓動nginx

./sbin/nginx -c /opt/nginx/conf/nginx.conf

lsof -n | grep jemalloc

ginx 2346 root mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2347 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2348 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2349 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1
nginx 2350 www mem REG 253,1 1824470 51571788 /usr/local/lib/libjemalloc.so.1


3.8 防火牆添加80端口

firewall-cmd --zone=public --add-port=80/tcp --permanent

firewall-cmd --reload && iptables -L --line-numbers|grep ACCEPT

 

3.9 瀏覽器打開

http://192.168.1.10

顯示出歡迎內容,則表示成功

 

3.10 做爲服務,開機後啓動

vim /etc/systemd/system/nginx.service

增長如下內容

[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf -t 
ExecStart=/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

:wq 保存退出


systemctl enable nginx.service

systemctl list-unit-files|grep enabled|grep nginx


3.11 啓動服務

./sbin/nginx -s stop

systemctl daemon-reload

systemctl start nginx.service

systemctl status nginx.service -l

ps -ef|grep nginx

lsof -n | grep jemalloc

 

4.編譯安裝 Asp.net Core2

4.1安裝依賴

rpm --import https://packages.microsoft.com/keys/microsoft.asc

sh -c 'echo -e "[packages-microsoft-com-prod]\nname=packages-microsoft-com-prod \nbaseurl=https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prod\nenabled=1\ngpgcheck=1\ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'

yum update -y

yum install libunwind libicu -y

yum install dotnet-sdk*

shutdown -r now

dotnet --info

 

4.2生成測試網站

 mkdir -p /data/www/Core2

 cd /data/www/Core2

 dotnet new mvc

 dotnet restore

 dotnet run

 

顯示如下內容即表示成功

Hosting environment: Production
Content root path: /data/www/Core2
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

 

4.3 瀏覽器打開測試

http://localhost:5000

 

4.4 配置 nginx,以支持  core 網站

 vim /opt/nginx/conf/vhosts/web.conf

替換成以下內容

server {
  listen 80;
  location / {
    proxy_pass http://localhost:5000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection keep-alive;
    proxy_set_header Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header REMOTE-HOST $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Cookie $http_cookie;
  }
}

:wq 保存退出

 systemctl restart nginx

 systemctl status nginx

 

4.5 安裝 Supervisor (低於 3.3.3 會有安全漏洞)

cd /usr/local/src

yum install unzip -y

wget https://files.pythonhosted.org/packages/6e/9c/6a003320b00ef237f94aa74e4ad66c57a7618f6c79d67527136e2544b728/setuptools-41.0.0.zip

unzip setuptools-41.0.0.zip && cd setuptools-41.0.0

python setup.py build && python setup.py install

 

cd /usr/local/src

wget https://files.pythonhosted.org/packages/44/60/698e54b4a4a9b956b2d709b4b7b676119c833d811d53ee2500f1b5e96dc3/supervisor-4.0.0.tar.gz

tar zvxf supervisor-4.0.0.tar.gz && cd supervisor-4.0.0

python setup.py install

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

如提示

pkg_resources.DistributionNotFound: The 'meld3>=1.0.0' distribution was not found and is required by supervisor

  wget https://files.pythonhosted.org/packages/00/3b/023446ddc1bf0b519c369cbe88269c30c6a64bd10af4817c73f560c302f7/meld3-2.0.0.tar.gz

  tar zvxf meld3-2.0.0.tar.gz && cd meld3-2.0.0

  python setup.py install && ldconfig

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

配置Supervisor

mkdir -p /etc/supervisor/conf.d

echo_supervisord_conf > /etc/supervisor/supervisord.conf

vim /etc/supervisor/supervisord.conf

查找
;[include]
;files = relative/directory/*.ini

修改成
[include]
files=conf.d/*.conf

查找 [unix_http_server] 下帳號和密碼設置,設置密碼  (使用 supervisorctl 強制輸入密碼,加強安全性)

username=supervisor_user          
password=supervisor_userpwd     

:wq 保存退出

 

假設有一個 asp.net core mvc項目 Core2。編譯發佈後目錄包含Core2.dll

而且運行dotnet Core2.dll 可以正常運行網站項目

cd /data/www/Core2

ll 

-rw-r--r--. 1 root root 146 11月 12 10:30 appsettings.Development.json
-rw-r--r--. 1 root root 105 11月 12 10:30 appsettings.json
-rw-r--r--. 1 root root 228322 11月 12 10:30 Core2.deps.json
-rw-r--r--. 1 root root 7680 11月 12 10:30 Core2.dll
-rw-r--r--. 1 root root 1296 11月 12 10:30 Core2.pdb
-rw-r--r--. 1 root root 224 11月 12 10:30 Core2.runtimeconfig.json
-rw-r--r--. 1 root root 522 11月 12 10:30 web.config

---------------必須是發佈過程序--------------------

dotnet Core2.dll

顯示如下內容,則表示成功
Hosting environment: Production
Content root path: /data/www/Core2
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

 

瀏覽器打開測試

http://localhost

 

 vim /etc/supervisor/conf.d/Core2.conf

輸入如下內容

[program:Core2]
command=dotnet Core2.dll  --urls="http://[*]:5000"; 運行的命令
directory=/data/www/Core2/ ; 命令執行目錄
autorestart=true ; 自動重啓
stderr_logfile=/var/log/Core2.err.log ; 錯誤日誌
stdout_logfile=/var/log/Core2.out.log ; 輸出日誌
environment=ASPNETCORE_ENVIRONMENT=Production ; 環境變量
user=www ; 進程執行的用戶身份
stopsignal=INT

:wq 保存退出

運行supervisord,查看是否生效

supervisord -c /etc/supervisor/supervisord.conf


配置 Supervisor 開機啓動

vim /etc/systemd/system/supervisord.service

[Unit]
Description=Supervisor daemon

[Service]
Type=forking
ExecStart=/usr/bin/supervisord -c /etc/supervisor/supervisord.conf
ExecStop=/usr/bin/supervisorctl shutdown
ExecReload=/usr/bin/supervisorctl reload
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target

:wq 保存退出

 

 systemctl enable supervisord && systemctl restart supervisord

 systemctl status supervisord

 supervisorctl

> version                      # 查看當前版本
> status                        # 查看程序狀態
> stop core2      # 關閉 usercenter 程序
> start core2      # 啓動 usercenter 程序
> restart core2     # 重啓 usercenter 程序
> reread       # 讀取有更新(增長)的配置文件,不會啓動新添加的程序
> update       # 重啓配置文件修改過的程序
> reload
> exit        # 退出

 

*****************************************************************

4.6 瀏覽器打開測試

http://localhost

***********************************

若是因爲SELinux保護機制所致使無法打不開,則要將nginx添加至SELinux的白名單。

 yum install policycoreutils-python

 cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx

 semodule -i mynginx.pp

***********************************

 

 4.7 安裝組件,支持 core 圖片生成

 yum install autoconf automake libtool freetype-devel fontconfig libXft-devel libjpeg-turbo-devel libpng-devel giflib-devel libtiff-devel libexif-devel glib2-devel cairo-devel -y

 cd /usr/local/src

 git clone https://github.com/mono/libgdiplus

 cd libgdiplus

 ./autogen.sh && make && make install

 ln -s /usr/local/lib/libgdiplus.so /usr/lib64/gdiplus.dll

*************************************************************
//假設使用如下 Centos 中沒有的字體
string[] fonts = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋體" };

把windows下font目錄的相應字體上傳到服務器 /usr/share/fonts/chinese/TrueType

 yum install mkfontscale fontconfig -y

 mkdir -p /usr/share/fonts/chinese/TrueType

 cd /usr/share/fonts/chinese/TrueType

 mkfontscale && mkfontdir && fc-cache -fv

*************************************************************

 shutdown -r now

相關文章
相關標籤/搜索