centos7 配置 uwsgi 系统服务(systemd)

背景生产环境中采用nginx + uwsgi + django 来部署web服务,这里需要实现uwsgi的启动和停止,简单的处理方式可以直接在命令行中启动和kill掉uwsgi服务,但为了更安全、方便的管理uwsgi服务,配置uwsgi到systemd服务中,同时实现开启自启的功能;
另,鉴于supervisor不支持python3,没采用supervisor来管理uwsgi服务;

具体配置方法如下:

step1. 创建配置文件

/etc/systemd/system/server_uwsgi.service

step2. 填入以下内容

[Unit]
Description=HTTP Interface Server
After=syslog.target

[Service]
KillSignal=SIGQUIT
ExecStart=/usr/bin/uwsgi --ini /path/uwsgi.ini
Restart=always
Type=notify
NotifyAccess=all
StandardError=syslog

[Install]
WantedBy=multi-user.target

step3. 将该服务加入到systemd中

systemctl enable /etc/systemd/system/server_uwsgi.service

然后就可以通过systemctl来控制服务的启停

systemctl stop server_uwsgi.service 关闭uwsgi服务
systemctl start server_uwsgi.service 开启uwsgi服务
systemctl restart server_uwsgi.service 重启uwsgi服务

注意事项:

如果uwsgi配置文件中配置了 daemonize=/path/uwsgi.log (uwsgi服务以守护进程运行)
会导致sytemctl启动时多次重启而导致启动失败
需改为 logto=/path/uwsgi.log

CentOS7 安装 MySQL

系统环境

centOS7.5 64位

开始部署

1. 添加mysql yum源

在centOS上直接使用yum install mysql安装,最后安装上的会是MariaDB,所以要先添加mysql yum

rpm -Uvh https://repo.mysql.com//mysql80-community-release-el7-2.noarch.rpm

2. 安装(如果要安装最新版,可直接开始安装)

查看yum源中所有Mysql版本

yum repolist all | grep mysql

此时的最新版本是mysql8.0,把它禁用掉

yum-config-manager --disable mysql80-community

mysql5.7是我要安装的版本,启用mysql5.7

yum-config-manager --enable mysql57-community

检查刚才的配置是否生效

yum repolist enabled | grep mysql

开始安装

yum install mysql-community-server

3. 启动服务

service mysqld start

启动完成之后检查mysql状态,

service mysqld status

查看临时密码

grep 'temporary password' /var/log/mysqld.log

登录

mysql -uroot -p

CentOS7 安装 Docker 和 Docker-compose

注意需要使用 root 账户或者可以使用 sudo 的账户

1、安装 Docker

# 安装依赖
sudo yum install -y yum-utils 
  device-mapper-persistent-data 
  lvm2

# 添加docker下载仓库
sudo yum-config-manager 
    --add-repo 
    https://download.docker.com/linux/centos/docker-ce.repo

# 安装docker-ce
sudo yum install docker-ce

# 启动docker-ce
sudo systemctl start docker

# 验证
sudo docker --version

sudo docker run hello-world

2、安装 docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

sudo chmod +x /usr/local/bin/docker-compose

docker-compose --version

Centos7 系统下搭建.NET Core2.0+Nginx+Supervisor+Mysql环境

内容预览:

  • 直到微软推出完全开源的.NET Core~
  • 一方面,这个小巧的框架可以让某…~
  • 问题3:如果服务器宕机或需要重启我们则还是需要连入shell进行启动~

好记性不如烂笔头!

一、简介

一直以来,微软只对自家平台提供.NET支持,这样等于让这个“理论上”可以跨平台的框架在Linux和macOS上的支持只能由第三方项目提供(比如Mono .NET)。直到微软推出完全开源的.NET Core。这个开源的平台兼容.NET Standard,并且能在Windows、Linux和MacOS上提供完全一致的API。虽然这个小巧的.NET框架只是标准.NET的一个子集,但是已经相当强大了。

一方面,这个小巧的框架可以让某些功能性应用同时运行在三个平台上(就像某些功能性的Python脚本一样),另一方面,这也可以让服务器运维人员将ASP .NET服务程序部署在Linux服务器上(特别是对于运行Windows Server较为吃力的服务器)。

官网参考资料:https://www.microsoft.com/net/core#linuxcentos

二、.NET Core2.0 环境部署前准备

1.环境说明

服务器系统:CentOS 7.2.1511

2.安装前准备(关闭防火墙、关闭selinux)

1)关闭firewall:

systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动
firewall-cmd --state #查看默认防火墙状态(关闭后显示notrunning,开启后显示running)

2)关闭selinux

sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

查看改后文件如下:

[root@localhost ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

3)重启Centos

reboot

三、部署.NET Core2.0 环境

1.添加DOTNET产品

在安装.NET核心之前,您需要注册微软产品提要。这只需要做一次。首先,注册微软签名密钥,然后添加微软产品提要。

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-prodnenabled=1ngpgcheck=1ngpgkey=https://packages.microsoft.com/keys/microsoft.asc" > /etc/yum.repos.d/dotnetdev.repo'

2.安装.NET核心SDK

在下一步之前,请从您的系统中删除.NET .NET以前的任何预览版本。

以下命令更新用于安装的产品列表,安装.NET核心所需的组件,然后安装.NET核心SDK。

yum update
yum install libunwind libicu -y
yum install dotnet-sdk-2.0.0 -y

3.检查dotnet是否安装成功与版本查看

dotnet --info

dotnet --version

四、测试.NET Core2.0 环境

1.在home目录下初始化一个测试环境并输出”Hello World “内容 (测试方式一,可忽略)

cd /home
dotnet new console -o hwapp
cd hwapp
dotnet run

输出空内容如下:

[root@localhost hwapp]# dotnet run
Hello World!

2.上传.net core的实例页面进行测试 (测试方式二、推荐)

Centos 下.net core 2 环境测试用例 (把它上传到/home目录下或自定义的目录)

下载地址:

http://down.51cto.com/data/2334968

执行以下命令

cd /home/WebApplication1
dotnet restore   //如果使过用测试方式一,就需先执行这命令重新加载一下当前新的网站文件
dotnet run

运行后如下图:

未分类

通过IE访问测试页

五、安装配置nginx对ASP.NET Core应用的转发

1.安装Nginx环境

[root@localhost ~]#curl -o  nginx.rpm http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
[root@localhost ~]#rpm -ivh nginx.rpm
[root@localhost ~]#yum install nginx -y

输入:systemctl start nginx 来启动nginx。

[root@localhost ~]# systemctl start nginx

输入:systemctl enable nginx 来设置nginx的开机启动(linux宕机、重启会自动运行nginx不需要连上去输入命令)

[root@localhost ~]#systemctl enable nginx
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

2.通过iE检查能否访问

[root@localhost nginx-1.8.1]# ps -ef|grep nginx
root      14626      1  0 08:47 ?        00:00:00 nginx: master process nginx
nginx     14627  14626  0 08:47 ?        00:00:00 nginx: worker process
root      14636   3269  0 08:49 pts/1    00:00:00 grep --color=auto nginx

nginx常用的操作命令

systemctl start nginx.service               #启动nginx服务

systemctl enable nginx.service             #设置开机自启动

systemctl disable nginx.service            #停止开机自启动

systemctl status nginx.service             #查看服务当前状态

systemctl restart nginx.service           #重新启动服务

systemctl list-units –type=service        #查看所有已启动的服务

3.防火墙配置(如果系统有防火墙就需要进行写入规则)

命令:firewall-cmd –zone=public –add-port=80/tcp –permanent(开放80端口)

命令:systemctl restart firewalld(重启防火墙以使配置即时生效)

4.配置nginx对ASP.NET Core应用的转发

修改 /etc/nginx/conf.d/default.conf 文件。

将文件内容替换为

    server {
        listen 80;
        location / {
            proxy_pass http://localhost:88;
            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;
        }
    }

重新加载nignx

[root@localhost nginx]# nginx -s reload

nginx的配置己完成

5.开启dotnet run进行测试

[root@localhost ~]# cd /home/WebApplication1/
[root@localhost WebApplication1]# dotnet run
Using launch settings from /home/WebApplication1/Properties/launchSettings.json...
Hosting environment: Development
Content root path: /home/WebApplication1
Now listening on: http://[::]:88
Application started. Press Ctrl+C to shut down.

通过IP 80端口访问

六、配置守护服务(Supervisor)

目前存在三个问题

问题1:ASP.NET Core应用程序运行在shell之中,如果关闭shell则会发现ASP.NET Core应用被关闭,从而导致应用无法访问,这种情况当然是我们不想遇到的,而且生产环境对这种情况是零容忍的。

问题2:如果ASP.NET Core进程意外终止那么需要人为连进shell进行再次启动,往往这种操作都不够及时。

问题3:如果服务器宕机或需要重启我们则还是需要连入shell进行启动。

