[实验] Linux rpm 软件包的制作 (通过 rpm-build 实现) (Nginx 版)

注意:文中的很多信息例如软件的名称等是站主在本次操作中随意取的名称,读者可以根据自己的喜好换成任意别的名称

软件准备:

在 Nginx 官网上下载搭建集群所需软件 Nginx:

http://nginx.org/en/download.html

正文:

步骤目录:

步骤一:系统环境要求

步骤二:准备 rpm-build 软件
2.1 安装 rpm-build
2.2 生成 rpm-build 目录
2.3 显示生成的 rpm-build 目录

步骤三:制作 Nginx 软件的 rpm 包
3.1 将 Nginx 的源码安装包放在固定的位置
3.2 制作 Nginx 的 nginx.spec 文件
3.3 SPEC 配置文件里的内容说明
3.4 安装 Nginx 软件的依赖包
3.6 显示新生成的 Nginx rpm 软件包

步骤四:使用新生成的 Nginx rpm 软件包
4.1 安装刚刚新生成的 Nginx rpm 软件包
4.2 启动 Nginx 服务
4.3 显示 Nginx 服务的启动状态
4.3.1 显示 Nginx 网页是否可以访问
4.3.2 显示 Nginx 的端口有没有启动

具体的操作步骤:

步骤一:系统环境要求

1) 服务器的系统是 CentOS 7 版本
2) 服务器系统要配置好 yum 源

步骤二:准备 rpm-build 软件
2.1 安装 rpm-build

# yum -y install rpm-build

2.2 生成 rpm-build 目录

# rpmbuild -ba nginx.spec
error: failed to stat /root/nginx.spec: No such file or directory

2.3 显示生成的 rpm-build 目录

# ls /root/rpmbuild/
BUILD  BUILDROOT  RPMS	SOURCES  SPECS	SRPMS

步骤三:制作 Nginx 软件的 rpm 包
3.1 将 Nginx 的源码安装包放在固定的位置

# cp nginx-1.16.1.tar.gz /root/rpmbuild/SOURCES/

(补充:这里要安装的 Nginx 版本是 1.16.1)

3.2 制作 Nginx 的 nginx.spec 文件

# vi /root/rpmbuild/SPECS/nginx.spec

将部分内容修改如下:

