简体中文 繁體中文 English 日本語 Deutsch 한국 사람 بالعربية TÜRKÇE português คนไทย Français

站内搜索

搜索

活动公告

11-02 12:46
10-23 09:32
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,将及时处理!
10-23 09:31
10-23 09:28
通知:签到时间调整为每日4:00(东八区)
10-23 09:26

使用openSUSE Tumbleweed轻松搭建高效稳定的网络服务指南从基础配置到安全优化全方位解析助您快速掌握企业级应用

3万

主题

423

科技点

3万

积分

大区版主

木柜子打湿

积分
31916

三倍冰淇淋无人之境【一阶】财Doro小樱(小丑装)立华奏以外的星空【二阶】⑨的冰沙

发表于 2025-9-28 10:20:00 | 显示全部楼层 |阅读模式 [标记阅至此楼]

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

openSUSE Tumbleweed作为一款滚动发布的Linux发行版,以其稳定性、安全性和最新软件包而闻名,是搭建企业级网络服务的理想选择。本文将详细介绍如何使用openSUSE Tumbleweed从零开始构建高效、稳定且安全的网络服务环境,涵盖基础配置、服务部署、性能优化和安全加固等方面,帮助您快速掌握企业级应用搭建技能。

1. openSUSE Tumbleweed系统安装与基础配置

1.1 系统安装准备

在开始安装openSUSE Tumbleweed之前,需要做好以下准备工作:

1. 下载最新的openSUSE Tumbleweed ISO镜像文件:wget https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-DVD-x86_64-Current.iso
2. 创建启动U盘或光盘:# 在Linux系统下使用dd命令创建启动U盘
sudo dd if=openSUSE-Tumbleweed-DVD-x86_64-Current.iso of=/dev/sdX bs=4M status=progress
3. 确保服务器硬件满足最低要求:CPU: 2核或以上内存: 4GB或以上硬盘: 40GB或以上可用空间
4. CPU: 2核或以上
5. 内存: 4GB或以上
6. 硬盘: 40GB或以上可用空间

下载最新的openSUSE Tumbleweed ISO镜像文件:
  1. wget https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-DVD-x86_64-Current.iso
复制代码

创建启动U盘或光盘:
  1. # 在Linux系统下使用dd命令创建启动U盘
  2. sudo dd if=openSUSE-Tumbleweed-DVD-x86_64-Current.iso of=/dev/sdX bs=4M status=progress
复制代码

确保服务器硬件满足最低要求:

• CPU: 2核或以上
• 内存: 4GB或以上
• 硬盘: 40GB或以上可用空间

1.2 系统安装过程

1. 从启动介质引导服务器,选择”Installation”选项开始安装。
2. 在安装过程中:选择语言和键盘布局选择”Server”作为基础系统角色配置磁盘分区(建议使用LVM以便后续扩展)设置网络配置(静态IP或DHCP)创建管理员账户和密码
3. 选择语言和键盘布局
4. 选择”Server”作为基础系统角色
5. 配置磁盘分区(建议使用LVM以便后续扩展)
6. 设置网络配置(静态IP或DHCP)
7. 创建管理员账户和密码
8. 完成安装后重启系统。

从启动介质引导服务器,选择”Installation”选项开始安装。

在安装过程中:

• 选择语言和键盘布局
• 选择”Server”作为基础系统角色
• 配置磁盘分区(建议使用LVM以便后续扩展)
• 设置网络配置(静态IP或DHCP)
• 创建管理员账户和密码

完成安装后重启系统。

1.3 系统基础配置

安装完成后,首先更新系统到最新状态:
  1. # 刷新软件包仓库
  2. sudo zypper refresh
  3. # 更新所有软件包
  4. sudo zypper update --no-recommends
  5. # 如果内核更新,重启系统
  6. sudo reboot
复制代码

编辑网络配置文件,设置静态IP地址:
  1. # 查看网络接口名称
  2. ip addr
  3. # 编辑网络配置文件
  4. sudo nano /etc/sysconfig/network/ifcfg-eth0
复制代码

在配置文件中添加以下内容:
  1. BOOTPROTO='static'
  2. IPADDR='192.168.1.100/24'
  3. GATEWAY='192.168.1.1'
  4. DNS1='8.8.8.8'
  5. DNS2='8.8.4.4'
  6. STARTMODE='auto'
复制代码

重启网络服务:
  1. sudo systemctl restart network
复制代码
  1. # 设置主机名
  2. sudo hostnamectl set-hostname server.example.com
  3. # 编辑hosts文件
  4. sudo nano /etc/hosts
复制代码

在hosts文件中添加:
  1. 127.0.0.1   localhost
  2. 192.168.1.100   server.example.com   server
复制代码

openSUSE使用Firewalld作为防火墙管理工具:
  1. # 启动并启用防火墙
  2. sudo systemctl start firewalld
  3. sudo systemctl enable firewalld
  4. # 查看防火墙状态
  5. sudo firewall-cmd --state
  6. # 开放SSH端口(默认22)
  7. sudo firewall-cmd --permanent --add-service=ssh
  8. sudo firewall-cmd --reload
复制代码

为了安全起见,建议修改SSH默认配置:
  1. # 编辑SSH配置文件
  2. sudo nano /etc/ssh/sshd_config
复制代码

修改以下配置项:
  1. # 禁止root登录
  2. PermitRootLogin no
  3. # 更改默认端口
  4. Port 2222
  5. # 仅允许特定用户登录
  6. AllowUsers adminuser
  7. # 禁用密码认证,使用密钥认证
  8. PasswordAuthentication no
  9. PubkeyAuthentication yes
复制代码

重启SSH服务:
  1. sudo systemctl restart sshd
复制代码

2. 网络服务搭建

2.1 Web服务器搭建(Apache/Nginx)
  1. # 安装Apache
  2. sudo zypper install apache2
  3. # 启动并启用Apache服务
  4. sudo systemctl start apache2
  5. sudo systemctl enable apache2
  6. # 检查Apache状态
  7. sudo systemctl status apache2
