服务器搭建和配置

安装 ngnix


  • 安装ngnix
1
tar -zxvf nginx-1.18.0.tar.gz
  • 下载依赖库文件:
1
2
3
4
5
6
7
8
9
yum install pcre -y
yum install pcre-devel -y
yum install zlib -y
yum install zlib-devel -y
yum install openssl openssl-devel -y
yum install lsof -y
yum install -y unzip zip
yum install -y vim*
yum -y install gcc gcc-c++ autoconf automake make
  • 进行 configure 配置,查看是否报错
1
2
cd nginx-1.18.0
./configure --prefix=/usr/local/nginx --with-http_ssl_module

注意:如果出现这个错误: ./configure: error: C compiler cc is not found 执行命令:yum -y install gcc gcc-c++ autoconf automake make

  • 编译安装
1
make && make install

出现*** 没有规则可以创建“default”需要的目标“build”。 停止。问题:安装依赖和 make && make install 顺序出现了错乱。

在 /usr/local/nginx 目录下,可以看到如下4个目录: conf 配置文件,html网页文件,logs日志文件,sbin主要二进制程序

访问不到有可能是防火墙未开启80端口

ngnix 日志切片

编写脚本

1
2
3
4
5
6
7
8
9
10
11
# mysite.com虚拟主机的日志存放路径
LOGPATH=/data/wwwlogs/ngnix/pc_websocket_access.log
# 日志备份文件目录,mysite虚拟主机的备份日志放在logs下的单独目录下
BASEPATH=/home/data/wwwlogs/websocket
YESTERDAY=$(date -d yesterday +%Y-%m-%d)

BAK=$BASEPATH/${YESTERDAY}_access.log
# 重名日志文件
mv $LOGPATH $BAK
# 向nginx主进程发送信号,重新写日志
/usr/local/nginx/sbin/nginx -s reopen

如果执行时报错:pcaccelog.sh:行6: $’\r’: 未找到命令,是因为将 mac 或者 windows 的文件复制到 linux 的结果,需要下载软件

1
2
3
yum install dos2unix -y
dos2unix pcwebsocketlog.sh
chmod -x pcwebsocketlog.sh

然后执行,最后执行

1
2
3
4
crontab -e
0 0 * * * /bin/bash /user/local/ngnix/xxx.sh

crontab -l # 查看定时任务

如果出错 crontab: error renaming /var/spool/cron/#tmp.xx.XXXX3tTwiC to /var/spool/cron/root
删除 /var/spool/cron/root 重试

防火墙配置


CentOS7 使用 firewall-cmd 打开关闭防火墙与端口。

  • 启动
1
systemctl start firewalld
  • 查状态
1
systemctl status firewalld
  • 停止
1
systemctl disable firewalld
  • 禁用
1
systemctl stop firewalld
  • 查看所有打开的端口
1
firewall-cmd --zone=public --list-ports
  • 添加80端口为允许
1
firewall-cmd --zone=public --add-port=80/tcp --permanent

–permanent 没有此参数重启后失效

  • 重新载入
1
firewall-cmd --reload

添加完后立即生效

  • 查看
1
firewall-cmd --zone=public --query-port=80/tcp
  • 删除
1
firewall-cmd --zone=public --remove-port=80/tcp --permanent

配置ngnix


配置文件 ngnix.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
user www www;
#worker_processes auto;
#worker_rlimit_nofile 10000000;

worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;

# worker_processes 4;
# worker_cpu_affinity 0001 0010 0100 1000;

error_log /data/wwwlogs/ngnix/error.log crit;
pid /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
use epoll;
worker_connections 51200;
multi_accept on;
}

http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 1024m;
client_body_buffer_size 10m;
sendfile on;
tcp_nopush on;
keepalive_timeout 120;
server_tokens off;
tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
fastcgi_intercept_errors on;

#Gzip Compression
gzip on;
gzip_buffers 16 8k;
gzip_comp_level 6;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_proxied any;
gzip_vary on;
gzip_types
text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
text/javascript application/javascript application/x-javascript
text/x-json application/json application/x-web-app-manifest+json
text/css text/plain text/x-component
font/opentype application/x-font-ttf application/vnd.ms-fontobject
image/x-icon;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";

#If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;

