Linux SSH Swap
Supervisor Nginx git
1、前言
新购买的腾讯云服务器需要在上面进行环境的一些处理以及软件安装,这里我介绍一下我的服务器初始化过程。
2、系统 CentOS Linux release 7.5.1804 (Core)
Linux查看系统版本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 cat /etc/*release* CentOS Stream release 8 NAME="CentOS Stream" VERSION="8" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="8" PLATFORM_ID="platform:el8" PRETTY_NAME="CentOS Stream 8" ANSI_COLOR="0;31" CPE_NAME="cpe:/o:centos:centos:8" HOME_URL="https://centos.org/" BUG_REPORT_URL="https://bugzilla.redhat.com/" REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux 8" REDHAT_SUPPORT_PRODUCT_VERSION="CentOS Stream" CentOS Stream release 8 CentOS Stream release 8cpe:/o:centos:centos:8
3、初始化 3.1、用户
添加用户与组
1 2 3 4 5 6 7 8 useradd mingliang.gao groupadd opsdev passwd mingliang.gao usermod -G opsdev mingliang.gao
配置用户root权限
root用户vim /etc/sudoers
1 2 root ALL =(ALL ) ALL mingliang.gao ALL =(ALL ) ALL
其他
1 2 删除用户:userdel 用户名 删除组:groupdel 组名
3.2、环境编码 修改服务器系统环境编码,执行vim /etc/profile,加入一下内容:
1 2 3 export LC_ALL =en_US.UTF-8export LANG =en_US.UTF-8
查看环境编码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [root@VM-4-7-centos mingliang.gao]# echo $LANG en_US.UTF-8 [root@VM-4-7-centos mingliang.gao]# locale locale: Cannot set LC_CTYPE to default locale: No such file or directory locale: Cannot set LC_ALL to default locale: No such file or directoryLANG =en_US.UTF-8LC_CTYPE =zh_CN.UTF-8LC_NUMERIC ="en_US.UTF-8" LC_TIME ="en_US.UTF-8" LC_COLLATE ="en_US.UTF-8" LC_MONETARY ="en_US.UTF-8" LC_MESSAGES ="en_US.UTF-8" LC_PAPER ="en_US.UTF-8" LC_NAME ="en_US.UTF-8" LC_ADDRESS ="en_US.UTF-8" LC_TELEPHONE ="en_US.UTF-8" LC_MEASUREMENT ="en_US.UTF-8" LC_IDENTIFICATION ="en_US.UTF-8" LC_ALL=
3.3、VIM 新的云服务器初始化环境只有VI,但是编辑的时候VIM很好用,需要安装。
安装
配置
vi /etc/vimrc,打开vimrc配置问题添加一下2行简单配置,如果详细配置请百度。
3.4、SSH 用于免密码连接登录。
1 2 ssh-copy -id 用户@服务器IP 输入用户登录密码
详情:<a href="/articles/1431/" target="_blank" class="block_project_a">SSH之免密码登录 </a>
3.5、SWAP 有些人可能对swap可能陌生,简单说下SWAP可以理解成虚拟内存,当服务器物理内存不足时,拿出部分硬盘指定空间当swap分区(虚拟成内存)使用,从而解决内存容量不足的情况。 可以先free一下看下是否又swap,如果想配置请用root用户操作。
生成
1 2 3 4 dd if =/dev/zero of =/var/swap bs =1024 count =3072000 mkswap /var/swap swapon /var/swap free -m
解释一下dd,可以理解成dd获取了磁盘的一块空间,有兴趣的可以深入学习一下dd。
Swap开机初始化
vi /etc/fstab,最后一行添加
1 swap /var/swap swap defaults 0 0
清理swap
Swap活跃使用度
vim /proc/sys/vm/swappiness,范围是0~100,指数越大使用的活跃度越大,建议30-50即可。
3.6、GIT
查看
安装
配置
下面是全局配置,也可以在单独的git项目中单独配置属于项目的git配置。
1 2 3 4 5 6 7 git config --global user.name "mingliang.gao" git config --global user.email "gaoming971366@163.com" git config --list
3.7、MariaDB MariaDB安装与配置
3.8、Miniconda
下载
这里介绍2种方式,第一种就是直接去官网下载:https://docs.conda.io/projects/miniconda/en/latest/ 第二种方式直接在服务器上执行wget:
1 wget https:// repo.anaconda.com/miniconda/ Miniconda3-latest-Linux-x86_64.sh
安装
1 base Miniconda3-latest-Linux-x86_64.sh
Do you accept the license terms? [yes|no] 输入yes接受许可条款
配置安装路径,默认安装到根目录下的,可自定义路径(建议/usr/local/miniconda)
等待安装后,出现Do you wish the installer to initialize Miniconda3,如果输入yes会对~/.bashrc文件添加一下内容
1 2 3 4 5 6 7 8 9 10 11 12 13 14 __conda_setup="$('/usr/local/miniconda/bin/conda' 'shell.bash' 'hook' 2> /dev/null) " if [ $? -eq 0 ]; then eval "$__conda_setup " else if [ -f "/usr/local/miniconda/etc/profile.d/conda.sh" ]; then . "/usr/local/miniconda/etc/profile.d/conda.sh" else export PATH="/usr/local/miniconda/bin:$PATH " fi fi unset __conda_setup
配置
生效:
禁止用户登录后自动激活base环境:
1 conda config --set auto_activate_base
具体conda命令请参考学习官网或者baidu。
3.9、Gunicorn
安装
在python项目运行环境中会进行安装,单独安装命令:
日志配置
python项目需要用到,提前建好,用来存放日志,root用户执行。
1 mkdir -p /var /log /gunicorn
3.10、Supervisor
安装
1 2 3 4 5 6 7 yum install -y supervisorsystem ctl start supervisordsystem ctl enable supervisord
查看下进程
1 ps -ef | grep supervisord
系统默认的启用命令是:/usr/bin/supervisord -c /etc/supervisord.conf
自定义配置
切换root执行。
1 2 3 4 echo_supervisord_conf > /etc/supervisord.d /supervisord.conf cd /etc/supervisord.d mkdir include mkdir -p /var /log /supervisord
echo_supervisord_conf是配置文件写入,把项目配置文件与是sp的配置文件放在一起便于管理,建立include存放项目的对应配置文件,以下是本人的配置文件:
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 [unix_http_server] file =/var/run/supervisor.sock chmod =0777 [supervisorctl] serverurl =unix:///var/run/supervisor.sock [rpcinterface:supervisor] supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface[supervisord] logfile =/var/log/supervisord/supervisord.log logfile_maxbytes =50 MB logfile_backups =10 loglevel =info pidfile =/var/run/supervisord.pid nodaemon =false minfds =1024 minprocs =200 [include] files = /etc/supervisord.d/include/*.conf
启动命令:/usr/bin/supervisord -c /etc/supervisord.d/supervisord.conf -c为指定配置文件。
这样启动的supervisor如果重启需要杀死进程:
1 2 ps -ef | grep supervisordkill 进程ID
为了方便我把2个命令合并到一起:
1 ps -ef | grep supervisord | grep -v grep | awk -F " " '{print $2}' | xargs kill
直接执行上面这句话就可以直接杀死进行,这是个通用杀死指定进程的命令,只需要更换grep supervisord需要查询过滤的进程关键字。
其他
贴一下自己的supervisord项目配置文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 [program:open2lisapi] directory =/home/mingliang.gao/projects/open2lisapicommand =/home/mingliang.gao/projects/open2lisapi/.venv/bin/gunicorn -c /home/mingliang.gao/projects/open2lisapi/etc/prod/gunicorn.conf wsgi:app autostart =True autorestart =True startretries = 3 redirect_stderr =True stopasgroup =True priority =999 stdout_logfile_maxbytes =20 MB stdout_logfile_backups = 20 stdout_logfile =/var/log/supervisord/supervisor_open2lisapi.log
3.11、Nginx
检查gcc环境
gcc编译器。
1 2 3 4 5 gcc -v yum -y install gcc
安装Nginx
root用户或者sudo命令。
1 2 3 4 5 # 查看版本 yum list | grep nginx # 安装 yum - y install nginx
安装后执行whereis Nginx
执行目录:/usr/sbin/nginx
模块所在目录:/usr/lib64/nginx
配置所在目录:/etc/nginx/
默认站点目录:/usr/share/nginx/html
启动
1 2 3 4 5 6 7 8 system ctl start/stop/restart nginx.servicesystem ctl enable nginx.servicesystem ctl reload nginx.servicesystem ctl status nginx.service
查看进程
开放端口
1 2 3 4 5 6 7 8 9 firewall-cmd --zone=public --add-port=80/tcp --permanent firewall-cmd --reload systemctl restart firewalld firewall-cmd --list-all
这里根据自己的项目需求,对外开放端口。
配置
下面是我的nginx配置,仅供参考:
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 86 87 88 89 90 91 92 93 94 95 96 user root;worker_processes 2 ;error_log /var/log/nginx/error .log;pid /run/nginx.pid;include /usr/share/nginx/modules/*.conf ;events { worker_connections 1024 ; }http { log_format main '$remote_addr - $remote_user [$time_local ] "$request " ' '$status $body_bytes_sent "$http_referer " ' '"$http_user_agent " "$http_x_forwarded_for "' ; access_log /var/log/nginx/access.log main; sendfile on ; tcp_nopush on ; tcp_nodelay on ; keepalive_timeout 65 ; types_hash_max_size 2048 ; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf ; }
因为有些参数在腾讯云服务器不能识别,所以有些被注释。
user root本人使用root用户启动,使用哪个用户就配置哪个
server配置,单独文件,存放/etc/nginx/conf.d
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 server { listen 80 ; server_name _; access_log /var/log/nginx/default_server.access.log main; root /home/mingliang.gao/projects/blog; location = / { index root.html; } location / { index index.jsp index.html index.htm; proxy_redirect off ; proxy_set_header Host $host ; proxy_set_header X-Real-IP $remote_addr ; proxy_set_header X-Real-PORT $remote_port ; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; } error_page 404 /error .html; location = /40x.html { } error_page 500 502 503 504 /error .html; location = /50x.html { } }
常用命令
1 2 3 4 5 nginx -c /etc/nginx/nginx.conf nginx -s reload
访问
配置完把nginx重启之后,访问IP:PORT。
源码安装
Nginx源码安装教程
4、详细连接