复制代码

配置Apache虚拟主机:
  1. # 创建网站目录
  2. sudo mkdir -p /srv/www/example.com/public_html
  3. sudo chown -R wwwrun:www /srv/www/example.com/public_html
  4. # 创建虚拟主机配置文件
  5. sudo nano /etc/apache2/vhosts.d/example.com.conf
复制代码

添加以下配置:
  1. <VirtualHost *:80>
  2.     ServerName example.com
  3.     ServerAlias www.example.com
  4.     DocumentRoot /srv/www/example.com/public_html
  5.     ErrorLog /var/log/apache2/example.com-error.log
  6.     CustomLog /var/log/apache2/example.com-access.log combined
  7. </VirtualHost>
复制代码

启用配置并重启Apache:
  1. # 启用配置
  2. sudo a2enmod vhost_alias
  3. sudo a2ensite example.com
  4. # 重启Apache
  5. sudo systemctl restart apache2
复制代码
  1. # 安装Nginx
  2. sudo zypper install nginx
  3. # 启动并启用Nginx服务
  4. sudo systemctl start nginx
  5. sudo systemctl enable nginx
  6. # 检查Nginx状态
  7. sudo systemctl status nginx
复制代码

配置Nginx虚拟主机:
  1. # 创建网站目录
  2. sudo mkdir -p /srv/www/example.com/public_html
  3. sudo chown -R wwwrun:www /srv/www/example.com/public_html
  4. # 创建虚拟主机配置文件
  5. sudo nano /etc/nginx/vhosts.d/example.com.conf
复制代码

添加以下配置:
  1. server {
  2.     listen 80;
  3.     server_name example.com www.example.com;
  4.     root /srv/www/example.com/public_html;
  5.     index index.html index.htm;
  6.     location / {
  7.         try_files $uri $uri/ =404;
  8.     }
  9.     access_log /var/log/nginx/example.com-access.log;
  10.     error_log /var/log/nginx/example.com-error.log;
  11. }
复制代码

测试配置并重启Nginx:
  1. # 测试配置
  2. sudo nginx -t
  3. # 重启Nginx
  4. sudo systemctl restart nginx
复制代码

2.2 数据库服务器搭建(MariaDB/MySQL)
  1. # 安装MariaDB服务器和客户端
  2. sudo zypper install mariadb mariadb-client
  3. # 启动并启用MariaDB服务
  4. sudo systemctl start mariadb
  5. sudo systemctl enable mariadb
  6. # 运行安全安装脚本
  7. sudo mysql_secure_installation
复制代码

创建数据库和用户:
  1. # 登录MariaDB
  2. mysql -u root -p
  3. # 创建数据库
  4. CREATE DATABASE exampledb;
  5. # 创建用户并授权
  6. CREATE USER 'exampleuser'@'localhost' IDENTIFIED BY 'password';
  7. GRANT ALL PRIVILEGES ON exampledb.* TO 'exampleuser'@'localhost';
  8. FLUSH PRIVILEGES;
  9. EXIT;
复制代码
  1. # 添加MySQL仓库
  2. sudo zypper addrepo https://dev.mysql.com/get/mysql-zypper-repo.noarch.rpm
  3. # 刷新仓库
  4. sudo zypper refresh
  5. # 安装MySQL服务器
  6. sudo zypper install mysql-community-server
  7. # 启动并启用MySQL服务
  8. sudo systemctl start mysql
  9. sudo systemctl enable mysql
  10. # 运行安全安装脚本
  11. sudo mysql_secure_installation
复制代码

2.3 PHP环境搭建
  1. # 安装PHP及常用扩展
  2. sudo zypper install php8 php8-fpm php8-mysql php8-gd php8-mbstring php8-xml php8-curl php8-zip
  3. # 启动并启用PHP-FPM服务
  4. sudo systemctl start php-fpm
  5. sudo systemctl enable php-fpm
复制代码

配置Nginx使用PHP-FPM:
  1. # 编辑Nginx配置
  2. sudo nano /etc/nginx/nginx.conf
复制代码

在http块中添加:
  1. upstream php-handler {
  2.     server unix:/run/php-fpm/php-fpm.sock;
  3. }
复制代码

编辑虚拟主机配置:
  1. sudo nano /etc/nginx/vhosts.d/example.com.conf
复制代码

修改配置以支持PHP:
  1. server {
  2.     listen 80;
  3.     server_name example.com www.example.com;
  4.     root /srv/www/example.com/public_html;
  5.     index index.php index.html index.htm;
  6.     location / {
  7.         try_files $uri $uri/ =404;
  8.     }
  9.     location ~ \.php$ {
  10.         fastcgi_pass php-handler;
  11.         fastcgi_index index.php;
  12.         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  13.         include fastcgi_params;
  14.     }
  15.     access_log /var/log/nginx/example.com-access.log;
  16.     error_log /var/log/nginx/example.com-error.log;
  17. }
复制代码

重启Nginx和PHP-FPM:
  1. sudo systemctl restart nginx
  2. sudo systemctl restart php-fpm
复制代码

2.4 文件服务器搭建(NFS/Samba)
  1. # 安装NFS服务器
  2. sudo zypper install nfs-kernel-server
  3. # 创建共享目录
  4. sudo mkdir -p /srv/nfs/share
  5. sudo chown nobody:nogroup /srv/nfs/share
  6. sudo chmod 777 /srv/nfs/share
  7. # 配置NFS共享
  8. sudo nano /etc/exports
复制代码

添加以下内容:
  1. /srv/nfs/share 192.168.1.0/24(rw,sync,no_subtree_check)
复制代码

启动并启用NFS服务:
  1. sudo systemctl start nfs-server
  2. sudo systemctl enable nfs-server
  3. # 更新NFS共享表
  4. sudo exportfs -a
