[实验] Chrony 客户端的设置 (时间同步)

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

步骤目录:

步骤一:系统环境要求
步骤二:确保 Chrony 已经安装
步骤三:确保系统会使用 Chrony 或 NTP 的时间同步
步骤四:修改配置文件
步骤五:重启客户端的 Chrony 服务
步骤六:显示客户端时间同步的状态

具体的操作步骤:

步骤一:系统环境要求

1) 服务器的系统是 CentOS 8 版本
2) 服务器系统配置好可用的软件源

步骤二:确保 Chrony 已经安装


# yum -y install chrony

步骤三:确保系统会使用 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/chrony.conf

将部分内容修改如下:

......
pool 2.centos.pool.ntp.org iburst

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

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

# systemctl restart chronyd
# systemctl status chronyd

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

# chronyc sources -v
210 Number of sources = 4

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* time.rdg.uk.as44574.net       3   8   373   153    +73ms[  +68ms] +/-  233ms

(补充:当出现 ^* 时则代表时间同步成功)

或者:

# chronyc tracking

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

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

[步骤] NFS 远程共享服务的搭建 (RHEL 7 版) (转载)

RHEL7-部署NFS文件共享服务

一、NFS概述
NFS 是Network File System的缩写,即网络文件系统。
一种使用于分散式文件系统的协定,由Sun公司开发,于1984年向外公布。、

功能是通过网络让不同的机器、不同的操作系统能够彼此分享个别的数据,让应用程序在客户端通过网络访问位于服务器磁盘中的数据,是在类Unix系统间实现磁盘文件共享的一种方法。

NFS 的基本原则是"容许不同的客户端及服务端通过一组RPC分享相同的文件系统",它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享。

NFS在文件传送或信息传送过程中依赖于RPC协议。
RPC,远程过程调用 (Remote Procedure Call) 是能使客户端执行其他系统中程序的一种机制。

NFS本身是没有提供信息传输的协议和功能的,但NFS却能让我们通过网络进行资料的分享,这是因为NFS使用了一些其它的传输协议。而这些传输协议用到这个RPC功能的。可以说NFS本身就是使用RPC的一个程序。或者说NFS也是一个RPC SERVER。所以只要用到NFS的地方都要启动RPC服务,不论是NFS SERVER或者NFS CLIENT。这样SERVER和CLIENT才能通过RPC来实现PROGRAM PORT的对应。可以这么理解RPC和NFS的关系:NFS是一个文件系统,而RPC是负责负责信息的传输。

系统环境

系统平台: CentOS7或RHEL7
NFS Server IP: 192.168.1.63
防火墙已: 关闭或着允许NFS
iptables –F
iptables –X

SELINUX=disabled ==== setenforce 0

安装NFS服务
• nfs-utils :包括基本的NFS命令与监控程序
• rpcbind :支持安全NFS RPC服务的连接

[root@server ~]# yum -y install rpcbind nfs-utils

在这里插入图片描述
NFS系统守护进程
• nfsd:它是基本的NFS守护进程,主要功能是管理客户端是否能够登录服务器;
• mountd:它是RPC安装守护进程,主要功能是管理NFS的文件系统。
当客户端顺利通过nfsd登录NFS服务器后,在使用NFS服务所提供的文件前,还必须通过文件使用权限的验证。它会读取NFS的配置文件/etc/exports来对比客户端权限。
• rpcbind:主要功能是进行端口映射工作。
当客户端尝试连接并使用RPC服务器提供的服务(如NFS服务)时,rpcbind会将所管理的与服务对应的端口提供给客户端,从而使客户可以通过该端口向服务器请求服务。

NFS服务器的配置

NFS服务器的配置相对比较简单,只需要在相应的配置文件中进行设置,然后启动NFS服务器即可。
NFS的常用目录

