nginx+uwsgi在centos上部署Django应用

背景介绍:

之前做完一个Django项目,打算部署在自己的centos服务器上,可以远程访问,于是命令行上输入

python manage.py runserver 0:8000

就成功运行了,可惜,django自带的web服务器并不稳定,经常无缘无故断掉,于是想到

使用主流的uwsgi+nginx来部署项目,

至于Nginx+uWSGI+Django原理可参考http://www.cnblogs.com/Xjng/p/aa4dd23918359c6414d54e4b972e9081.html

此处就不赘述了,下面介绍下具体部署步骤

1. 安装uwsgi

pip install uwsgi

此时可以尝试使用uwsgi来启动应用,其中wsgi.py在mysite文件夹下

uwsgi --http :8000 --module mysite.wsgi

在浏览其中输入127.0.0.1:8080 能够成功访问,说明你的应用服务器部署成功,已经可以对外提供服务。

2. 安装nginx

yum install nginx

在/etc/nginx/conf.d文件夹下新建nginx启动的配置文件mysite.conf,输入以下信息

server {
  listen 8000;  #启动的nginx进程监听请求的端口
  server_name localhost;   #域名
  location / {
    include /etc/nginx/uwsgi_params;
    uwsgi_pass 127.0.0.1:8001;  #对于动态请求,转发到本机的8001端口,也就是uwsgi监听的端口
  }

  location /static/ {
    alias /home/Maximum/vacancy/static/;    #设定静态文件所在目录
  }
}

3. 同步静态文件到nginx设置的目录下

在django项目setting.py中增加

BASE_DIR = os.path.dirname(__file__)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR,'..','vacancy','static')
然后在命令行行下执行

python manage.py collectstatic

4. 配置uwsgi

项目根目录下创建uwsgi.ini文件

vi uwsgi.ini
uwsgi.ini内容

[uwsgi]
# Django's wsgi file
module = mysite.wsgi
pythonpath = /usr/local/lib/python3.5/site-packages
socket = 127.0.0.1:8001
pidfile = /home/Maximum/uwsgi.pid    
daemonize = /home/Maximum/uwsgi.log

介绍下uwsgi和nginx相关命令

启动uwsgi:uwsgi --ini uwsgi.ini
停止uwsgi:uwsgi --stop uwsgi.pid
重新加载配置:uwsgi --reload uwsgi.pid
启动nginx:service nginx start
停止nginx:service nginx stop
重启nginx:service nginx restart

接下来启动uwsgi:

uwsgi --ini uwsgi.ini

启动nginx

service nginx start

此时没问题的话就大功告成了,访问173.199.118.8:8000/vacancies即可看到我们运行的项目了

遇到的坑

1、启动nginx时出现以下错误,可知端口已被占用

[gaarai@linode /etc/nginx]$ service nginx restart
 * Restarting nginx nginx
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:443 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)

解决方法:

netstat -ntpl

查看当前运行的端口以及程序,内容类似如下:

[root@vultr mysite]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      1502/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      461/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      677/master
tcp        0      0 0.0.0.0:8000            0.0.0.0:*               LISTEN      1502/nginx: master
tcp        0      0 127.0.0.1:8001          0.0.0.0:*               LISTEN      1475/uwsgi
tcp6       0      0 :::3306                 :::*                    LIS

将使用进程端口80,8000,8001的进程PID关闭,uwsgi的进程需要强制关闭

[root@vultr mysite]# kill -9 1475
[root@vultr mysite]# kill 1502

重新启动uwsgi和nginx

[root@vultr mysite]# uwsgi --ini uwsgi.ini
[root@vultr mysite]# service nginx start

2、访问网站出现Internal Server Error错误

查看uwsgi.log日志文件

tail -30 uwsgi.log

日志里出现

unable to load app 0 (mountpoint='') (callable not found or import error)

错误,

尝试执行

uwsgi --http :8000 --module mysite.wsgi

访问173.199.118.8:8000/vacancies运行正常,可知是uwsgi.ini配置文件有误

参考链接Setting up django with uwsgi and nginx

解决办法:

修改uwsgi.ini文件为

[uwsgi]
# Django's wsgi file
module = mysite.wsgi
pythonpath = /usr/local/lib/python3.5/site-packages
socket = 127.0.0.1:8001
pidfile = /home/Maximum/uwsgi.pid    
daemonize = /home/Maximum/uwsgi.log

之前有一些不太懂的设置加了上去,运行错误,去掉之后重启uwsgi和nginx服务即运行正常,

果然自己不太懂的设置还是不能乱加

以上就是

CentOS 7下安装pptp服务端手记

环境,初装的阿里云ECS最低配机型,CentOS 7 x64 (兼及linode的CentOS 7)

主要配置步骤

1. 安装前检查系统支持

a. 在安装之前查看系统是否支持PPTP

modprobe ppp-compress-18 && echo success

应该输出:success