复制代码

配置防火墙允许NFS:
  1. sudo firewall-cmd --permanent --add-service=nfs
  2. sudo firewall-cmd --permanent --add-service=mountd
  3. sudo firewall-cmd --permanent --add-service=rpc-bind
  4. sudo firewall-cmd --reload
复制代码
  1. # 安装Samba
  2. sudo zypper install samba
  3. # 创建共享目录
  4. sudo mkdir -p /srv/samba/share
  5. sudo chown nobody:nogroup /srv/samba/share
  6. sudo chmod 777 /srv/samba/share
  7. # 配置Samba
  8. sudo nano /etc/samba/smb.conf
复制代码

添加以下配置:
  1. [global]
  2.    workgroup = WORKGROUP
  3.    server string = Samba Server
  4.    security = user
  5.    map to guest = bad user
  6.    dns proxy = no
  7. [Share]
  8.    path = /srv/samba/share
  9.    browsable = yes
  10.    writable = yes
  11.    guest ok = yes
  12.    read only = no
复制代码

启动并启用Samba服务:
  1. sudo systemctl start nmb smb
  2. sudo systemctl enable nmb smb
复制代码

配置防火墙允许Samba:
  1. sudo firewall-cmd --permanent --add-service=samba
  2. sudo firewall-cmd --reload
复制代码

2.5 邮件服务器搭建(Postfix/Dovecot)
  1. # 安装Postfix
  2. sudo zypper install postfix
  3. # 配置Postfix
  4. sudo nano /etc/postfix/main.cf
复制代码

修改以下配置:
  1. myhostname = mail.example.com
  2. mydomain = example.com
  3. myorigin = $mydomain
  4. inet_interfaces = all
  5. mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
  6. mynetworks = 127.0.0.0/8, 192.168.1.0/24
  7. home_mailbox = Maildir/
复制代码

启动并启用Postfix:
  1. sudo systemctl start postfix
  2. sudo systemctl enable postfix
复制代码
  1. # 安装Dovecot
  2. sudo zypper install dovecot dovecot-imapd dovecot-pop3d
  3. # 配置Dovecot
  4. sudo nano /etc/dovecot/dovecot.conf
复制代码

确保以下配置正确:
  1. protocols = imap pop3 lmtp
  2. listen = *
复制代码

配置认证:
  1. sudo nano /etc/dovecot/conf.d/10-auth.conf
复制代码

修改以下配置:
  1. disable_plaintext_auth = no
  2. auth_mechanisms = plain login
复制代码

配置邮件位置:
  1. sudo nano /etc/dovecot/conf.d/10-mail.conf
复制代码

修改以下配置:
  1. mail_location = maildir:~/Maildir
复制代码

启动并启用Dovecot:
  1. sudo systemctl start dovecot
  2. sudo systemctl enable dovecot
复制代码

配置防火墙允许邮件服务:
  1. sudo firewall-cmd --permanent --add-service=smtp
  2. sudo firewall-cmd --permanent --add-service=pop3
  3. sudo firewall-cmd --permanent --add-service=imap
  4. sudo firewall-cmd --permanent --add-service=smtps
  5. sudo firewall-cmd --permanent --add-service=pop3s
  6. sudo firewall-cmd --permanent --add-service=imaps
  7. sudo firewall-cmd --reload
复制代码

3. 服务优化与性能调优

3.1 系统性能优化

编辑sysctl配置文件:
  1. sudo nano /etc/sysctl.d/99-server-tuning.conf
复制代码

添加以下内容:
  1. # 增加文件描述符限制
  2. fs.file-max = 100000
  3. # 网络调优
  4. net.core.rmem_max = 16777216
  5. net.core.wmem_max = 16777216
  6. net.ipv4.tcp_rmem = 4096 87380 16777216
  7. net.ipv4.tcp_wmem = 4096 65536 16777216
  8. net.ipv4.tcp_fin_timeout = 30
  9. net.ipv4.tcp_keepalive_time = 1200
  10. net.ipv4.tcp_max_syn_backlog = 65536
  11. net.core.netdev_max_backlog = 65536
  12. net.ipv4.tcp_max_tw_buckets = 1440000
  13. net.ipv4.tcp_tw_reuse = 1
  14. net.ipv4.tcp_tw_recycle = 0
  15. net.ipv4.tcp_syncookies = 1
  16. net.ipv4.tcp_synack_retries = 2
  17. net.ipv4.tcp_syn_retries = 2
  18. # 虚拟内存调优
  19. vm.swappiness = 10
  20. vm.dirty_ratio = 60
  21. vm.dirty_background_ratio = 2
复制代码

应用配置:
  1. sudo sysctl -p /etc/sysctl.d/99-server-tuning.conf
复制代码

为提高文件系统性能,可以调整挂载选项:
  1. # 编辑fstab文件
  2. sudo nano /etc/fstab
复制代码

修改根分区挂载选项,添加noatime和nodiratime:
  1. UUID=xxxx-xxxx   /   ext4   defaults,noatime,nodiratime   0   1
复制代码

重新挂载文件系统:
  1. sudo mount -o remount /
复制代码

3.2 Web服务器性能优化

编辑Apache配置文件:
  1. sudo nano /etc/apache2/server-tuning.conf
复制代码

修改以下参数:
  1. <IfModule prefork.c>
  2.     StartServers          5
  3.     MinSpareServers       5
  4.     MaxSpareServers      10
  5.     MaxClients          150
  6.     MaxRequestsPerChild  0
  7. </IfModule>
  8. <IfModule worker.c>
  9.     StartServers          2
  10.     MaxClients          150
  11.     MinSpareThreads      25
  12.     MaxSpareThreads      75
  13.     ThreadsPerChild      25
  14.     MaxRequestsPerChild  0
  15. </IfModule>
  16. KeepAlive On
  17. KeepAliveTimeout 5
  18. MaxKeepAliveRequests 100