/etc/exports #NFS服务的主要配置文件
/usr/sbin/exportfs #NFS服务的管理命令
/usr/sbin/showmount #客户端的查看命令
/var/lib/nfs/etab # 记录NFS分享出来的目录的完整权限设定值
/var/lib/nfs/xtab #记录曾经登录过的客户端信息。

NFS服务的配置文件为 /etc/exports,

这个文件是NFS的主要配置文件,不过系统并没有默认值,所以这个文件不一定会存在,可能要使用vim手动建立,然后在文件里面写入配置内容。
/etc/exports文件内容格式:
<输出目录> [客户端1 选项(访问权限,用户映射,其他)] [客户端2 选项(访问权限,用户映射,其他)]
输出目录:
输出目录是指NFS系统中需要共享给客户机使用的目录;
客户端:
客户端是指网络中可以访问这个NFS输出目录的计算机

客户端常用的指定方式
• 指定ip地址的主机:192.168.0.200
• 指定子网中的所有主机:192.168.0.0/24 192.168.0.0/255.255.255.0
• 指定域名的主机:nfs.cnhzz.com
• 指定域中的所有主机:.cnhzz.com
• 所有主机:

选项:
选项用来设置输出目录的访问权限、用户映射等。
NFS主要有3类选项:
访问权限选项
• 设置输出目录只读:ro ReadOnly
• 设置输出目录读写:rw Read and Write
用户映射选项
• all_squash:将远程访问的所有普通用户及所属组都映射为匿名用户或用户组(nfsnobody);
• no_all_squash:与all_squash取反(默认设置);
• root_squash:将root用户及所属组都映射为匿名用户或用户组(默认设置);
• no_root_squash:与rootsquash取反;
• anonuid=xxx:将远程访问的所有用户都映射为匿名用户,并指定该用户为本地用户(UID=xxx);
• anongid=xxx:将远程访问的所有用户组都映射为匿名用户组账户,并指定该匿名用户组账户为本地用户组账户(GID=xxx);
其它选项
• secure:限制客户端只能从小于1024的tcp/ip端口连接nfs服务器(默认设置);
• insecure:允许客户端从大于1024的tcp/ip端口连接服务器;
• sync:将数据同步写入内存缓冲区与磁盘中,效率低,但可以保证数据的一致性;
• async:将数据先保存在内存缓冲区中,必要时才写入磁盘;
• wdelay:检查是否有相关的写操作,如果有则将这些写操作一起执行,这样可以提高效率(默认设置);
• no_wdelay:若有写操作则立即执行,应与sync配合使用;
• subtree:若输出目录是一个子目录,则nfs服务器将检查其父目录的权限(默认设置);
• no_subtree:即使输出目录是一个子目录,nfs服务器也不检查其父目录的权限,这样可以提高效率;

NFS服务器的启动与停止
在对exports文件进行了正确的配置后,就可以启动NFS服务器了。

1、启动停止NFS服务器
为了使NFS服务器能正常工作,需要启动rpcbind和nfs两个服务,并且rpcbind一定要先于nfs启动。

[root@server ~]# systemctl start rpcbind nfs

    1

[root@server ~]# systemctl is-active rpcbind nfs
在这里插入图片描述
[root@server ~]# systemctl stop nfs
[root@停止NFS服务器
server ~]# systemctl stop rpcbind

实践操作
将NFS Server 的/home/test / 共享给192.168.1.0/24网段,权限读写

1.准备测试文件
[root@server ~]# mkdir /home/test
[root@server ~]# cp /etc/passwd /etc/hosts /home/test/
[root@server ~]# ls /home/test/
在这里插入图片描述
2.编辑nfs主配置文件
[root@server ~]# vim /etc/exports
在这里插入图片描述
/home/test 192.168.1.0/24(rw)

3.重启服务
[root@server ~]# systemctl restart
[root@server ~]# systemctl is-active rpcbind nfs
在这里插入图片描述
4.服务器端使用showmount命令查询NFS的共享状态
#查看共享的目录
[root@server ~]# showmount -e 192.168.1.63
在这里插入图片描述
#测试客户端与NFS服务器的共享状态
[root@server0 student]# showmount -e foundation0.ilt.example.com
Export list for foundation0.ilt.example.com:
/content 172.25.0.0/255.255.0.04、