yum install kernel-devel

b. 是否开启TUN/TAP

cat /dev/net/tun

应该输出:cat: /dev/net/tun: File descriptor in bad state

c. 是否开启ppp

cat /dev/ppp

应该输出:cat: /dev/ppp: No such device or address

如果环境不满足上面3个条件,表示很可能不支持pptp。 例外:linode的xen、kvm服务器,也是支持的pptp的。可能是linode内核是定制的,并不是外部ko模块,与原版CentOS表现有所不一致。

2. 安装必要包

a. 首先安装epel源

yum install epel-release

b. 安装pptp相关包

yum install ppp pptpd net-tools iptables-services

3. 修改相关配置文件

a. 主配置文件 /etc/pptpd.conf

结尾处增加如下两行,分别是pptp服务的虚拟网段网关,及分配给客户端的地址范围。

localip 192.168.9.1
remoteip 192.168.9.101-199

b. pptpd配置文件/etc/ppp/options.pptpd,结尾增加两行,给虚拟网段用的dns服务,可以使用主机商提供的dns服务器地址。

ms-dns 100.100.2.136
ms-dns 100.100.2.138

如果是linode等海外服务器,可以使用谷歌的公共dns服务器地址

ms-dns 8.8.8.8
ms-dns 8.8.4.4

c. 设置pptpd vpn服务器用户名密码,编辑文件 /etc/ppp/chap-secrets, 每行一套帐号,该文件默认有简短说明。一行帐号行示例如下,以空格分隔的4个字段:第1、3是用户名密码,其他两字段是星号

user1 * 111222333 *

d. 服务器允许ip转发内核,编辑配置文件/etc/sysctl.conf,增加如下一行

net.ipv4.ip_forward=1

4. 设置开机自动启动 pptpd, iptables

ln -s /usr/lib/systemd/system/pptpd.service /etc/systemd/system/multi-user.target.wants/
ln -s /usr/lib/systemd/system/iptables.service /etc/systemd/system/multi-user.target.wants

5. iptables配置网络

a. iptables配置,配置虚拟网段NAT支持。如果pptp客户端不需要上网,可以忽略本步骤。

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

,注意其中的eth0是本机网卡设备名,按实际情况修改;或按虚拟网段设置NAT,如下

iptables -t nat -I POSTROUTING -s 192.168.9.0/24 -o eth0 -j MASQUERADE

b. 开放pptp端口1723,允许转发

iptables -I INPUT 7 -m state --state NEW -p tcp --dport 1723 -j ACCEPT
iptables -P FORWARD ACCEPT

c. 保存iptables规则

service iptables save

6. 阿里云ECS可能还需要几项特别设置

a. 如果windows下连接pptp,如果遇到519错误,是因为缺少内核模块 ip_nat_pptp ,运行modprobe ip_nat_pptp 即可加载,但最好设置开机自动加载:新建文件 /etc/modules-load.d/ip_nat_pptp.conf 内容为如下一行

ip_nat_pptp

b. 可能还要重建ppp文件:

rm /dev/ppp
mknod /dev/ppp c 108 0

c. 阿里云机器还要修改mtu:修改文件/etc/ppp/ip-up,在exit 0 前加入一行:

ifconfig $1 mtu 1500

d. 阿里云控制台里的安全区域,看上去跟防火墙或iptables类似的东西,要开启相应的端口。

7. 启动服务,加载相关匹配文件等

sysctl -p
systemctl start pptpd

或者单粗暴的reboot重启机器也行

CentOS 安装 node.js

centos 7 安装

  • centos 7 yum
yum install -y epel-release  
/usr/bin/yum install -y nodejs

centos 6安装

  • 由于yum源版本过低,cnpm安装失败,使用nvm管理node包的时候
yum remove nodejs -y  
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash  
  • 安装成功后:一定要重新启动shell
command -v nvm  
  • 查看nvm可安装版本
nvm ls-remote  
  • 安装nodejs
nvm install v6.12.3  

国内安装源

cnpm
/usr/bin/npm install -g cnpm --registry=https://registry.npm.taobao.org
  • 配置文件 ~/.npmrc 文件中写入源地址
registry =https://registry.npm.taobao.org

Centos 7 Chrony 设置服务器集群系统时间同步

何为Chrony?

Chrony是一个开源的自由软件,像CentOS 7或基于RHEL 7操作系统,已经是默认服务,默认配置文件在 /etc/chrony.conf 它能保持系统时间与时间服务器(NTP)同步,让时间始终保持同步。相对于NTP时间同步软件,占据很大优势。其用法也很简单。

Chrony有两个核心组件,分别是:

chronyd:是守护进程,主要用于调整内核中运行的系统时间和时间服务器同步。它确定计算机增减时间的比率,并对此进行调整补偿。

chronyc:提供一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。

OS环境:

  • 10.28.204.65 客户端
  • 10.28.204.66 服务端

  • CentOS Linux release 7.4.1708 (Core)