复制代码

启用Apache缓存模块:
  1. sudo a2enmod cache
  2. sudo a2enmod cache_disk
  3. sudo a2enmod expires
  4. sudo a2enmod headers
  5. sudo a2enmod deflate
复制代码

配置缓存:
  1. sudo nano /etc/apache2/conf.d/cache.conf
复制代码

添加以下内容:
  1. <IfModule mod_cache.c>
  2.     CacheEnable disk /
  3.     CacheRoot /var/cache/apache2/mod_cache_disk
  4.     CacheDirLevels 2
  5.     CacheDirLength 1
  6.     CacheDefaultExpire 3600
  7.     CacheMaxFileSize 1000000
  8.     CacheMinFileSize 1
  9. </IfModule>
  10. <IfModule mod_expires.c>
  11.     ExpiresActive On
  12.     ExpiresByType image/jpg "access plus 1 year"
  13.     ExpiresByType image/jpeg "access plus 1 year"
  14.     ExpiresByType image/gif "access plus 1 year"
  15.     ExpiresByType image/png "access plus 1 year"
  16.     ExpiresByType text/css "access plus 1 month"
  17.     ExpiresByType application/pdf "access plus 1 month"
  18.     ExpiresByType text/x-javascript "access plus 1 month"
  19.     ExpiresByType application/x-shockwave-flash "access plus 1 month"
  20.     ExpiresByType image/x-icon "access plus 1 year"
  21.     ExpiresDefault "access plus 2 days"
  22. </IfModule>
  23. <IfModule mod_deflate.c>
  24.     AddOutputFilterByType DEFLATE text/plain
  25.     AddOutputFilterByType DEFLATE text/html
  26.     AddOutputFilterByType DEFLATE text/xml
  27.     AddOutputFilterByType DEFLATE text/css
  28.     AddOutputFilterByType DEFLATE application/xml
  29.     AddOutputFilterByType DEFLATE application/xhtml+xml
  30.     AddOutputFilterByType DEFLATE application/rss+xml
  31.     AddOutputFilterByType DEFLATE application/javascript
  32.     AddOutputFilterByType DEFLATE application/x-javascript
  33. </IfModule>
复制代码

重启Apache:
  1. sudo systemctl restart apache2
复制代码

编辑Nginx主配置文件:
  1. sudo nano /etc/nginx/nginx.conf
复制代码

修改以下参数:
  1. user wwwrun;
  2. worker_processes auto;
  3. worker_rlimit_nofile 100000;
  4. events {
  5.     worker_connections 4096;
  6.     use epoll;
  7.     multi_accept on;
  8. }
  9. http {
  10.     # 基本设置
  11.     sendfile on;
  12.     tcp_nopush on;
  13.     tcp_nodelay on;
  14.     keepalive_timeout 30;
  15.     keepalive_requests 100000;
  16.     reset_timedout_connection on;
  17.     client_body_timeout 10;
  18.     send_timeout 2;
  19.    
  20.     # 缓冲区设置
  21.     client_body_buffer_size 128k;
  22.     client_max_body_size 10m;
  23.     client_header_buffer_size 1k;
  24.     large_client_header_buffers 4 4k;
  25.     output_buffers 1 32k;
  26.     postpone_output 1460;
  27.    
  28.     # Gzip压缩
  29.     gzip on;
  30.     gzip_min_length 10240;
  31.     gzip_proxied expired no-cache no-store private auth;
  32.     gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
  33.     gzip_disable "MSIE [1-6]\.";
  34.    
  35.     # 缓存设置
  36.     open_file_cache max=200000 inactive=20s;
  37.     open_file_cache_valid 30s;
  38.     open_file_cache_min_uses 2;
  39.     open_file_cache_errors on;
  40. }
复制代码

重启Nginx:
  1. sudo systemctl restart nginx
复制代码

3.3 数据库性能优化

编辑MariaDB/MySQL配置文件:
  1. sudo nano /etc/my.cnf
复制代码

添加以下内容:
  1. [mysqld]
  2. # 基本设置
  3. character-set-server = utf8mb4
  4. collation-server = utf8mb4_unicode_ci
  5. default-storage-engine = InnoDB
  6. # 内存设置
  7. innodb_buffer_pool_size = 2G
  8. innodb_buffer_pool_instances = 2
  9. innodb_log_file_size = 256M
  10. innodb_log_buffer_size = 8M
  11. innodb_flush_log_at_trx_commit = 2
  12. innodb_flush_method = O_DIRECT
  13. innodb_file_per_table = 1
  14. key_buffer_size = 256M
  15. max_allowed_packet = 16M
  16. thread_cache_size = 16
  17. table_open_cache = 2000
  18. query_cache_type = 1
  19. query_cache_size = 128M
  20. query_cache_limit = 2M
  21. # 连接设置
  22. max_connections = 200
  23. max_connect_errors = 100000
  24. wait_timeout = 300
  25. interactive_timeout = 300
  26. # 其他设置
  27. skip-name-resolve
  28. sync_binlog = 0
  29. slow_query_log = 1
  30. slow_query_log_file = /var/log/mysql/slow.log
  31. long_query_time = 2
复制代码

重启MariaDB/MySQL:
  1. sudo systemctl restart mariadb
复制代码

3.4 PHP性能优化

编辑PHP-FPM配置文件:
  1. sudo nano /etc/php8/php-fpm.d/www.conf
复制代码

修改以下参数:
  1. pm = dynamic
  2. pm.max_children = 100
  3. pm.start_servers = 20
  4. pm.min_spare_servers = 10
  5. pm.max_spare_servers = 30
  6. pm.max_requests = 1000
复制代码

编辑PHP配置文件:
  1. sudo nano /etc/php8/php.ini
复制代码