客户端挂载NFS服务器共享的目录
[root@server2 ~]# mount 192.168.1.63:/home/test /mnt/

在这里插入图片描述
在这里插入图片描述
4、NFS的共享权限和访问控制
ls: 无法创建目录: 权限不够
[root@server2 ~]# touch /mnt/a.txt
在这里插入图片描述
是因为NFS 服务器端共享的目录本身的写权限没有开放给其他用户,在服务器端打开该权限.
去NFS服务器上面修改文件权限:
[root@server ~]# chmod 777 -R /home/test
在这里插入图片描述
用root去创建文件,文件的权限编程了nfsnobody用户了
[root@server2 ~]# touch /mnt/nihao.txt
[root@server2 ~]# mkdir /mnt/san
[root@server2 ~]# ll /mnt/
在这里插入图片描述
使用普通用户挂载、写入文件测试。
[root@server2 ~]# useradd lisi
[root@server2 ~]# su - lisi
[lisi@server2 ~]$ touch /mnt/lisi.txt
[lisi@server2 ~]$ touch /mnt/lisi01.txt
[lisi@server2 ~]$ ll /mnt/
在这里插入图片描述
卸载已挂载的NFS共享目录
[root@server2 ~]# umount /mnt/

启动自动挂载 vim /etc/fstab
[root@server2 ~]# echo “192.168.1.63:/home/test /media nfs4 defaults 0 0” >> /etc/fstab
在这里插入图片描述

相关命令
1、exportfs
如果我们在启动了NFS之后又修改了/etc/exports,是不是还要重新启动nfs呢?这个时候我们就可以用exportfs 命令来使改动立刻生效,该命令格式如下:
#exportfs [-aruv]
-a # 全部挂载或卸载 /etc/exports中的内容
-r #重新读取/etc/exports 中的信息,并同步更新/etc/exports、/var/lib/nfs/xtab
-u #卸载单一目录(和-a一起使用为卸载所有/etc/exports文件中的目录)
-v #在export的时候,将详细的信息输出到屏幕上。

具体例子:
[root@server ~]# exportfs -au #卸载所有共享目录
在这里插入图片描述
[root@server ~]# exportfs -rv #重新共享所有目录并输出详细信息
在这里插入图片描述
2、nfsstat
查看NFS的运行状态,对于调整NFS的运行有很大帮助。
在这里插入图片描述
3、rpcinfo
查看rpc执行信息,可以用于检测rpc运行情况的工具,
在这里插入图片描述
利用rpcinfo -p 可以查看出RPC开启的端口所提供的程序有哪些。
在这里插入图片描述
4、showmount
-a 显示已经于客户端连接上的目录信息
-e IP或者hostname 显示此IP地址分享出来的目录
在这里插入图片描述

最后注意两点,虽然通过权限设置可以让普通用户访问,但是挂载的时候默认情况下只有root可以去挂载,普通用户可以执行sudo。
NFS server 关机的时候一点要确保NFS服务关闭,没有客户端处于连接状态!通过showmount -a 可以查看,如果有的话用kill killall pkill 来结束,(-9 强制结束)
————————————————
版权声明:本文为CSDN博主「Gusixsixsix」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Gusixsixsix/java/article/details/82801289

注明:所有转载内容皆直接从被转载文章网页的标题和内容的文本中复制而来

CC 4.0 BY-SA 版权协议网址:https://creativecommons.org/licenses/by-sa/4.0/deed.z
站主补充:
案例一:临时挂载某一个 NFS 目录的方法
# mount -o nolock,nfsvers=3,vers=3 -t nfs 192.168.100.1:/tmp /tmp