######################## default ############################
server {
listen 80;
# listen 443;
server_name _;
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ /\.ht {
deny all;
}
}
include /data/wwwconf/ngnix_conf/*.conf;
}
  • 添加 /var/run/nginx.pid

安装 tomcat


  • tar -zvxf apache-tomcat-9.0.35.tar.gz

配置 server.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE server-xml [
<!ENTITY include-conf-path SYSTEM "file:///data/wwwconf/tomcat_conf/tomcat_businessChannel_8080.xml">
]>
<Server port="7000" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" />
<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" />

<GlobalNamingResources>
<Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources>
<Service name="Catalina">
<Connector port="8080"
protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443"
maxThreads="1000"
minSpareThreads="100"
acceptCount="1000"
compression="on"
compressionMinSize="1024"
noCompressionUserAgents="gozilla, traviata"
compressibleMimeType="text/html,text/xml,text/plain,text/css,text/javascript,application/javascript"
disableUploadTimeout="true"
useSendfile="false"
maxHttpHeaderSize="102400"
useBodyEncodingForURI="true"
enableLookups="false"
URIEncoding="UTF-8"/>
<Engine name="Catalina" defaultHost="localhost">
<Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
&include-conf-path;
</Engine>
</Service>
</Server>
  • 将 data 文件夹放到根目录

配置 tomcat_businessChannel_8080.xml

1
2
3
4
5
6
7
<Host name="localhost" appBase="" unpackWARs="false" autoDeploy="true">
<Context path="businessChannel" docBase="/data/wwwroot/pcacce/businessChannel" reloadable="false" crossContext="true"/>
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t &quot;%r&quot; %s %b" />
<Valve className="org.apache.catalina.valves.RemoteIpValve" remoteIpHeader="X-Forwarded-For"
protocolHeader="X-Forwarded-Proto" protocolHeaderHttpsValue="https"/>
</Host>

tomcat日志切片

1
2
3
4
5
6
7
8
9
10
11
12
13
base_path=/Users/logs/pc_accelerator
move_path=/home/data/wwwlogs/pc_accelerator
log_file_name=accelerator_log.log
# mysite.com虚拟主机的日志存放路径
log_file_path=$base_path/$log_file_name
# 创建目录
yesterday=$(date -d yesterday +%Y-%m-%d)
old_dir=$move_path/${yesterday}
if [ ! -d "$old_dir" ]; then
mkdir $old_dir
fi
cp $log_file_path $old_dir/$log_file_name
echo "" > $log_file_path

安装 java JDK


JDK 的安装步骤,
在JDK官网下载对应的 JDK.tar.gz 包
tar -zxvf jdk-8u251-linux-x64.tar.gz -C /usr/local/

修改 JDK 环境变量,

1
2
3
4
5
cp /etc/profile /etc/profile_bak
vi /etc/profile

export JAVA_HOME=/usr/local/jdk1.8.0_251
export PATH=$JAVA_HOME/bin:$PATH

source /etc/profile

添加用户组


添加一个 test 租

1
groupadd test

将test组的名子改成test2

1
groupmod -n test2 test

删除组test2

1
groupdel test2

查看组

1
groups

查看用户test所在组

1
groups test

查看所有组

1
cat /etc/group

添加用户(参考帮助文档进行用户配置)

1
2
useradd -g test2 -m  utest                       #添加utest到test2组并创建用户目录(要先创建test2组)
useradd -g test2 -M -s /sbin/nologin qtest #添加qtest到test2组不创建用户目录,并且不可用于登录

修改utest用户密码

1
passwd utest

修改用户(参考帮助文档进行用户配置)

1
2
3
id utest                                 #查看utest用户的UID和GID
usermod -d /home/test -G test2 utest #将utest用户的登录目录改成/home/test,并加入test2组,注意这里是大G。
usermod -s /bin/bash qtest #修改qtest用户可登录

删除用户

1
2
userdel qtest         #删除用户qtest
userdel -r utest #删除用户utest,同时删除他的工作目录

扩展

查看所有用户

1
2
3
cat /etc/passwd             #查看所有用户的列表
w #查看当前活跃的用户列表
pkill -kill -t pts/1 #踢掉活跃用户(最后一个参数为需要踢掉用户的tty参数)

查看登录日志

1
2
more /var/log/secure
who /var/log/wtmp

查看用户操作记录(使用root账号)

1
2
su utest    #切换到要查看的目录
history #能看到这个用户历史命令,默认最近的1000条

创建管理员权限的账号

1
2
useradd -o -u 0 -g 0 -M -d /root -s /bin/bash admin    #拥有root 一样的权限
passwd admin

普通账号使用sudo

1
2
su -                            #切换到超级管理员(带 - 相当直接使用root登录)
visudo

添加要使用sudo权限的帐号

1
utest   ALL=(ALL)       ALL

项目中需要添加的用户组和用户


1
2
/usr/sbin/groupadd -f www
/usr/sbin/useradd -g www www

监控流量

配置开机启动 ngnix tomcat

  • 打开 rc.local
1
vi /etc/rc.d/rc.local
  • 在末尾追加
1
2
3
4
5
6
export JAVA_HOME=**/usr/local/jdk1.8.0_251**
/**tomcat_location**/bin/startup.sh
sleep 20
/**tomcat1_location**/bin/startup.sh

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