修改以下参数:
  1. memory_limit = 256M
  2. max_execution_time = 300
  3. max_input_time = 300
  4. upload_max_filesize = 64M
  5. post_max_size = 64M
  6. max_file_uploads = 20
  7. realpath_cache_size = 4096k
  8. realpath_cache_ttl = 120
  9. opcache.enable = 1
  10. opcache.memory_consumption = 128
  11. opcache.interned_strings_buffer = 8
  12. opcache.max_accelerated_files = 4000
  13. opcache.revalidate_freq = 60
  14. opcache.fast_shutdown = 1
  15. opcache.enable_file_override = 0
  16. opcache.validate_timestamps = 1
复制代码

重启PHP-FPM:
  1. sudo systemctl restart php-fpm
复制代码

4. 安全加固与防护

4.1 系统安全加固

创建管理员用户并禁用root远程登录:
  1. # 创建新用户
  2. sudo useradd -m -G wheel adminuser
  3. sudo passwd adminuser
  4. # 禁用root远程SSH登录
  5. sudo nano /etc/ssh/sshd_config
复制代码

修改以下配置:
  1. PermitRootLogin no
复制代码

重启SSH服务:
  1. sudo systemctl restart sshd
复制代码

编辑sudoers文件:
  1. sudo visudo
复制代码

添加以下内容:
  1. # 允许wheel组成员使用sudo
  2. %wheel ALL=(ALL) ALL
  3. # 允许adminuser无需密码使用sudo
  4. adminuser ALL=(ALL) NOPASSWD: ALL
复制代码

安装并配置自动更新:
  1. # 安装自动更新工具
  2. sudo zypper install zypper-automatic
  3. # 配置自动更新
  4. sudo nano /etc/zypp/zypper-automatic.conf
复制代码

修改以下配置:
  1. [Main]
  2. UpdateInterval = daily
  3. RandomizeDelay = 60
  4. UpdateType = security
复制代码

启用并启动自动更新服务:
  1. sudo systemctl enable --now zypper-automatic.timer
复制代码

4.2 防火墙配置

配置更严格的防火墙规则:
  1. # 创建新的防火墙区域
  2. sudo firewall-cmd --permanent --new-zone=publicweb
  3. # 设置默认策略
  4. sudo firewall-cmd --permanent --zone=publicweb --set-target=DROP
  5. # 开放必要端口
  6. sudo firewall-cmd --permanent --zone=publicweb --add-service=http
  7. sudo firewall-cmd --permanent --zone=publicweb --add-service=https
  8. sudo firewall-cmd --permanent --zone=publicweb --add-service=ssh
  9. # 将网络接口分配到新区域
  10. sudo firewall-cmd --permanent --zone=publicweb --change-interface=eth0
  11. # 重新加载防火墙配置
  12. sudo firewall-cmd --reload
复制代码

如果需要配置端口转发:
  1. # 启用IP伪装(NAT)
  2. sudo firewall-cmd --permanent --zone=publicweb --add-masquerade
  3. # 配置端口转发
  4. sudo firewall-cmd --permanent --zone=publicweb --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100
  5. # 重新加载防火墙配置
  6. sudo firewall-cmd --reload
复制代码

4.3 Fail2Ban防护
  1. # 安装Fail2Ban
  2. sudo zypper install fail2ban
  3. # 创建本地配置文件
  4. sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  5. # 编辑配置文件
  6. sudo nano /etc/fail2ban/jail.local
复制代码

修改以下配置:
  1. [DEFAULT]
  2. bantime = 1h
  3. findtime = 10m
  4. maxretry = 3
  5. [sshd]
  6. enabled = true
  7. port = 2222
  8. logpath = %(sshd_log)s
  9. maxretry = 3
  10. bantime = 1d
  11. [apache-auth]
  12. enabled = true
  13. port = http,https
  14. logpath = %(apache_error_log)s
  15. maxretry = 3
  16. bantime = 1d
  17. [nginx-http-auth]
  18. enabled = true
  19. port = http,https
  20. logpath = %(nginx_error_log)s
  21. maxretry = 3
  22. bantime = 1d
复制代码

启动并启用Fail2Ban:
  1. sudo systemctl start fail2ban
  2. sudo systemctl enable fail2ban
复制代码

4.4 SSL/TLS安全配置

使用Let’s Encrypt获取免费SSL证书:
  1. # 安装Certbot
  2. sudo zypper install certbot
  3. # 获取证书
  4. sudo certbot certonly --webroot -w /srv/www/example.com/public_html -d example.com -d www.example.com
复制代码

启用SSL模块:
  1. sudo a2enmod ssl
复制代码

创建SSL配置文件:
  1. sudo nano /etc/apache2/vhosts.d/example.com-ssl.conf
复制代码

添加以下内容:
  1. <IfModule mod_ssl.c>
  2. <VirtualHost *:443>
  3.     ServerName example.com
  4.     ServerAlias www.example.com
  5.     DocumentRoot /srv/www/example.com/public_html
  6.     ErrorLog /var/log/apache2/example.com-ssl-error.log
  7.     CustomLog /var/log/apache2/example.com-ssl-access.log combined
  8.    
  9.     SSLEngine on
  10.     SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
  11.     SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
  12.    
  13.     # 安全配置
  14.     SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
  15.     SSLCipherSuite HIGH:!aNULL:!MD5
  16.     SSLHonorCipherOrder on
  17.     SSLCompression off
  18.     SSLSessionTickets off
  19.    
  20.     # HSTS
  21.     Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
  22. </VirtualHost>
  23. </IfModule>
复制代码

重启Apache:
  1. sudo systemctl restart apache2
复制代码

编辑Nginx虚拟主机配置:
  1. sudo nano /etc/nginx/vhosts.d/example.com.conf
复制代码