案例二:设置一个目录可以同时被两个 NFS 客户端永久挂载的方法
# vim /etc/exports
/tmp 192.168.100.1(rw,no_root_squash,no_subtree_check) 192.168.100.2(rw,no_root_squash,no_subtree_check)

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

介绍:

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

使用方法:
1. 将网站的网页数据备份、网站的 SSL 公钥、网站的 SSL 私钥、网站的数据库备份和本脚本,5 个文件放在同一目录下
2. 如果没有网站的数据库备份则将网页数据备份、网站的 SSL 公钥、网站的 SSL 私钥和本脚本,4 个文件放在同一目录下
3. 在此脚本的分割线内写入相应的内容
4. 服务器都要开启 SELinux
5. 给此脚本添加执行权限
6. 执行此脚本:./<此脚本>

脚本分割线里的变量:
1. webdomain=”eternalcenter.com” #网站的域名,注意不要在前面加任何前缀
2. webtar=”eternalcenter-backup-*.tar.gz”网站的网页数据备份,如果没有这个备份,可以下载一个开源的 WordPress 网页程序
3. webcrt=”eternalcenter.com.crt” #网站 SSL 的公钥,可以自己创建也可以在 FreeSSL 上申请
4. webkey=”eternalcenter.com.key” #网站 SSL 的私钥,可以自己创建也可以在 FreeSSL 上申请
5. sqlbackup=”eternalcenter-backup-*.sql” #网站数据库数据备份,如果没有这个备份(数据库是全量备份),则这里可以为空
6. db=”ec” #网站在数据库中库
7. dbuser=”ec” #网站在数据库中的用户
8. dbuserpw=”eternalcenter” #网站在数据库中的用户密码
9. dbrootpw=”eternalcenter” #数据库的 root 密码

注意:
1. 服务器的系统需要是 CentOS 8 版本
2. 服务器系统要配置好可用的软件源
3. 服务器要能够连接外网

脚本:

#!/bin/bash

####################### Separator ########################
webdomain="eternalcenter.com"
webtar="eternalcenter-backup-*.tar.gz"
webcrt="eternalcenter.com.crt"
webkey="eternalcenter.com.key"
sqlbackup="eternalcenter-backup-*.sql"
db="ec"
dbuser="ec"
dbuserpw="eternalcenter"
dbrootpw="eternalcenter"
####################### Separator ########################

#Determine whether SELinux is on
getenforce | grep Enforcing
if [ $? -ne 0 ];then
	echo "SELinux is not set to enforcing mode and cannot continue"
	exit 2
fi

#Determine whether the required file exists
ls $webtar
if [ $? -ne 0 ];then
	echo "No web page data backup, unable to continue"
	exit 2
fi

ls $webcrt
if [ $? -ne 0 ];then
	echo "Cannot continue without site public key"
	exit 2
fi

ls $webkey
if [ $? -ne 0 ];then
	echo "Unable to continue without site private key"
	exit 2
fi

#Update system
yum clean all
yum repolist
yum makecache
yum -y update

#Make sure the required software is installed
yum -y install tar
yum -y install firewalld

#Deploying Nginx
yum -y install nginx

echo 'worker_processes  1;

events {
    worker_connections  1024;
}

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  60;
    client_body_timeout 20s;
    client_header_timeout 10s;
    send_timeout 30s;

    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      /etc/nginx/ssl/eternalcenter.com.crt;
        ssl_certificate_key  /etc/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;
            fastcgi_index index.php;
            include fastcgi.conf;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html/$fastcgi_script_name;
            include fastcgi_params;
        } 

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

        if (-f $request_filename/index.html){rewrite (.) $1/index.html break;}
        if (-f $request_filename/index.php){rewrite (.) $1/index.php;}
        if (!-f $request_filename){rewrite (.) /index.php;}
        
        }

        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;

}' > /etc/nginx/nginx.conf