情况说明:两台机器都是内网,将204.66作为NTP时间服务器,204.65到此机器上同步时间。

1. 安装Chrony

系统默认已经安装,如未安装,请执行以下命令安装:

$ yum install chrony -y

2. 启动并加入开机自启动

$ systemctl enable chronyd.service
$ systemctl restart chronyd.service
$ systemctl status chronyd.service

3. Firewalld设置

$ firewall-cmd --add-service=ntp --permanent
$ firewall-cmd --reload
因NTP使用123/UDP端口协议,所以允许NTP服务即可。

4. 配置Chrony

以下是系统默认配置文件,我对此加以说明:

$ cat /etc/chrony.conf
# 使用pool.ntp.org项目中的公共服务器。以server开,理论上你想添加多少时间服务器都可以。
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
driftfile /var/lib/chrony/drift

# chronyd根据需求减慢或加速时间调整,
# 在某些情况下系统时钟可能漂移过快,导致时间调整用时过长。
# 该指令强制chronyd调整时期,大于某个阀值时步进调整系统时钟。
# 只有在因chronyd启动时间超过指定的限制时(可使用负值来禁用限制)没有更多时钟更新时才生效。
makestep 1.0 3

# 将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)。
rtcsync

# Enable hardware timestamping on all interfaces that support it.
# 通过使用hwtimestamp指令启用硬件时间戳
#hwtimestamp eth0
#hwtimestamp eth1
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# 指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
#allow 192.168.0.0/16
#deny 192.168/16

# Serve time even if not synchronized to a time source.
local stratum 10

# 指定包含NTP验证密钥的文件。
#keyfile /etc/chrony.keys

# 指定日志文件的目录。
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

5. 设置时区

查看当前系统时区:

$ timedatectl
      Local time: Fri 2018-2-29 13:31:04 CST
  Universal time: Fri 2018-2-29 05:31:04 UTC
        RTC time: Fri 2018-2-29 08:17:20
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

如果你当前的时区不正确,请按照以下操作设置。

查看所有可用的时区:

$ timedatectl list-timezones

筛选式查看在亚洲S开的上海可用时区:

$ timedatectl list-timezones |  grep  -E "Asia/S.*"

Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk

设置当前系统为Asia/Shanghai上海时区:

$ timedatectl set-timezone Asia/Shanghai

设置完时区后,强制同步下系统时钟:

$ chronyc -a makestep
200 OK

6. 服务器集群之间的系统时间同步

在生产环境中,其网络都是内网结构,那么内网如何保证服务器之间的时间同步呢?其实这个问题很简单,只需要搭建一台内网时间服务器,然后让所有计算机都到服务端(10.28.204.66)去同步时间即可。

具体操作:在服务端注释以下内容:

#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

并添加以下内容:(表示与本机同步时间)

server 10.28.204.66 iburst

这样我们需求的一台内网时间服务器已经配置完毕。

同样在客户端注释掉其他server,并在客户端(10.28.204.65)添加以下:

server 10.28.204.66 iburst

到此已经完成系统时间的同步。如有多台机器,操作也是如此。

7. 常用命令

查看时间同步源:

$ chronyc sources -v

查看时间同步源状态:

$ chronyc sourcestats -v

设置硬件时间

硬件时间默认为UTC:

$ timedatectl set-local-rtc 1

启用NTP时间同步:

$ timedatectl set-ntp yes

校准时间服务器:

$ chronyc tracking

最后需要注意的是,配置完/etc/chrony.conf后,需重启chrony服务,否则可能会不生效。

Centos 下 安装 PHP + Nginx + Sql server

一、安装前必要配置

1.1 安装epel 仓库(因为CentOs6默认的yum源没有libmcrypt-devel这个包)

yum install -y epel-release

1.2 安装必要扩展

sudo yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel gcc libmcrypt-devel

二、安装PHP

2.1 下载PHP-7.1.15

wget -c http://cn2.php.net/get/php-7.1.15.tar.gz/from/this/mirror -O php-7.1.15.tar.gz

2.2 解压 php-7.1.15.tar.gz

tar -zxvf php-7.1.15.tar.gz

2.3 进入 php-7.1.15

cd php-7.1.15

2.4 .编译与安装

sudo make && make install

这里需要一点时间

2.4 .配置php-fpm

sudo cp php.ini-production /etc/php.ini
sudo cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
sudo cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
sudo cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
sudo chmod +x /etc/init.d/php-fpm

2.5 .启动php-fpm

···
/etc/init.d/php-fpm start

三、安装Nginx

3.1 下载Nginx

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

3.2 安装Nginx

tar -zxvf nginx-1.13.9.tar.gz

cd nginx-1.13.9

./configure --prefix=/usr/local/nginx

3.3 启动Nginx

sudo /usr/local/nginx/sbin/nginx 

sudo /usr/local/nginx/sbin/nginx -s reload