添加以下内容:
  1. server {
  2.     listen 80;
  3.     server_name example.com www.example.com;
  4.     return 301 https://$host$request_uri;
  5. }
  6. server {
  7.     listen 443 ssl http2;
  8.     server_name example.com www.example.com;
  9.     root /srv/www/example.com/public_html;
  10.     index index.php index.html index.htm;
  11.    
  12.     ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
  13.     ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
  14.    
  15.     # 安全配置
  16.     ssl_protocols TLSv1.2 TLSv1.3;
  17.     ssl_prefer_server_ciphers on;
  18.     ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
  19.     ssl_session_timeout 1d;
  20.     ssl_session_cache shared:SSL:50m;
  21.     ssl_session_tickets off;
  22.     ssl_stapling on;
  23.     ssl_stapling_verify on;
  24.    
  25.     # HSTS
  26.     add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
  27.    
  28.     location / {
  29.         try_files $uri $uri/ =404;
  30.     }
  31.    
  32.     location ~ \.php$ {
  33.         fastcgi_pass php-handler;
  34.         fastcgi_index index.php;
  35.         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  36.         include fastcgi_params;
  37.     }
  38.    
  39.     access_log /var/log/nginx/example.com-ssl-access.log;
  40.     error_log /var/log/nginx/example.com-ssl-error.log;
  41. }
复制代码

重启Nginx:
  1. sudo systemctl restart nginx
复制代码

4.5 安全扫描与审计

Lynis是一款强大的安全审计工具:
  1. # 安装Lynis
  2. sudo zypper install lynis
  3. # 运行安全审计
  4. sudo lynis audit system
  5. # 查看报告
  6. sudo cat /var/log/lynis-report.dat
复制代码

安装并配置auditd:
  1. # 安装auditd
  2. sudo zypper install auditd
  3. # 启动并启用auditd
  4. sudo systemctl start auditd
  5. sudo systemctl enable auditd
  6. # 添加审计规则
  7. sudo nano /etc/audit/rules.d/audit.rules
复制代码

添加以下内容:
  1. # 监控文件变更
  2. -w /etc/passwd -p wa -k identity
  3. -w /etc/group -p wa -k identity
  4. -w /etc/shadow -p wa -k identity
  5. -w /etc/sudoers -p wa -k identity
  6. -w /etc/ssh/sshd_config -p wa -k sshd_config
  7. # 监控系统调用
  8. -a always,exit -F arch=b64 -S chmod,fchmod,fchmodat -F auid>=1000 -F auid!=4294967295 -k perm_mod
  9. -a always,exit -F arch=b64 -S chown,fchown,lchown,fchownat -F auid>=1000 -F auid!=4294967295 -k perm_mod
复制代码

重新加载审计规则:
  1. sudo systemctl restart auditd
  2. sudo auditctl -R /etc/audit/rules.d/audit.rules
复制代码

5. 监控与维护

5.1 系统监控
  1. # 添加Zabbix仓库
  2. sudo rpm -Uvh https://repo.zabbix.com/zabbix/5.0/sles/15/x86_64/zabbix-release-5.0-1.sles15.noarch.rpm
  3. # 刷新仓库
  4. sudo zypper refresh
  5. # 安装Zabbix服务器、前端和代理
  6. sudo zypper install zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-agent
  7. # 创建数据库和用户
  8. mysql -u root -p
  9. CREATE DATABASE zabbix CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
  10. CREATE USER zabbix@localhost IDENTIFIED BY 'password';
  11. GRANT ALL PRIVILEGES ON zabbix.* TO zabbix@localhost;
  12. EXIT;
  13. # 导入初始架构和数据
  14. zcat /usr/share/doc/packages/zabbix-server-mysql/create.sql.gz | mysql -uzabbix -p zabbix
  15. # 配置Zabbix服务器
  16. sudo nano /etc/zabbix/zabbix_server.conf
复制代码

修改以下配置:
  1. DBHost=localhost
  2. DBName=zabbix
  3. DBUser=zabbix
  4. DBPassword=password
复制代码

配置PHP前端:
  1. sudo nano /etc/php7/php.ini
复制代码

修改以下配置:
  1. max_execution_time = 300
  2. memory_limit = 128M
  3. post_max_size = 16M
  4. upload_max_filesize = 2M
  5. max_input_time = 300
  6. date.timezone = Asia/Shanghai
复制代码

启动并启用Zabbix服务:
  1. sudo systemctl restart zabbix-server zabbix-agent apache2
  2. sudo systemctl enable zabbix-server zabbix-agent apache2
复制代码

访问http://server.example.com/zabbix完成Zabbix前端安装。
  1. # 安装Prometheus
  2. sudo zypper install prometheus prometheus-node_exporter
  3. # 配置Prometheus
  4. sudo nano /etc/prometheus/prometheus.yml
复制代码

添加以下内容:
  1. global:
  2.   scrape_interval: 15s
  3.   evaluation_interval: 15s
  4. rule_files:
  5.   # - "first_rules.yml"
  6.   # - "second_rules.yml"
  7. scrape_configs:
  8.   - job_name: 'prometheus'
  9.     static_configs:
  10.       - targets: ['localhost:9090']
  11.   - job_name: 'node_exporter'
  12.     static_configs:
  13.       - targets: ['localhost:9100']
复制代码

启动并启用Prometheus和Node Exporter:
  1. sudo systemctl start prometheus node_exporter
  2. sudo systemctl enable prometheus node_exporter
复制代码

安装Grafana:
  1. # 添加Grafana仓库
  2. sudo zypper addrepo https://packages.grafana.com/oss/rpm grafana
  3. # 刷新仓库
  4. sudo zypper refresh
  5. # 安装Grafana
  6. sudo zypper install grafana
  7. # 启动并启用Grafana
  8. sudo systemctl start grafana-server
  9. sudo systemctl enable grafana-server
复制代码

访问http://server.example.com:3000配置Grafana,并添加Prometheus作为数据源。

5.2 日志管理