为了解决这个问题,我们需要有一个程序来监听ASP.NET Core 应用程序的状况。在应用程序停止运行的时候立即重新启动。这边我们用到了Supervisor这个工具,Supervisor使用Python开发的。

1.安装Supervisor

[root@localhost /]# yum install python-setuptools -y
[root@localhost /]#easy_install supervisor

2.配置Supervisor

[root@localhost /]#mkdir /etc/supervisor
[root@localhost /]#echo_supervisord_conf > /etc/supervisor/supervisord.conf

修改supervisord.conf文件,将文件尾部的配置

[root@localhost /]# vi /etc/supervisor/supervisord.conf

将里面的最后两行:

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

改为

[include]
files = conf.d/*.conf

ps:如果服务已启动,修改配置文件可用“supervisorctl reload”命令来使其生效

3.配置对ASP.NET Core应用的守护

创建一个 WebApplication1.conf文件,内容大致如下

[root@localhost /]# vi WebApplication1.conf
[program:WebApplication1]
command=dotnet WebApplication1.dll ; 运行程序的命令
directory=/home/WebApplication1/ ; 命令执行的目录
autorestart=true ; 程序意外退出是否自动重启
stderr_logfile=/var/log/WebApplication1.err.log ; 错误日志文件
stdout_logfile=/var/log/WebApplication1.out.log ; 输出日志文件
environment=ASPNETCORE_ENVIRONMENT=Production ; 进程环境变量
user=root ; 进程执行的用户身份
stopsignal=INT

将文件拷贝至:“/etc/supervisor/conf.d/WebApplication1.conf”下

[root@localhost /]#mkdir /etc/supervisor/conf.d
[root@localhost /]#cp WebApplication1.conf /etc/supervisor/conf.d/

运行supervisord,查看是否生效

[root@localhost /]#supervisord -c /etc/supervisor/supervisord.confsupervisord -c /etc/supervisor/supervisord.conf
[root@localhost /]# ps -ef | grep WebApplication1
root      29878  29685  0 09:57 ?        00:00:00 dotnet WebApplication1.dll
root      29892  29363  0 09:57 pts/3    00:00:00 grep --color=auto WebApplication1 

如果存在dotnet WebApplication1.dll 进程则代表运行成功,这时候在使用浏览器进行访问。

至此关于ASP.NET Core应用程序的守护即配置完成。

Supervisor守护进程常用操作

【启动supervisord】
确保配置无误后可以在每台主机上使用下面的命令启动supervisor的服务器端supervisord
supervisord

【停止supervisord】
supervisorctl shutdown

【重新加载配置文件】
supervisorctl reload

七 、配置Supervisor开机启动

新建一个“supervisord.service”文件

[root@localhost /]# vi supervisord.service
# dservice for systemd (CentOS 7.0+)
# by ET-CS (https://github.com/ET-CS)
[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

将文件拷贝至:“/usr/lib/systemd/system/supervisord.service”

[root@localhost /]# cp supervisord.service /usr/lib/systemd/system/

执行命令:systemctl enable supervisord

[root@localhost /]# systemctl enable supervisord
Created symlink from /etc/systemd/system/multi-user.target.wants/supervisord.service to /usr/lib/systemd/system/supervisord.service.

执行命令:systemctl is-enabled supervisord #来验证是否为开机启动

[root@localhost /]# systemctl is-enabled supervisord

重启系统看能否能成功访问

[root@localhost /]# reboot

CentOS 7安装pip3

首先要确保在CentOS 7上已经安装了EPEL仓库。如果没有安装,执行以下命令安装:

yum install -y epel-release

安装后,安装pip3

$sudo yum install python34-pip
$pip3.4 install foo

Centos7搭建Git及安装使用

一、Centos7.2 下 Git的安装

1、查看是否安装了git

rpm -qa|grep git

未分类

若已经安装,需要先卸载。卸载命令如下:

rpm -e --nodeps git  或者  rpm -e git

未分类

2、安装Git

yum install git

未分类

输入y,并回车

未分类

再使用 rpm -qa|grep git 来查看是否已经安装好了Git。

未分类

3、创建Git仓库

mkdir six_git        // 创建文件夹
useradd six             //创建用户名并设置密码
passwd six            //(系统会提示输入密码和再次密码)
groupadd git        // 创建组
git init --bare        //进入所创建的文件夹,初始化一个仓库
chown -R six:git /var/www/six_git/        // 赋权限

未分类

二、windows7 下连接Git版本库

1、安装Git 和 TortoiseGit(小乌龟)

2、在本地创建文件夹

>在windows7中某个盘符下创建一个名为“six” 的文件夹,这个文件夹就是我们与远程仓库通讯的文件夹。

3、进入“six”文件夹,右击菜单设置本机用户名和email

为了直观分辨不同用户为版本开发的贡献,需要设定用户名和email
下面分别是中文版和英文版的 TortoiseGit

未分类

未分类

未分类

未分类

4、Clone版本库(克隆)

在要克隆版本库的文件夹中点击右键,点击“Git克隆”菜单,根据图中信息填写后,点击确定,输入当时创建“six”账户是设置的密码即可。

未分类

未分类

未分类

输入创建“six”用户时,设置的密码。
英文版本:

未分类

未分类

填写好后,点击“ok”按钮,并要求输入密码。

未分类

5、添加文件

在clone库的文件夹中添加一个测试文件,并在空白处点击邮右键,选择“TortoiseGit”-à “添加”

未分类

勾选未受版本控制的文件,也就是刚刚创建的测试文件,点击确定按钮。

未分类

添加完成后,弹出“加入完成”对话框,点击确定即可

未分类

再在该文件夹空白出点击右键,点击“Git提交(C)->”master”…”

未分类

在对话框中填写相关的说明信息并选择文件,点击提交即可。

未分类

6、更新版本库

更新版本库前应该改先从服务器上“拉取”一下,再把自己修改的内容“推送”到服务器上.

未分类

未分类

三、建立发布版本库

1、在centos中建立发布版本库,然后进入文件夹。

未分类

2、更新版本库,进入版本目录,执行“git pull”命令

未分类

3、 禁止shell登陆

出于安全考虑,git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。
找到类似下面的一行:

six:x:502:502::/home/six:/bin/bash
改为
six:x:502:502::/home/six:/usr/local/git/bin/git-shell
或者
six:x:502:502::/home/six:/usr/bin/git-shell
six:x:502:502::/home/six:/bin/false

git用户可以正常通过ssh使用git,但无法登录shell,因为我们为git用户指定的git-shell每次一登录就自动退出。

未分类

四、错误

未分类

在 windows文件夹 下 bash命令中输入:

git fetch --all
git reset --hard origin/master

Git的bash操作:

进入git bash,我这个windows下个git已经安装配置完成了,所以直接clone,192.168.153.129为我centos7的ip
git clone [email protected]:/srv/git/project.git
输入git的密码
进入project.git
cd project.git
创建一个测试文件
vim test.txt
随便写一些内容
查看状态
git status      看到有一个待添加的文件texs.txt了
添加test.txt让git追踪
git add test.txt
提交到版本库,并写上备注信息
git commit test.txt -m ‘just a test’
把他推送到服务器的版本库中去
git push origin master

未分类

git client

未分类

git commit

已经推送到服务器端区了。。。接下来去服务器端看看有木有

刚才俺们在服务器端创建版本库时用的是 git init –bare project.git 加了个–bare就是创建一个裸仓库,没有工作区哒,所以这里只记录了文件的改动,要看是不是同步过来了,需要在git clone一次,看是不是这一次有test.txt啦

未分类

未分类

git clone [email protected]:/srv/git/project.git,输入密码后,开始下载代码了,果然master分支已经有test.txt啦.
git log查看一下提交记录,在mon feb 2 09:47:46 2015 +0800的这个,邮件为xxx的作者xxx提交哒。因为windows客户端配置的git是偶滴工作邮件和姓名,就打码了,ok,私有git搭建完成。

git的删除操作

rm test.txt
git status
$ git rm test.txt
rm 'test.txt'
git add -u
$ git commit -m "remove test.txt"
git push

现在,文件就从版本库中被删除了。

另一种情况是删错了,因为版本库里还有呢,所以可以很轻松地把误删的文件恢复到最新版本:

$ git checkout -- test.txt

git checkout其实是用版本库里的版本替换工作区的版本,无论工作区是修改还是删除,都可以“一键还原”。

小结

命令git rm用于删除一个文件。如果一个文件已经被提交到版本库,那么你永远不用担心误删,但是要小心,你只能恢复文件到最新版本,你会丢失最近一次提交后你修改的内容。

CentOS7 Samba简单配置

1. 安装

yum -y install samba

# 备份配置文件
cp  /etc/samba/smb.conf  /etc/samba/smb.conf.bak

# 创建samba用户
# 首先 添加系统用户
useradd officecoop
rm -rf /home/officecoop/.*
# 然后添加smb用户,按提示输入密码
smbpasswd -a officecoop

其他注意 iptables(或防火墙配置) 和 selinux 配置

2. 设置共享目录

2.1 目标

windows 访问 10.0.209.140 (假设samba服务器假设在140) 时显示 officecoop 目录

2.2 步骤

# 追加如下内容到配置文件
cat >> /etc/samba/smb.conf << EOF
[officecoop]
comment = officecoop
path = /home/officecoop/officecoop
writable = yes
EOF

# 创建文件夹
mkdir /home/officecoop/officecoop

# 设置属主
chmod officecoop /home/officecoop/officecoop

# 重启服务
systemctl restart smb

centos7下部署iptables环境纪录(关闭默认的firewalle)

CentOS7默认的防火墙不是iptables,而是firewall.
由于习惯了用iptables作为防火墙,所以在安装好centos7系统后,会将默认的firewall关闭,并另安装iptables进行防火墙规则设定

下面介绍centos7关闭firewall安装iptables,并且开启80端口、3306端口的操作记录:

[root@localhost ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)

1、关闭firewall:

[root@localhost ~]# systemctl stop firewalld.service            //停止firewall
[root@localhost ~]# systemctl disable firewalld.service        //禁止firewall开机启动

2、安装iptables防火墙

[root@localhost ~]# yum install iptables-services            //安装
[root@localhost ~]# vim /etc/sysconfig/iptables              //编辑防火墙配置文件
# Firewall configuration written by system-config-firewall
# Manual customization of this file is not recommended.
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
[root@localhost ~]# systemctl restart iptables.service                  //最后重启防火墙使配置生效
[root@localhost ~]# systemctl enable iptables.service                  //设置防火墙开机启动
[root@localhost ~]# iptables -L                 //查看防火墙规则,默认的是-t filter,如果是nat表查看,即iptables -t nat -L

3、关闭SELINUX

[root@localhost ~]# vim /etc/selinux/config
#SELINUX=enforcing           //注释掉
#SELINUXTYPE=targeted           //注释掉
SELINUX=disabled              //增加
[root@localhost ~]# setenforce 0       //使配置立即生效

CentOS7部署vsftpd服务

vsftp安装 创建用户 并限定用户在自己的目录

1、查看是否已经安装了vsftpd

vsftpd -version

2、安装vsftpd(CentOS7)

yum install -y vsftpd

3、新建FTP目录

创建的FTP目录如下:

/data/KodServer/data/User/tomas/home

4、创建新用户

ftpname为你为该ftp创建的用户名,/data/KodServer/data/User/tomas/home 为登录进去后的位置

useradd -d /data/KodServer/data/User/tomas/home -s /bin/bash ftpname

5、为新建的用户设置密码

passwd ftpname

【备注:用cat etc/passwd可以查看当前系统用户】

6、设置主目录(更改登录时看到的目录,看个人需要更改,如果第4步已设置好,此步可忽略)

usermod -d /data/KodServer/data/User/tomas/home ftpname

7、添加用户组(这步可忽略)

groupadd ftpgroup

8、防火墙添加FTP服务

firewall-cmd --permanent --zone=public --add-service=ftp
firewall-cmd --reload

解决办法:

①、查看ftp的Selinux状态:sestatus -b | grep ftp
②、在结果中可以看到: ftp_home_dir off
③、将状态改为on:setsebool -P ftp_home_dir on
④、重启vsftp服务:systemctl restart vsftpd.service

setsebool -P ftpd_full_access on

systemctl restart firewalld.service

9、配置只能访问自身目录(/data/KodServer/data/User/tomas/home),不能访问其他路径

修改/etc/vsftpd.conf

cd /etc/vsftpd/
vi vsftpd.conf

#去掉前面的注释
chroot_local_user=YES

ascii_upload_enable=YES
ascii_download_enable=YES

#文件末尾添加
allow_writeable_chroot=YES

保存,重启vsftpd

设置开机启动:

systemctl enable vsftpd.service

启动vsftpd服务

systemctl start  vsftpd.service

禁止ftp用户通过22端口登录ftp服务器:

由于需要限制ftp用户在自己的目录,在21端口下没有问题,但当ftp用户用sftp登录时,还是可以访问上级目录,于是禁止ftp用户ssh登录,切断22端口的通信。

首先,执行如下命令,找到nologin的shell:

vi /etc/shells

可以看到禁止登录的shell文件为/usr/sbin/nologin,然后执行如下命令:

usermod -s /usr/sbin/nologin tomas

如果要恢复tomas的ssh登录,执行如下命令:

usermod -s /bin/bash tomas