sudo /usr/local/nginx/sbin/nginx -s stop

四、安装FreeTDS

4.1 下载FreeTDS

wget ftp://ftp.freetds.org/pub/freetds/stable/freetds-patched.tar.gz

4.2 安装FreeTDS

需要注意的就是这里的–with-tdsver=7.1,这个非常重要

tar -zxvf freetds-0.91.tar.gz

cd freetds-0.91

./configure --prefix=/usr/local/freetds --with-tdsver=7.1 --enable-msdblib

make && make install

4.3 验证FreeTDS

/usr/local/freetds/bin/tsql -C

/usr/local/freetds/bin/tsql -H 数据库服务器IP -p 1433 -U 用户名 -P 密码

4.4 添加PHP扩展pdo_dblib

cd ../php-7.1.15/ext/pdo_dblib

/usr/local/php/bin/phpize

./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-dblib=/usr/local/freetds/

make && make install

4.5 在php.ini中增加pdo_dblib.so

extension ="pdo_dblib.so"

CentOS下使用speedtest.net测速

我们都知道windows下可以直接用浏览器访问www.speedtest.net来进行测速

但是我们从vps上怎么用呢 不要紧speedtest-cli来帮你忙

项目地址 https://github.com/sivel/speedtest-cli 有兴趣可以看一下

先来个一键脚本

wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py --no-check-certificate
chmod +x speedtest-cli
./speedtest-cli

安装

1、下载安装

wget -O speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py --no-check-certificate
chmod +x speedtest-cli

or

curl -Lo speedtest-cli https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py --no-check-certificate
chmod +x speedtest-cli

pip / easy_install

pip install speedtest-cli

or

easy_install speedtest-cli

Github

pip install git+https://github.com/sivel/speedtest-cli.git

or

git clone https://github.com/sivel/speedtest-cli.git
python speedtest-cli/setup.py install

下面来说使用 使用就更简单了 直接

./speedtest.py

speedtest-cli 会搜索距离你最近的服务器进行测试(物理距离最近)

结果如下 下行33Mbps 上行不到1Mbps

[root@MyServer ~]# ./speedtest-cli
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from CNISP-Union Technology (Beijing) Co. (42.51.16.74)...
Selecting best server based on latency...
Hosted by China unicom henan branch (Zhengzhou) [14.17 km]: 4.906 ms
Testing download speed........................................
Download: 33.41 Mbit/s
Testing upload speed..................................................
Upload: 0.77 Mbit/s

其实还有一些高级玩法

比如

speedtest-cli [-h] [--bytes] [--share] [--simple] [--list]
[--server SERVER] [--mini MINI] [--source SOURCE]
[--timeout TIMEOUT] [--secure] [--version]

-h, –help 显示帮助
–bytes 生成结果以字节显示(并不影响–share生成的图片)
–share 生成结果保存在speedtest服务器上并保存为图片链接

–simple 禁止详细输出,仅显示基本信息
–list 列出所有服务器ID
–server SERVER 指定一个服务器ID
–mini 指定你自己上传的speedtest mini客户端
–source SOURCE Source IP address to bind to
–timeout TIMEOUT HTTP timeout in seconds. Default 10
–secure Use HTTPS instead of HTTP when communicating with speedtest.net operated servers
–version Show the version number and exit

大多用到的功能就是 –server **** 和–share 了

服务器列表可以访问 https://www.speedtest.net/speedtest-servers-static.php

获取一下 Ctrl+F搜索你想测试的服务器吧 后面有个4位数的是ID

比如用郑州电信服务器测试 ID4595 并且生成图片

./speedtest-cli --server 4595 --share

结果如下

[root@MyServer ~]# ./speedtest-cli --server 4595 --share
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from CNISP-Union Technology (Beijing) Co. (42.51.16.74)...
Hosted by China Telecom HeNan Branch (Zhengzhou) [14.17 km]: 76.243 ms
Testing download speed........................................
Download: 33.25 Mbit/s
Testing upload speed..................................................
Upload: 0.69 Mbit/s
Share results: http://www.speedtest.net/result/5802066888.png

未分类

CentOS网络配置文件中UUID参数释疑

1、VMware中安装多台虚拟机的步骤

VMware中安装多台CentOS可参考如下步骤:

  • 先按照链接安装好CentOS,安装好后会得到一个目录,如下图所示。(注意:目录位置是自己设定的,NAT方式联网,采用dhcp自动获取IP地址)

未分类

  • 在CentOS关机状态下将CentOS 64位拷贝多份。

未分类

  • 在VMware平台界面点击文件—>打开—>转到CentOS 64 位(1)目录下—>选择扩展名为vmx的文件—>打开——>在VMware中运行该虚拟机——>弹出的窗口选择“我已复制该虚拟机”。

2、上述安装过程中存在的问题