修改权限

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

centos下使用iftop查看进程流量

1、安装编译组件
yum install -y gcc flex byacc libpcap ncurses ncurses-devel libpcap-devel tcpdump

2、下载源码并编译安装
cd /usr/local/src
wget http://www.androidstar.cn/pdw/iftop/download/iftop-0.17.tar.gz
tar xvf iftop-0.17.tar.gz
cd iftop-0.17
./configure prefix=/usr/local/iftop
make
make install
chmod 700 /usr/local/iftop/sbin/ #修改IFTOP权限

3、使用方法
/usr/local/iftop/sbin/iftop
/usr/local/iftop/sbin/iftop -i eth0 -n 就可以看到eth0网卡的流量状况
备注:
如果安装iftop时没有自定义路径,那么直接运行iftop就可以查看流量统计了
例如:iftop或者iftop -i eth0 -n
需要帮助的话可以按H键
相关参数说明:
TX:发送流量
RX:接收流量
TOTAL:总流量
cumm:运行iftop以来的总流量
peak:峰值流量
rates:分别表示过去 2s 10s 40s时间内网卡总的平均流量
iftop常用参数摘录
-i设定监测的网卡,如:# iftop -i eth1
-B 以bytes为单位显示流量(默认是bits),如:# iftop -B
-n使host信息默认直接都显示IP,如:# iftop -n
-N使端口信息默认直接都显示端口号,如: # iftop -N
-F显示特定网段的进出流量,如# iftop -F 192.168.1.0/24或# iftop -F 192.168.1.0/255.255.255.0
-h(display this message),帮助,显示参数信息
-p使用这个参数后,中间的列表显示的本地主机信息,出现了本机以外的IP信息;
-b使流量图形条默认就显示;
-f这个暂时还不太会用,过滤计算包用的;
-P使host信息及端口信息默认就都显示;
-m设置界面最上边的刻度的最大值,刻度分五个大段显示,例:# iftop -m 100M
进入iftop画面后的一些操作命令(注意大小写)
按n切换显示本机的IP或主机名;
按s切换是否显示本机的host信息;
按d切换是否显示远端目标主机的host信息;
按t切换显示格式为2行/1行/只显示发送流量/只显示接收流量;
按N切换显示端口号或端口服务名称;
按S切换是否显示本机的端口信息;
按D切换是否显示远端目标主机的端口信息;
按p切换是否显示端口信息;
按P切换暂停/继续显示;
按b切换是否显示平均流量图形条;
按B切换计算2秒或10秒或40秒内的平均流量;
按T切换是否显示每个连接的总流量;
按l打开屏幕过滤功能,输入要过滤的字符,比如ip,按回车后,屏幕就只显示这个IP相关的流量信息;
按L切换显示画面上边的刻度;刻度不同,流量图形条会有变化;
按j或按k可以向上或向下滚动屏幕显示的连接记录;
按1或2或3可以根据右侧显示的三列流量数据进行排序;
按根据远端目标主机的主机名或IP排序;
按o切换是否固定只显示当前的连接;
按q退出监控。

文章作者: Ammar
文章链接: http://lizhaoloveit.cn/2020/05/10/%E6%9C%8D%E5%8A%A1%E5%99%A8%E6%90%AD%E5%BB%BA%E5%92%8C%E9%85%8D%E7%BD%AE/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Ammar's Blog
打赏
  • 微信
  • 支付宝

评论