安装Elasticsearch:
  1. # 添加Elasticsearch仓库
  2. sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
  3. sudo zypper addrepo https://artifacts.elastic.co/packages/7.x/yum elasticsearch
  4. # 刷新仓库
  5. sudo zypper refresh
  6. # 安装Elasticsearch
  7. sudo zypper install elasticsearch
  8. # 配置Elasticsearch
  9. sudo nano /etc/elasticsearch/elasticsearch.yml
复制代码

修改以下配置:
  1. network.host: 0.0.0.0
  2. discovery.type: single-node
复制代码

启动并启用Elasticsearch:
  1. sudo systemctl start elasticsearch
  2. sudo systemctl enable elasticsearch
复制代码

安装Logstash:
  1. # 安装Logstash
  2. sudo zypper install logstash
  3. # 创建配置文件
  4. sudo nano /etc/logstash/conf.d/02-beats-input.conf
复制代码

添加以下内容:
  1. input {
  2.   beats {
  3.     port => 5044
  4.   }
  5. }
复制代码

创建过滤器配置:
  1. sudo nano /etc/logstash/conf.d/10-syslog-filter.conf
复制代码

添加以下内容:
  1. filter {
  2.   if [type] == "syslog" {
  3.     grok {
  4.       match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
  5.     }
  6.     date {
  7.       match => [ "syslog_timestamp", "MMM  d HH:mm:ss", "MMM dd HH:mm:ss" ]
  8.     }
  9.   }
  10. }
复制代码

创建输出配置:
  1. sudo nano /etc/logstash/conf.d/30-elasticsearch-output.conf
复制代码

添加以下内容:
  1. output {
  2.   elasticsearch {
  3.     hosts => ["localhost:9200"]
  4.     index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"
  5.   }
  6. }
复制代码

启动并启用Logstash:
  1. sudo systemctl start logstash
  2. sudo systemctl enable logstash
复制代码

安装Kibana:
  1. # 安装Kibana
  2. sudo zypper install kibana
  3. # 配置Kibana
  4. sudo nano /etc/kibana/kibana.yml
复制代码

修改以下配置:
  1. server.host: "0.0.0.0"
  2. elasticsearch.hosts: ["http://localhost:9200"]
复制代码

启动并启用Kibana:
  1. sudo systemctl start kibana
  2. sudo systemctl enable kibana
复制代码

访问http://server.example.com:5601配置Kibana。
  1. # 安装Filebeat
  2. sudo zypper install filebeat
  3. # 配置Filebeat
  4. sudo nano /etc/filebeat/filebeat.yml
复制代码