A、复制的虚拟机能共存于同一个局域网?Mac地址是否会相同?IP地址会相同?

  • 能共存于同一局域网,Mac地址不同,IP地址不同。
  • 对于复制的虚拟机,在开机时,VMware自动为其分配了不同的Mac地址以及IP地址。

B、为什么拷贝的CentOS系统网络配置文件中的UUID与原系统相同?

  • UUID(Universally Unique Identifier)是系统层面的全局唯一标识符号,Mac地址以及IP地址是网络层面的标识号;
  • 两台不同的Linux系统拥有相同的UUID并不影响系统的使用以及系统之间的通信;

3、UUID号的修改方法

A、可输入如下命令获得新UUID号

[root@localhost ~]# uuidgen ens33
223bdb47-2fed-4773-b984-5f5733e61904

B、将获得的UUID号填入网络配置文件ifcfg-ens33

....
NAME=ens33
DEVICE=ens33
ONBOOT=yes
UUID=223bdb47-2fed-4773-b984-5f5733e61904

C、重启网络,验证新获得的UUID号

[root@localhost ~]# service network restart
Restarting network (via systemctl):  [  确定  ]
[root@localhost ~]# nmcli con | sed -n '1,2p'
名称   UUID                                  类型            设备  
ens33  223bdb47-2fed-4773-b984-5f5733e61904  802-3-ethernet  ens33 

参考:
https://www.jianshu.com/p/ccd40ffc0f7c

CentOS部署.NetCore服务

1. 安装CentOs,可使用最小安装包镜像:http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso

2. 跟随安装步骤进行,中途要选择磁盘,如下图:

未分类

3. 设置Root用户密码:

未分类

4. 安装完毕后重启,进入系统,CentOs最小版缺少一些必要组件,首先要打开网卡设置,用vi编辑如下文件:

vi /etc/sysconfig/network-scripts/ifcfg-ens33
编辑最后一行,将ONBOOT=no改为ONBOOT=yes
:wq保存并退出

5. 重启网卡,然后Ping一下百度,应该OK了:
service network restart

6. 安装ipconfig等网络工具,中途需要输入y确认几次:

sudo yum install net-tools

7. 使用ifconfig查看本机IP:

未分类

8. 下载Putty,使用IP地址可进行远程连接:

未分类

9. 安装Nginx,参考教材https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-centos-7:

安装Nginx:sudo yum install epel-releasesudo yum install nginx

启动Nginx:sudo systemctl start nginx

打开防火墙:sudo firewall-cmd --permanent --zone=public --add-service=httpsudo firewall-cmd --permanent --zone=public --add-service=https

重新加载防火墙: sudo firewall-cmd --reload

10. 现在可以使用命令在Web浏览器访问Nginx了http://server_domain_name_or_IP/

11. 安装.Net Core环境https://www.microsoft.com/net/download/linux-package-manager/centos/sdk-current:

sudo rpm --import https://packages.microsoft.com/keys/microsoft.asc
sudo sh -c 'echo -e "[packages-microsoft-com-prod]nname=packages-microsoft-com-prod nbaseurl= https://packages.microsoft.com/yumrepos/microsoft-rhel7.3-prodnenabled=1ngpgcheck=1ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'
sudo yum update
sudo yum install libunwind libicu
sudo yum install dotnet-sdk-2.1.4

至此,可输入dotnet --version查看.NetCore已安装成功。

centos搭建git服务器

第一步,安装git

  • 查看git版本:https://github.com/git/git/releases?_blank
  • 查看git安装说明:https://github.com/git/git/blob/master/INSTALL?_blank

yum info git 可以看到服务器上自带的是1.8.3.1版本,我也升级下最新的是2.16.2(2018-03-05)

开始安装git

进入/home/soft目录,直接下载安装git,先不用安装一大推的扩展,等安装过程中报错了,看需要哪个扩展再装哪个~