Name:nginx
Version:1.16.1
Release:1.0
Summary:Nginx is a web server software.
......
License:GPL
URL:www.nginx.org
Source0:%{name}-%{version}.tar
......
%description
nginx is an HTTP and reverse proxy server ......
......
%post
useradd nginx
......
%prep
%setup -q
......
%build
./configure --user=nginx --group=nginx --with-http_ssl_module
make %{?_smp_mflags}
%install
make install DESTDIR=%{buildroot}
......
%files
......
%doc
/usr/local/nginx/*
......
%changelog


补充:
1) 这里的 Name:nginx 是指软件名称是 nginx
2) 这里的 Version:1.16.1 是指软件的版本是 1.16.1
3) 这里的 Release:1.0 是指发布的版本是 1。0
4) 这里的 Summary: Nginx is a web server software. 是指软件的描述是 Summary: Nginx is a web server software.
5) 这里的 License:GPL 是指软件使用的协议是 GPL
6) 这里的 URL:www.nginx.org 是指软件的官网是 www.nginx.org
7) 这里的 Source0:%{name}-%{version}.tar 是指软件源码文件的名称
8) 这里的 #BuildRequires: 是指软件编译安装时需要的依赖包,这里没有内容
9) 这里的 #Requires: 是指软件安装时所需要的依赖包,这里没有内容
10) 这里的 %description 是指软件的详细描述,这里没有内容
11) 这里的

%post
useradd nginx

是指软件安装后创建 nginx 用户

12) 这里的 %prep 是指软件安装前的准备,这里没有内容
13) 这里的 %setup –q 是指自动解压软件的源码包,并 cd 进入刚刚解压出来的目录
14) 这里的

%build
./configure
make %{?_smp_mflags}

是指对源码安装包进行配置
15) 这里的

%install
make install DESTDIR=%{buildroot}

是指对源码安装包进行编译安装
16) 这里的

%files
%doc
/usr/local/nginx/*

是指将源码安装包安装到 /usr/local/nginx/ 目录里

3.4 安装 Nginx 软件的依赖包

# yum -y install gcc pcre-devel openssl-devel

3.5 生成 Nginx rpm 软件包

# rpmbuild -ba /root/rpmbuild/SPECS/nginx.spec

3.6 显示新生成的 Nginx rpm 软件包

# rpm -qpi /root/rpmbuild/RPMS/x86_64/nginx-1.16.1-1.0.x86_64.rpm

步骤四:使用新生成的 Nginx rpm 软件包
4.1 安装刚刚新生成的 Nginx rpm 软件包

# rpm -ivh /root/rpmbuild/RPMS/x86_64/nginx-1.16.1-1.0.x86_64.rpm

4.2 启动 Nginx 服务

# /usr/local/nginx/sbin/nginx

4.3 显示 Nginx 服务的启动状态
4.3.1 显示 Nginx 网页是否可以访问

# curl http://127.0.0.1/

4.3.2 显示 Nginx 的端口有没有启动

# ss -ntulap | grep 80

[工具] Shell 批量检测域名的 DNS 解析是否正确

介绍:

作者:朱明宇
名称:批量检测域名的 DNS 解析是否正确
作用:批量检测域名的 DNS 解析是否正确

使用方法:
1. 在此脚本的分割线内写入相应的内容
2. 给此脚本添加执行权限
3. 执行此脚本

脚本分割线里的变量:
1. main_domain=eternalcenter.com #此处填写网站的“主域名”,网站一般会有几个域名,其中“主域名”直接指向 ip 地址,其他的域名都指向主域名
2. ip=192.154.240.64 #域名指向的 ip 地址
3. nginx=1.16.0 #web 程序的对应版本的版本
4. content1=”eteranlcenter” # 随即填写一个具有代表性的网站首页的内容
5. content2=”Mingyu” # 随即填写一个具有代表性的网站首页的内容,但是要和前面的 content1 在同一行
6. domain=”eternalcenter.com eternalcentre.com zhumingyu.com mingyuzhu.com eternalcenter.org eternalcentre.org” #需要检测的域名

脚本:

#!/bin/bash

####################### Separator ########################

main_domain=eternalcenter.com
ip=144.48.142.148
nginx=1.16.0
content1="eternalcenter"
content2="Mingyu"
domain="eternalcenter.com eternalcentre.com zhumingyu.com mingyuzhu.com eternalcenter.org eternalcentre.org"

####################### Separator ########################

check_domain_name(){
if [ $1 == $main_domain ];then
	
	ping -c4 -i0.4 $1 | grep $ip &> /dev/null
	if [ $? -ne 0 ];then
		echo -e "\033[31m $1 is error !!!!! ip is error !!!!! \033[0m"
	else
		curl $1 | grep $nginx &> /dev/null
		if [ $? -ne 0 ];then
			echo -e "\033[31m $1 is error !!!!! software is error !!!!! \033[0m"
		else	
			elinks $1 | grep "$content1" | grep "$content2" &> /dev/null
			if [ $? -ne 0 ];then
				echo -e "\033[31m $1 is error !!!!! web is error !!!!! \033[0m"
			else
				echo -e "\033[32m $1 is ok \033[0m"
			fi
		fi
		
	fi
else
	elinks $1 | grep "$content1" | grep "$content2" &> /dev/null
	if [ $? -ne 0 ];then
		echo -e "\033[31m $1 is error !!!!! web is error !!!!! \033[0m"
	else
		echo -e "\033[32m $1 is ok \033[0m"
	fi


fi
}

for i in $domain
do
	check_domain_name $i
done

[内容] Apache 的使用 (开启网页目录结构)

内容一:开启 Apache 目录结构的目的

默认安装了 Apache 之后,被访问的网页只会显示一张 Apache 的欢迎页面或者 404
有时候我们需要让网页现实网页根目录中的文件和目录,用于提供 http 下载等功能,这个时候就需要开启 Apache 的目录结构

内容二:开启 Apahce 目录结构的方法

# vim /etc/httpd/conf.d/welcome.conf

将其中的:

......
Options -Indexes 
......

修改为:

......
Options +Indexes 
......

[工具] Shell 半自动化部署 LNMP 平台 + SSL (CentOS Linux 7 版)

介绍:

作者:朱明宇
脚本名称:半自动化部署 LNMP 平台 + SSL
作用:半自动化安装 LNMP,即 Nginx、Mariadb、PHP、php-fpm,实现 HTTPS

使用方法:
为了安全起见,设置系统用户和数据库用户和数据库权限的工作请按照脚本最后的步骤进行手动设置

注意:
1. 执行本脚本之前请确保 Nginx、PHP、网站程序文件、SQL、SSL 的公钥、SSL 的私钥、shell、本脚本 8 个文件,如果是新建网站的话,不用准备 SQL 文件
2. Nginx 文件:请将 Nginx 源码安装包放在 /root/
3. PHP 文件:请将 PHP 源码安装包放在 /root/
4. 网站程序文件:请将网站程序文件的名字中包含“eternalcenter-backup-”字段,并且是 tar 包,并放在 /root/
5. SQL 文件:如果是恢复网站的话,请将备份的数据文件 *.sql 放在 /root/,注意这是一个 MySQL 备份文件
6. SSL 的公钥文件和 SSL 私钥文件:请将网站安全证书的公钥和私钥分别命名为:eternalcenter.com.crt 和 eternalcenter.com.key,并放在 /root/
7. shell 文件:请将监控脚本命名为 port_monitoring.sh 并放在 /root/
8. 除此之外 /root/ 目录下请不要放任何文件
9. 服务器的系统需要是 CentOS 7 版本
10. 服务器系统要配置好可用的软件源(最好是软件数量最多的官方版本)
11. 服务器要能够连接外网

补充:
1. Nginx 下载网址:https://nginx.org/
2. PHP 下载网址:https://www.php.net/downloads.php
3. WordPress 是一款开源的 php 网站程序,建议使用,下载网址:https://cn.wordpress.org/download/
4. 网站安全证书的公钥和私钥可以在这里申请:https://freessl.cn/

其他:

  运行此脚本所需要的文件如下,请尽量使用图中的命名规则给他们命名,并将他们全部放在 /root/ 目录下,请保证 /root/ 下有且仅有这些文件,并以 root 的身份运行,否则脚本会执行失败。其中:

  eternalcenter-backup-2019-08-12-13.sql 指的是 mariadb 的数据库备份文件,如果是新建网站的话可以不要。

  eternalcenter-backup-2019-08-13.tar 指的是网页程序的压缩包,必须是 tar 格式,如果是新建的网站的话,这个也可以换成,新网站的网页程序。

  eternalcenter.com.crt 指的是网站的公钥,需要单独申请,方法可以在搜索引擎上搜索。

  eternalcenter.com.key 指的是网站的私钥,需要单独申请,方法可以在搜索引擎上搜索。

  install.sh 就是指的本脚本。

  nginx-1.16.0.tar.gz 指的是 nginx 的源码安装包,这里的 -1.16.0.tar.gz 可以任意命名,只用保证这个文件名字里包含 nginx 就行了,这个文件可以在 nginx 官网上下载。

  php-7.2.2-.tar.xz 指的是 php 的源码安装包,同样 这里的 -7.2.2-tar.xz 可以任意命名,只用保证这个文件名字包含 php 就行了,这个文件可以在 php 官网上下载。

  port_monitoring.sh 只是一个监控端口的脚本,可以不要,使用者可以根据自我需求写一个,站主是通过这个脚本定期检查并自动启动需要启动但是没有启动的端口,关闭不需要但是启动了的端口。

脚本:

#!/bin/bash

# variable initialization
cd
nginxp=`ls | grep "nginx"`
phpp=`ls | grep "php"`
webp=`ls | egrep "eternalcenter-backup-.*tar$"`
nginxr=${nginxp%%.tar*}
phpr=${phpp%%.tar*}


# upgrade system
yum makecache
yum -y update

yum clean all
yum repolist


# remove useless software
yum -y remove php*

yum -y remove mariadb*

systemctl stop postfix
yum -y remove postfix

systemctl stop chronyd
yum -y remove chrony

yum -y update


# install dependency package
yum -y install gcc pcre-devel openssl-devel libxml2-devel curl-devel libpng libpng-devel


# install nginx
cd
tar -xvf $nginxp -C /root/
cd $nginxr

useradd -s /sbin/nologin nginx
./configure --user=nginx --group=nginx --with-http_ssl_module
make && make install
echo 'worker_processes  1;

events {
    worker_connections  65536;
}

http {
    limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       80;
        limit_req zone=one burst=5;
        server_name www.eternalcenter.com eternalcenter.com;

        rewrite ^/(.*)$ https://eternalcenter-may-1-2022.github.io/$1 permanent;
      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        }

    server {
        listen       443 ssl;
        server_name www.eternalcenter.com eternalcenter.com;

        if ($request_method !~ ^(GET|POST)$){
        return 444;
        }

        ssl_certificate      /usr/local/nginx/ssl/eternalcenter.com.crt;
        ssl_certificate_key  /usr/local/nginx/ssl/eternalcenter.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

        location ~ \.php$ {
            fastcgi_pass 127.0.0.1:9000;
            include fastcgi.conf;
        } 

        location / {
        root html;
        index index.php index.html index.htm;
        }

        location ~ ^/\.user\.ini {
        deny all;
        }
    
        location ~*\.(jpd|jpeg|gif|png|css|js|ico|xml)$ {
        expires 30d;
        }

        error_page  404              /404.html;
        }

        gzip on;
	gzip_min_length 1000;
	gzip_comp_level 4;
	gzip_types text/plain test/css application/json application/x-javascript text/xml application/xml
	application/xml+rss text/javascripts;

	client_header_buffer_size 1k;
	large_client_header_buffers 4 4k;

	open_file_cache max=2000 inactive=20s;
	open_file_cache_valid  60s;
	open_file_cache_min_uses 5;
	open_file_cache_errors off;

}' > /usr/local/nginx/conf/nginx.conf
cd
mkdir /usr/local/nginx/ssl
mv eternalcenter.com.crt /usr/local/nginx/ssl
mv eternalcenter.com.key /usr/local/nginx/ssl

# install php
cd
tar -xvf $phpp -C /root/
cd $phpr
useradd -s /sbin/nologin php-fpm 
./configure --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --prefix=/usr/local/php --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mysqlnd --enable-mbstring --with-curl --with-gd --with-zlib
make && make install
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
chown -R php-fpm:php-fpm /usr/local/nginx/html/

# install mariadb
yum -y install mariadb mariadb-server mariadb-devel
sed -i "/^datadir/a log_bin=ec" /etc/my.cnf
sed -i "/^datadir/a binlog_format="mixed"" /etc/my.cnf
sed -i "/^datadir/a server_id=51" /etc/my.cnf
systemctl enable mariadb

# deploy web software
cd
rm -rf /usr/local/nginx/html/*
mkdir websoftcache
tar -xvf $webp -C websoftcache
cd websoftcache
websoftcachen=`ls | wc -l`

if [ $websoftcachen -ne 1 ];then
        cd
        tar -xvf $webp -C /usr/local/nginx/html/
else
        webdir=`ls`
        cd $webdir
        mv * /usr/local/nginx/html/
fi

chown -R php-fpm:php-fpm /usr/local/nginx/html/

# recover database
systemctl start mariadb
cd
mysql -uroot < *.sql &> /dev/null
systemctl stop mariadb

# set the concurrency number accepted by the system to 65535
echo '* soft nofile 65535
* hard nofile 65535' >> /etc/security/limits.conf

# open firewall port
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
systemctl restart firewalld

# move the monitoring scripts that need to be used regularly to the specified location(/root/shell/port_monitoring.sh you need to write it yourself)
mkdir /shell
mv port_monitoring.sh /shell
chmod u+x /shell/*

# set up regular automatic running monitoring script(/root/shell/port_monitoring.sh you need to write it yourself)
echo '*/1 * * * * root /shell/port_monitoring.sh' >> /etc/crontab

