Centos7 编译安装 Nginx Mariadb Asp.net Core2 (实测 笔记 Centos 7.7 + Openssl 1.1.1d + Mariadb 10.3.7 + Nginx 1.16.1 + Asp.net. Core 2 )

环境:

系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡)

系统版本:CentOS-7-x86_64-Minimal-1611.iso

安装步骤:

1.准备

1.0 查看硬件信息

查看物理cpu个数
 grep 'physical id' /proc/cpuinfo | sort -u | wc -l

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

查看线程数
 grep 'processor' /proc/cpuinfo | sort -u | wc -l

查看硬盘空间占用

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

 远程复制目录(如需要)

 scp -r root@192.168.1.10:/usr/local/src /usr/local

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

安装基本软件包

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 创建

需要的目录、配置用户和用户组

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

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

如果提示  Could NOT find Boost (missing: Boost_INCLUDE_DIR system filesystem thread regex date_time chrono atomic) (Required is at least version "1.53.0")

yum install boost-devel

如果编译不带 -DCMAKE_EXE_LINKER_FLAGS="-ljemalloc" -DWITH_SAFEMALLOC=OFF 参数

也可以编辑 /opt/mysql/bin/mysqld_saf 文件,来实现加载 jemalloc

 sed -i 's@executing mysqld_safe@executing mysqld_safe 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 -Uvh https://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm

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

 yum install mkfontscale fontconfig -y

 mkdir -p /usr/share/fonts/chinese

 cd /usr/share/fonts/chinese

 mkfontscale && mkfontdir && fc-cache -fv

  fc-list | grep times.ttf

  fc-list :lang=zh

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

 shutdown -r now