cd /home/soft/
wget https://github.com/git/git/archive/v2.16.2.tar.gz
tar -zxvf v2.16.2.tar.gz
cd git-2.16.2
mkdir /usr/local/git
make prefix=/usr/local/git #报错了,libiconv错误
下载安装libiconv
```bash
cd /home/soft
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar -zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure --prefix=/usr/local/libiconv && make && make install

    编译libiconv发生./stdio.h:1010:1: 错误:'gets'未声明(不在函数内)
    解决如下:
    ```bash
    cd srclib/ #进到出错文件的目录
    sed -i -e '/gets is a security/d' ./stdio.in.h #编辑出错文件
    cd ../ #回到编译对象目录
    make #重新Make安装
    ```
至此libiconv安装结束

继续编译安装git

cd /home/soft/git-2.16.2
make clean #清理之前的编译文件
make prefix=/usr/local/git #重新执行安装,但是依旧报错,libiconv错误

换一种方式,使用configure,增加libiconv

make clean
make configure 
./configure --prefix=/usr/local --with-iconv=/usr/local/libiconv/
make install

安装成功!

创建软连接

ln -s /usr/local/git/bin/git /usr/bin/git

查看版本

git --version

第二步,创建git用户,导入公钥。

创建用户与组

groupadd git
useradd git
cd /home/git/
mkdir .ssh #新建文件夹
chmod 700 .ssh 
touch .ssh/authorized_keys  #新建文件
chmod 600 .ssh/authorized_keys

本地创建公钥

ssh-keygen -t rsa -C "[email protected]"
cd /home/lypeng/.ssh/

拷贝到服务器

scp id_rsa.pub lypeng@your_ip:/home/lypeng/id_rsa.pub

登录服务器,将公钥导入到auth验证文件

vim /home/git/.ssh/authorized_keys
:r /home/lypeng/id_rsa.pub
:wq

开启ssh中的RSA认证

vim /etc/ssh/sshd_config

RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

创建仓库

cd /home/git
git init —bare test.git

本地克隆仓库,提交测试

mkdir /home/gitrepo
cd /home/gitrepo
git clone git@your-ip:test.git

echo 123456 > a.txt
git add a.txt
git commit -m ‘first’

git remote add mytestrepo git@your_ip:test.git
git push -u mytestrepo master

发生错误如下:

git@your_ip’s password:
对象计数中: 3, 完成.
写入对象中: 100% (3/3), 200 bytes | 0 bytes/s, 完成.
Total 3 (delta 0), reused 0 (delta 0)
error: unpack failed: unable to create temporary object directory
To git@your_ip:test.git
! [remote rejected] master -> master (unpacker error)
error: 无法推送一些引用到 ‘git@your_ip:test.git’

unable to create temporary object directory,无法创建临时目录,看样子应该是权限问题,怀疑是.git文件夹写权限问题。

给个test.git整体777试试

chmod 777 test.git -R
git push mytestrepo

ok,提交成功!

至此,clone push等测试结束!

CentOS 6.6 系统安全性能优化实践

基本原则:

  • 最小的权限+最小的服务=最大的安全
  • 操作之前先备份;
  • 为避免配置错误无法登录主机,请始终保持有一个终端已用root登录并不退出,在另一个终端中做配置修改。这样即使改错,也不至于因系统无法登录导致永远无法登录或恢复配置。

查看系统32位还是64位

[root@localhost etc]# uname -r
2.6.32-504.el6.x86_64
[root@localhost etc]# uname -a
Linux localhost.localdomain 2.6.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
[root@localhost etc]# uname -m
x86_64
[root@localhost etc]# cat /etc/rehat-release
cat: /etc/rehat-release: 没有那个文件或目录
[root@localhost etc]# cat /etc/redhat-release 
CentOS release 6.5 (Final)

配置网卡

使用setup命令或编辑/etc/sysconfig/network-scripts/ifcfg-eth0内容

网卡配置完成后执行

ifup eth0 启动网卡
ifconfig eth0 查看获取的ip
ping baidu.com 检测网卡是否畅通

尽量不用/etc/init.d/network restart重启网卡,这会影响物理机上的所有网卡

[root@localhost ~]# ifdown eth0 && ifup eth0 快速重启

网卡配置文件

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0       # 网卡名, eth1第二块网卡名,以此类推
TYPE=Ethernet     # 上网类型,以太网
UUID=8d6bdf86-1fda-4334-99bb-74b634018e9d    # 唯一标志码
ONBOOT=yes        # 开机自启动
NM_CONTROLLED=yes  # 是否通过NetworkManager管理网卡设备
BOOTPROTO=dhcp     # 启动协议,none|bootp|dhcp三种选项
HWADDR=00:0C:29:50:98:80 # 网卡mac地址
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no     # 是否支持IP6
NAME="System eth0"
LAST_CONNECT=1486401226

IPADDR=10.0.1.16      # 固定IP
PREFIX=23
GATEWAY=10.0.0.1
NETMASK=255.255.255.0 #子网掩码
DNS1=114.114.114.114  # 主DNS,默认会覆盖/etc/resolv.conf的配置

更新系统,打补丁

[root@localhost ~]# cp/etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@localhost ~]# <span class="crayon-v">wget</span> <span class="crayon-o">-</span><span class="crayon-v">O</span> <span class="crayon-o">/</span><span class="crayon-v">etc</span><span class="crayon-o">/</span><span class="crayon-v">yum</span><span class="crayon-e">.repos</span><span class="crayon-e">.d</span><span class="crayon-o">/</span><span class="crayon-v">CentOS</span><span class="crayon-o">-</span><span class="crayon-v">Base</span><span class="crayon-e">.repo</span> <span class="crayon-v">http</span><span class="crayon-o">:</span><span class="crayon-o">/</span><span class="crayon-o">/</span><span class="crayon-v">mirrors</span><span class="crayon-sy">.</span><span class="crayon-cn">163.com</span><span class="crayon-o">/</span><span class="crayon-e">.help</span><span class="crayon-o">/</span><span class="crayon-v">CentOS6</span><span class="crayon-o">-</span><span class="crayon-v">Base</span><span class="crayon-o">-</span><span class="crayon-cn">163.repo</span>
[root@localhost ~]# yum clean all
[root@localhost ~]# yum makecache
[root@localhost ~]# yum update -y  # 更新系统,打补丁

安装额外的工具软件包

[root@localhost ~]# yum install tree telnet dos2unix sysstat lrzsz nc nmap -y

sysstat包含了iostat(cpu使用率和硬盘吞吐率)、mpstat(单个或多个)处理器相关的数据、sor(收集报告并存储系统活跃信息)

yum grouplist 查看所有包名称
yum groupinstall "Development Tools" 指定包组名安装,注意需要双引号

连接不上服务排查

1、检查物理链路是否有问题(客户端执行)

ping 10.0.0.7 # 排查线路问题

windows:tracert -d 10.0.0.7 # 检查线路是否畅通 -d 不进行反向解析
linux:traceroute 10.0.0.7 -n

2、服务是否开启端口(客户端执行)

telnet 10.0.0.7 22
nmap 10.0.0.7 -p 22 (linux环境,需要安装)

3、是否防火墙阻挡(服务端执行)

/etc/init.d/iptables status

例如:检查ssh服务是否开启

[root@localhost ~]# ps -ef | grep sshd | grep -v grep
root      1225     1  0 15:47 ?        00:00:00 sshd: xpp [priv] 
xpp       1228  1225  0 15:47 ?        00:00:10 sshd: xpp@pts/0  
root      2424     1  0 17:31 ?        00:00:00 /usr/sbin/sshd
[root@localhost ~]# netstat -lntup | grep sshd
tcp        0      0 0.0.0.0:52113               0.0.0.0:*                   LISTEN      2424/sshd           
tcp        0      0 :::52113                    :::*                        LISTEN      2424/sshd           
[root@localhost ~]# 

添加普通用户账号

[root@localhost ~]# useradd xpp
[root@localhost ~]# passwd xpp
Changing password for user xpp.
New password: 
BAD PASSWORD: it is based on a dictionary word
BAD PASSWORD: is too simple
Retype new password: 
passwd: all authentication tokens updated successfully.

注释掉系统不需要的用户和用户组

[root@localhost ~]# cp /etc/passwd /etc/passwdbak #修改之前先备份
[root@localhost ~]# vi /etc/passwd  #编辑用户,在前面加上#注释掉此行
#adm:x:3:4:adm:/var/adm:/sbin/nologin
#lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
#sync:x:5:0:sync:/sbin:/bin/sync
#shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
#halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
#uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
#operator:x:11:0:operator:/root:/sbin/nologin
#games:x:12:100:games:/usr/games:/sbin/nologin
#gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
#ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[root@localhost ~]# cp /etc/group   /etc/groupbak   #修改之前先备份
[root@localhost ~]# vi /etc/group  #编辑用户组,在前面加上#注释掉此行
#adm:x:4:root,adm,daemon
#lp:x:7:daemon,lp
#uucp:x:14:uucp
#games:x:20:
#dip:x:40:

关闭系统不需要的服务

方法1:

执行ntsysv命令

方法2:

执行setuo命令->system service,然后在弹出的窗口中进行设置

[root@localhost ~]# chkconfig | grep 3:on #检查开机自启动的服务
[root@localhost ~]# for oldboy in `chkconfig --list|grep "3:on"|awk '{print $1}'|grep -vE "crond|network|sshd|rsyslog|sysstat"`;do chkconfig $oldboy off;done

只保留五个服务

crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off

关闭seLinux及iptables(在工作场景,如果有外部IP一般要打开)

[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config #关闭selinx
[root@localhost ~]# /etc/init.d/iptables stop  #关闭防火墙
iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
iptables: Flushing firewall rules:                         [  OK  ]
iptables: Unloading modules:                               [  OK  ]
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# /etc/init.d/iptables stop #重复执行确认已关闭
[root@localhost ~]# chkconfig iptables off   #关闭开机自启动命令,前面已经关闭这里无需执行
[root@localhost ~]# chkconfig --list|grep ipt
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off

跟改SSH服务端远程登录配置

echo "#-----------------sshConfig修改ssh默认登录端口,禁止root登录----------#"
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.`date +"%F%H%M%S"`
sed -i 's%#Port 22%Port 52113%' /etc/ssh/sshd_config #修改端口
sed -i 's%#PermitRootLogin yes%PermitRootLogin no%' /etc/ssh/sshd_config #是否允许密码为空的用户远程登录
sed -i 's%#UseDNS yes%UseDNS no%' /etc/ssh/sshd_config   #指定sshd是否应该对远程主机名进行反向解析
sed -i 's%GSSAPIAuthentication yes%GSSAPIAuthentication no%' /etc/ssh/sshd_config #解决linux之间使用ssh远程连接慢的问题
egrep "UseDNS|52113|RootLogin|EmptyPass|GSSAPIAuthentication" /etc/ssh/sshd_config 
/etc/init.d/sshd reload

添加密钥

[xpp@localhost ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/xpp/.ssh/id_rsa): 
Created directory '/home/xpp/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/xpp/.ssh/id_rsa.
Your public key has been saved in /home/xpp/.ssh/id_rsa.pub.
The key fingerprint is:
09:ee:a6:b7:c7:bc:20:20:46:e4:54:d0:c3:50:e6:46 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
| =BE             |
|+ ++             |
| o o. .          |
|. .  . . .       |
|...   . S        |
|.. . .           |
|    . +o         |
|     +..+        |
|    ...o..       |
+-----------------+

具体参考:http://www.xuepanpan.com/blog/299

根据自己服务器给普通账号设置sudo

具体参考: http://www.xuepanpan.com/blog/299

删除系统登录欢迎信息

删除/etc/issue、/etc/issue.net、/etc/redhat-release和/etc/motd文件内容

[root@localhost ~]# echo /dev/null >/etc/issue
[root@localhost ~]# echo /dev/null >/etc/issue.net
[root@localhost ~]# echo /dev/null >/etc/redhat-release
[root@localhost ~]# echo /dev/null >/etc/motd

禁止 Control-Alt-Delete 键盘关闭命令

[root@localhost ~]# vim /etc/init/control-alt-delete.conf

找到如下内容:exec /sbin/shutdown -r now “Control-Alt-Delete pressed”在之前加上“ #”,注释掉即可

设置Shell历史命令记录功能

[root@localhost ~]# vim /etc/bashrc

加入下面四行内容让history命令自动记录所有shell命令的执行时间

HISTFILESIZE=4000
HISTSIZE=4000
HISTTIMEFORMAT='%F %T'
export HISTTIMEFORMAT

设置tcp_wrappers 防火墙只允许固定IP登录

查看系统是否安装了 Tcp_Wrappers

[root@localhost ~]# rpm -q tcp_wrappers 
tcp_wrappers-7.6-57.el6.x86_64
[root@localhost xpp]# rpm -qa | grep tcp
tcp_wrappers-libs-7.6-57.el6.x86_64
tcpdump-4.0.0-3.20090921gitdf3cb4.2.el6.x86_64
tcp_wrappers-7.6-57.el6.x86_64
[root@localhost ~]# vim /etc/hosts.allow

添加内容:

sshd:172.16.1.118
[root@localhost ~]# vim /etc/hosts.allow

添加内容:

sshd:ALL

linux中文显示设置

[root@localhost etc]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
[root@localhost etc]# cp /etc/sysconfig/i18n /etc/sysconfig/i18n.ori
[root@localhost etc]# echo 'LANG="zh_CN.UTF-8"' >/etc/sysconfig/i18n
[root@localhost etc]# source /etc/sysconfig/i18n #使上文修改生效
[root@localhost etc]# echo $LANG
zh_CN.UTF-8

设置Linux服务器时间同步

[root@localhost ~]# echo "#times sync by lee at $(date +%F)" >>/var/spool/cron/root
[root@localhost ~]#  echo "*/5 * * * * /usr/sbin/ntpdate time.windows.com >/dev/null 2>&1" >> /var/spool/cron/root
[root@localhost ~]# crontab -l
#times sync by lee at 2017-12-28
*/5 * * * * /usr/sbin/ntpdate time.windows.com >/dev/null 2>&1

调整Linux文件描述符数量

TBD

Linux内核参数优化

TBD

定时清理邮件服务临时目录垃圾文件

TBD

锁定关键文件,防止篡改

TBD

禁止系统被Ping

TBD

升级具有典型漏洞的软件版本

TBD

基础优化与安全

  • 不用root登录,使用普通用户,通过sudo授权
  • 更改默认ssh端口,禁止root远程登录,甚至修改ssh只监听内网IP
  • 定时自动更新系统时间
  • 更新yum源
  • 关闭SELinux和iptables
  • 调整文件描述符数量。进程及文件的打开都会消耗文件描述符数量
  • 定时自动清零邮件临时目录,防止磁盘inode数量被小文件占满
  • 精简开机任务(如只保留crond、sshd、network、rsyslog、systat)
  • linux内核优化/etc/sysctl.conf,执行sysctl -p生效
  • 更改系统字符集LANG=en_us.UTF-8或LANG=zh_CN.UTF-8
  • 锁定系统关键文件,如/etc/passwd、/etc/shadow、/etc/group、/etc/gshadow、/etc/inittab,处理以上内容吧chattr、lsatr改名为oldboy并转移,这样就安全多了。
  • 清除系统版本信息,清空或修改/etc/issue、/etc/issue.net,去除登录后的系统信息显示
  • 清除系统多余的虚拟用户账号