sed -i "s/server_name www.eternalcenter.com eternalcenter.com;/server_name www.$webdomain $webdomain;/" /etc/nginx/nginx.conf
sed -i "s@rewrite ^/(.*)$ https://eternalcenter-may-1-2022.github.io/\$1 permanent@rewrite ^/(.*)$ https://$webdomain/\$1 permanent@" /etc/nginx/nginx.conf;
sed -i "s/eternalcenter.com.crt/$webcrt/" /etc/nginx/nginx.conf
sed -i "s/eternalcenter.com.key/$webkey/" /etc/nginx/nginx.conf

mkdir /etc/nginx/ssl
mv $webcrt /etc/nginx/ssl
mv $webkey /etc/nginx/ssl
chcon -t httpd_config_t /etc/nginx/ssl/$webcrt
chcon -t httpd_config_t /etc/nginx/ssl/$webkey
chcon -t httpd_config_t /etc/nginx/ssl/

rm -rf /usr/share/nginx/html/*
tar -xvf $webtar -C /usr/share/nginx/html/ && rm -rf $webtar
chcon -t httpd_sys_content_t -R /usr/share/nginx/html/*

yum -y install sendmail
yum -y install policycoreutils
setsebool -P httpd_can_network_connect 1
setsebool -P httpd_can_network_connect_db 1
setsebool -P httpd_can_sendmail 1
setsebool -P httpd_can_connect_ftp 1
setsebool -P httpd_unified 1
setsebool -P httpd_enable_cgi 1
setsebool -P httpd_builtin_scripting 1
setsebool -P mysql_connect_http 1

systemctl start nginx
systemctl enable nginx

#Deploy MariaDB
yum -y install mariadb mariadb-server

grep "^log_bin=" /etc/my.cnf.d/mariadb-server.cnf
if [ $? -ne 0 ];then
	sed -i '/^datadir/a log_bin=ec' /etc/my.cnf.d/mariadb-server.cnf
fi

grep "^binlog_format=" /etc/my.cnf.d/mariadb-server.cnf
if [ $? -ne 0 ];then
	sed -i '/^datadir/a binlog_format=\"mixed\"' /etc/my.cnf.d/mariadb-server.cnf
fi

grep "^server_id=" /etc/my.cnf.d/mariadb-server.cnf
if [ $? -ne 0 ];then
	sed -i '/^datadir/a server_id=51' /etc/my.cnf.d/mariadb-server.cnf
fi

sed -i 's/^plugin-load-add=auth_gssapi.so/#plugin-load-add=auth_gssapi.so/' /etc/my.cnf.d/auth_gssapi.cnf

sed -i '/^user=.*/d' /etc/my.cnf.d/mariadb-server.cnf
sed -i "/\[mysqld\]/a user=mysql" /etc/my.cnf.d/mariadb-server.cnf

sed -i '/^bind-address=.*/d' /etc/my.cnf.d/mariadb-server.cnf
sed -i "/\[mysqld\]/a bind-address=127.0.0.1" /etc/my.cnf.d/mariadb-server.cnf

chown -R mysql /var/lib/mysql

systemctl start mariadb
systemctl enable mariadb

ls $sqlbackup
if [ $? -ne 0 ];then
        mysql -uroot -e "create database $db;"
        mysql -uroot -e "create user \"$dbuser\"@\"localhost\" identified by \"$dbuserpw\";"
        mysql -uroot -e "grant all privileges on $db.* to \"$dbuser\"@\"localhost\" identified by \"$dbuserpw\";"
        mysql -uroot -e "set password for 'root'@'localhost'=password(\"$dbrootpw\")"
else
        mysql -uroot -e "create database $db;"
        mysql -uroot $db < $sqlbackup
	mysql -uroot -e "create user \"$dbuser\"@\"localhost\" identified by \"$dbuserpw\";"
	mysql -uroot -e "grant all privileges on $db.* to \"$dbuser\"@\"localhost\" identified by \"$dbuserpw\";"
	mysql -uroot -e "set password for 'root'@'localhost'=password(\"$dbrootpw\")"
	rm -rf $sqlbackup