# add another user(for data backup)
useradd zhumingyu
mkdir /cache
chown -R zhumingyu:zhumingyu /cache

# delete redundant files
cd
rm -rf *

# restart the server
reboot

[步骤] Linux 时间同步的设置 (通过 NTP 实现)

注意:从 CentOS 7.2&RHEL 7.0 开始实现时间同步的程序默认从 NTP 换成了 Chrony 。 NTP 也可以继续使用,但是需要单独安装 NTP 的安装包。

步骤一:系统环境要求

服务器系统要配置好可用的软件源

步骤二:确保 NTP 已经安装

# yum -y install ntp

步骤三:确保系统会使用 Chrony 或 NTP 的时间同步

# timedatectl set-ntp true
# timedatectl
               Local time: Fri 2020-06-12 09:52:30 EDT
           Universal time: Fri 2020-06-12 13:52:30 UTC
                 RTC time: Fri 2020-06-12 13:51:52
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

(补充:此时 NTP service 的状态可能还不是 active 状态)

步骤四:修改配置文件

# vim /etc/sysconfig/ntp

将部分内容修改如下:

......
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

(说明:在配置文件的末尾添加要进行时间同步的服务器)

步骤五:重启客户端的 NTP 服务

# service ntpd restart

步骤六:显示客户端时间同步的状态
6.1 通过 NTP 命令显示

# ntpq -p


补充:
1) 此命令可能会显示多行,每一行显示一个时钟源的状态
2) 最左边有一个星号 “*” 的行代表目前正在被使用的时钟源,这代表这个时钟源可以被使用且最精确

6.2 通过 timedatectl 命令显示

# timedatectl
               Local time: Fri 2020-06-12 09:52:30 EDT
           Universal time: Fri 2020-06-12 13:52:30 UTC
                 RTC time: Fri 2020-06-12 13:51:52
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

(补充:如果 NTP 时间同步正常,则此时 NTP service 的状态肯定是 active 状态)