修改以下配置:
  1. filebeat.inputs:
  2. - type: log
  3.   enabled: true
  4.   paths:
  5.     - /var/log/*.log
  6.     - /var/log/messages
  7.     - /var/log/secure
  8.     - /var/log/maillog
  9. output.logstash:
  10.   hosts: ["localhost:5044"]
复制代码

启动并启用Filebeat:
  1. sudo systemctl start filebeat
  2. sudo systemctl enable filebeat
复制代码

5.3 自动化备份

创建备份脚本:
  1. sudo nano /usr/local/bin/backup.sh
复制代码

添加以下内容:
  1. #!/bin/bash
  2. # 设置变量
  3. BACKUP_DIR="/backup"
  4. DATE=$(date +%Y%m%d)
  5. RETENTION_DAYS=30
  6. # 创建备份目录
  7. mkdir -p $BACKUP_DIR/$DATE
  8. # 备份系统配置文件
  9. tar -czf $BACKUP_DIR/$DATE/etc.tar.gz /etc
  10. # 备份网站文件
  11. tar -czf $BACKUP_DIR/$DATE/www.tar.gz /srv/www
  12. # 备份数据库
  13. mysqldump --all-databases | gzip > $BACKUP_DIR/$DATE/mysql.sql.gz
  14. # 删除旧备份
  15. find $BACKUP_DIR -type d -mtime +$RETENTION_DAYS -exec rm -rf {} \;
  16. # 记录日志
  17. echo "Backup completed on $(date)" >> /var/log/backup.log
复制代码

使脚本可执行:
  1. sudo chmod +x /usr/local/bin/backup.sh
复制代码

编辑crontab:
  1. sudo crontab -e
复制代码

添加以下内容:
  1. # 每天凌晨2点执行备份
  2. 0 2 * * * /usr/local/bin/backup.sh
复制代码

6. 高级配置与故障排除

6.1 负载均衡配置

编辑Nginx配置:
  1. sudo nano /etc/nginx/conf.d/load-balancer.conf
复制代码

添加以下内容:
  1. upstream backend {
  2.     least_conn;
  3.     server backend1.example.com weight=5;
  4.     server backend2.example.com weight=5;
  5.     server backend3.example.com backup;
  6. }
  7. server {
  8.     listen 80;
  9.     server_name loadbalancer.example.com;
  10.     location / {
  11.         proxy_pass http://backend;
  12.         proxy_set_header Host $host;
  13.         proxy_set_header X-Real-IP $remote_addr;
  14.         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  15.         proxy_set_header X-Forwarded-Proto $scheme;
  16.     }
  17. }
复制代码

重启Nginx:
  1. sudo systemctl restart nginx
复制代码

安装HAProxy:
  1. sudo zypper install haproxy
复制代码

配置HAProxy:
  1. sudo nano /etc/haproxy/haproxy.cfg
复制代码

添加以下内容:
  1. global
  2.     log         127.0.0.1 local2
  3.     chroot      /var/lib/haproxy
  4.     pidfile     /var/run/haproxy.pid
  5.     maxconn     4000
  6.     user        haproxy
  7.     group       haproxy
  8.     daemon
  9.     ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
  10.     ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
  11. defaults
  12.     mode                    http
  13.     log                     global
  14.     option                  httplog
  15.     option                  dontlognull
  16.     option http-server-close
  17.     option forwardfor       except 127.0.0.0/8
  18.     option                  redispatch
  19.     retries                 3
  20.     timeout http-request    10s
  21.     timeout queue           1m
  22.     timeout connect         10s
  23.     timeout client          1m
  24.     timeout server          1m
  25.     timeout http-keep-alive 10s
  26.     timeout check           10s
  27.     maxconn                 3000
  28. frontend http-in
  29.     bind *:80
  30.     default_backend servers
  31. backend servers
  32.     balance     roundrobin
  33.     server      server1 192.168.1.101:80 check
  34.     server      server2 192.168.1.102:80 check
  35.     server      server3 192.168.1.103:80 check backup
复制代码

启动并启用HAProxy:
  1. sudo systemctl start haproxy
  2. sudo systemctl enable haproxy
复制代码

6.2 高可用性配置

安装Keepalived:
  1. sudo zypper install keepalived
复制代码

配置Keepalived(主节点):
  1. sudo nano /etc/keepalived/keepalived.conf
复制代码

添加以下内容:
  1. vrrp_script chk_nginx {
  2.     script "killall -0 nginx"
  3.     interval 2
  4.     weight 2
  5. }
  6. vrrp_instance VI_1 {
  7.     state MASTER
  8.     interface eth0
  9.     virtual_router_id 51
  10.     priority 101
  11.     advert_int 1
  12.     authentication {
  13.         auth_type PASS
  14.         auth_pass mysecret
  15.     }
  16.     virtual_ipaddress {
  17.         192.168.1.100/24 dev eth0
  18.     }
  19.     track_script {
  20.         chk_nginx
  21.     }
  22. }
复制代码

配置Keepalived(备节点):
  1. sudo nano /etc/keepalived/keepalived.conf
复制代码

添加以下内容:
  1. vrrp_script chk_nginx {
  2.     script "killall -0 nginx"
  3.     interval 2
  4.     weight 2
  5. }
  6. vrrp_instance VI_1 {
  7.     state BACKUP
  8.     interface eth0
  9.     virtual_router_id 51
  10.     priority 100
  11.     advert_int 1
  12.     authentication {
  13.         auth_type PASS
  14.         auth_pass mysecret
  15.     }
  16.     virtual_ipaddress {
  17.         192.168.1.100/24 dev eth0
  18.     }
  19.     track_script {
  20.         chk_nginx
  21.     }
  22. }
复制代码

启动并启用Keepalived:
  1. sudo systemctl start keepalived
  2. sudo systemctl enable keepalived
复制代码

安装Corosync和Pacemaker:
  1. sudo zypper install corosync pacemaker
复制代码

配置Corosync:
  1. sudo nano /etc/corosync/corosync.conf
复制代码

添加以下内容:
  1. totem {
  2.     version: 2
  3.     cluster_name: mycluster
  4.     transport: udpu
  5.     interface {
  6.         ringnumber: 0
  7.         bindnetaddr: 192.168.1.0
  8.         mcastport: 5405
  9.     }
  10. }
  11. nodelist {
  12.     node {
  13.         ring0_addr: 192.168.1.101
  14.         name: node1
  15.         nodeid: 1
  16.     }
  17.     node {
  18.         ring0_addr: 192.168.1.102
  19.         name: node2
  20.         nodeid: 2
  21.     }
  22. }
  23. quorum {
  24.     provider: corosync_votequorum
  25.     two_node: 1
  26. }
  27. logging {
  28.     to_logfile: yes
  29.     logfile: /var/log/cluster/corosync.log
  30.     to_syslog: yes
  31. }
复制代码

启动并启用Corosync和Pacemaker:
  1. sudo systemctl start corosync pacemaker
  2. sudo systemctl enable corosync pacemaker
复制代码

配置集群资源:
  1. # 禁用STONITH
  2. sudo pcs property set stonith-enabled=false
  3. # 设置无仲裁策略
  4. sudo pcs property set no-quorum-policy=ignore
  5. # 创建虚拟IP资源
  6. sudo pcs resource create virtualip ocf:heartbeat:IPaddr2 ip=192.168.1.100 cidr_netmask=24 op monitor interval=30s
  7. # 创建Web服务器资源
  8. sudo pcs resource create webserver systemd:nginx op monitor interval=30s
  9. # 设置资源约束
  10. sudo pcs constraint colocation add webserver with virtualip INFINITY
  11. sudo pcs constraint order virtualip then webserver
复制代码

6.3 常见故障排除

检查网络接口状态:
  1. ip addr
复制代码

检查路由表:
  1. ip route
复制代码

检查DNS解析:
  1. nslookup example.com
复制代码

检查端口监听状态:
  1. ss -tulpn
复制代码

检查服务状态:
  1. sudo systemctl status servicename
复制代码

查看服务日志:
  1. sudo journalctl -u servicename
复制代码

检查服务配置文件:
  1. sudo -u servicename configfile
复制代码

检查系统负载:
  1. top
  2. htop
复制代码

检查内存使用:
  1. free -h
复制代码

检查磁盘I/O:
  1. iostat -xz 1
复制代码

检查网络连接:
  1. netstat -an
  2. ss -s
复制代码

7. 总结

通过本文的详细介绍,您已经学会了如何使用openSUSE Tumbleweed搭建高效稳定的网络服务。我们从系统安装和基础配置开始,逐步介绍了Web服务器、数据库服务器、文件服务器和邮件服务器的搭建方法,然后深入探讨了服务优化与性能调优的技巧,接着介绍了安全加固与防护的措施,最后讲解了监控与维护的方法。

openSUSE Tumbleweed作为一款滚动发布的Linux发行版,其稳定性和安全性使其成为企业级网络服务的理想选择。通过合理配置和优化,您可以构建一个高性能、高可用且安全可靠的网络服务环境。

希望本文能够帮助您快速掌握openSUSE Tumbleweed的网络服务搭建技能,并在实际工作中应用这些知识,构建出满足企业需求的高效稳定网络服务。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

频道订阅

频道订阅

加入社群

加入社群

联系我们|TG频道|RSS

Powered by Pixtech

© 2025 Pixtech Team.