fi
	
systemctl restart mariadb

#Deploy PHP
yum -y install php php-fpm php-mysqlnd php-gd php-mbstring php-opcache php-json php-xml php-xmlrpc php-pecl-zip
useradd php-fpm -s /sbin/nologin
chown -R php-fpm:php-fpm /usr/share/nginx/html/*

sed -i /"^user =.*"/d /etc/php-fpm.conf
sed -i /"^group =.*"/d /etc/php-fpm.conf
sed -i /"^listen =.*"/d /etc/php-fpm.conf
sed -i /"^[www]"/d /etc/php-fpm.conf
sed -i /"^pm = .*"/d /etc/php-fpm.conf
sed -i /"^pm.start_servers = .*"/d /etc/php-fpm.conf
sed -i /"^pm.min_spare_servers = .*"/d /etc/php-fpm.conf
sed -i /"^pm.max_spare_servers = .*"/d /etc/php-fpm.conf
sed -i /"^pm.max_children = .*"/d /etc/php-fpm.conf
sed -i /"^pm.max_requests = .*"/d /etc/php-fpm.conf
sed -i /"^request_terminate_timeout = .*"/d /etc/php-fpm.conf

echo '[www]' >> /etc/php-fpm.conf
echo 'user = php-fpm' >> /etc/php-fpm.conf
echo 'group = php-fpm' >> /etc/php-fpm.conf
echo 'listen = 127.0.0.1:9000' >> /etc/php-fpm.conf
echo 'pm = dynamic' >> /etc/php-fpm.conf
echo 'pm.start_servers = 2' >> /etc/php-fpm.conf
echo 'pm.min_spare_servers = 2' >> /etc/php-fpm.conf
echo 'pm.max_spare_servers = 4' >> /etc/php-fpm.conf
echo 'pm.max_children = 4' >> /etc/php-fpm.conf
echo 'pm.max_requests = 1024' >> /etc/php-fpm.conf
echo 'request_terminate_timeout = 300' >> /etc/php-fpm.conf

systemctl start php-fpm
systemctl enable php-fpm

#Improve system performance
grep "^* soft nofile" /etc/security/limits.conf
if [ $? -ne 0 ];then
	echo '* soft nofile 1024' >> /etc/security/limits.conf
fi

grep "^* hard nofile" /etc/security/limits.conf
if [ $? -ne 0 ];then
	echo '* hard nofile 1024' >> /etc/security/limits.conf
fi

#Open firewall
systemctl start firewalld
systemctl enable firewalld
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
firewall-cmd --reload

#Limit log space
echo "/var/log/mariadb/mariadb.log {
        create 600 mysql mysql
        notifempty
	daily
        rotate 3
        missingok
        compress
    postrotate
	# just if mysqld is really running
        if [ -e /run/mariadb/mariadb.pid ]
        then
           kill -1 $(</run/mariadb/mariadb.pid)
        fi
    endscript
}" > /etc/logrotate.d/mariadb

echo "/var/log/nginx/*log {
    create 0664 nginx root
    size 1024M
    rotate 1
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}" > /etc/logrotate.d/nginx

echo "/var/log/php-fpm/*log {
    size 100M
    rotate 1
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /bin/kill -SIGUSR1 `cat /run/php-fpm/php-fpm.pid 2>/dev/null` 2>/dev/null || true
    endscript
}" > /etc/logrotate.d/php-fpm

echo "/var/log/cron
/var/log/maillog
/var/log/messages
/var/log/secure
/var/log/spooler
{
    size 100M
    rotate 1
    missingok
    sharedscripts
    postrotate
        /usr/bin/systemctl kill -s HUP rsyslog.service >/dev/null 2>&1 || true
    endscript
}" > /etc/logrotate.d/syslog

#Delete this script
scriptwhere=`readlink -f "$0"`
rm -rf $scriptwhere

#Restart the system
reboot

[实验] LNMP 平台的搭建 (openSUSE Leap 15 版)

步骤目录:

步骤一:LNMP 简介

步骤二:系统环境要求

步骤三:搭建 LNMP
3.1 Nginx 网页服务
3.1.1 安装 Nginx 网页服务
3.1.2 配置 Nginx 网页服务的配置文件
3.1.2.1 删除原有的 Nginx 服务的配置文件
3.1.2.2 创建新的 Nginx 网页服务的配置文件
3.1.2.3 配置 Nginx 网页服务的配置文件
3.1.3 启动 nginx 网页服务
3.2 MariaDB 数据库
3.2.1 安装 MariaDB 数据库
3.2.2 启动 MariaDB 数据库
3.3 PHP 环境和连接服务
3.3.1 安装 PHP 环境和连接服务
3.3.2 创建提供 PHP 连接服务的用户
3.3.3 配置 PHP 连接服务的配置文件

步骤四:后续工作

步骤五:测试 LNMP 平台

具体的操作步骤:

步骤一:LNMP 简介

LNMP 是一个实现网站服务的方法,它由 4 样东西组成:
1) Linux 系统
2) Nginx 网页服务
3) MariaDB 数据库
4) PHP 网页程序

步骤二:系统环境要求

1) 服务器的系统需要是 openSUSE 15 版本
2) 服务器要关闭防火墙
3) 服务器系统要配置好可用的软件源(最好是软件数量最多的官方版本)

步骤三:搭建 LNMP
3.1 Nginx 网页服务
3.1.1 安装 Nginx 网页服务

# zypper -n install nginx

3.1.2 配置 Nginx 网页服务的配置文件
3.1.2.1 删除原有的 Nginx 服务的配置文件

# rm /etc/nginx/nginx.conf

3.1.2.2 创建新的 Nginx 网页服务的配置文件

# cp /etc/nginx/nginx.conf.default /etc/nginx.conf

3.1.2.3 配置 Nginx 网页服务的配置文件

# vi /etc/nginx/nginx.conf

将其中的:

......
        location / {
            root   html;
            index  index.html index.htm;
        }
......
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}
......

修改为:

......
        location / {
            root   html;
            index  index.php index.html index.htm;
        }
......
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
        #   fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            include        fastcgi.conf;
        }
......

(补充:这里以让 Nginx 将对于 PHP 的请求传递到本机的 9000 端口为例)

3.1.3 启动 Nginx 网页服务

# systemctl start nginx

3.2 MariaDB 数据库
3.2.1 安装 MariaDB 数据库

# zypper -n install mariadb mariadb-server

3.2.2 启动 MariaDB 数据库

# systemctl start mariadb

3.3 PHP 环境和连接服务
3.3.1 安装 PHP 环境和连接服务

# zypper -n install php7 php7-fpm php7-mysql php7-gd php7-mbstring php7-opcache php7-json php7-xmlrpc php7-zlib

3.3.2 创建提供 PHP 连接服务的用户

# useradd php-fpm -s /sbin/nologin

3.3.3 配置 PHP 连接服务的配置文件

# vi /etc/php-fpm.conf

将以下内容:

......
user = nouser
group = nouser
......

修改为:

......
user = php-fpm
group = users
listen = 127.0.0.1:9000
......


补充:这里以
1) 以 php-fpm 用户和 users 用户组的身份启动 php-fpm
2) 让 php-fpm 监听本地 9000 端口为例

步骤四:后续工作

1) 给 MariaDB 数据库设置用于存储网页数据的用户和密码
2) 将 PHP 网页程序放到 Nginx 的网页目录下(/srv/www/htdocs)
3) 给 PHP 网页程序设置用于连接 MariaDB 数据库的用户和密码

步骤五:测试 LNMP 平台

使用浏览器访问服务器 IP 地址就可以看到对应 PHP 网页了