|
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
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镜像文件:
- wget https://download.opensuse.org/tumbleweed/iso/openSUSE-Tumbleweed-DVD-x86_64-Current.iso
复制代码
创建启动U盘或光盘:
- # 在Linux系统下使用dd命令创建启动U盘
- 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 系统基础配置
安装完成后,首先更新系统到最新状态:
- # 刷新软件包仓库
- sudo zypper refresh
- # 更新所有软件包
- sudo zypper update --no-recommends
- # 如果内核更新,重启系统
- sudo reboot
复制代码
编辑网络配置文件,设置静态IP地址:
- # 查看网络接口名称
- ip addr
- # 编辑网络配置文件
- sudo nano /etc/sysconfig/network/ifcfg-eth0
复制代码
在配置文件中添加以下内容:
- BOOTPROTO='static'
- IPADDR='192.168.1.100/24'
- GATEWAY='192.168.1.1'
- DNS1='8.8.8.8'
- DNS2='8.8.4.4'
- STARTMODE='auto'
复制代码
重启网络服务:
- sudo systemctl restart network
复制代码- # 设置主机名
- sudo hostnamectl set-hostname server.example.com
- # 编辑hosts文件
- sudo nano /etc/hosts
复制代码
在hosts文件中添加:
- 127.0.0.1 localhost
- 192.168.1.100 server.example.com server
复制代码
openSUSE使用Firewalld作为防火墙管理工具:
- # 启动并启用防火墙
- sudo systemctl start firewalld
- sudo systemctl enable firewalld
- # 查看防火墙状态
- sudo firewall-cmd --state
- # 开放SSH端口(默认22)
- sudo firewall-cmd --permanent --add-service=ssh
- sudo firewall-cmd --reload
复制代码
为了安全起见,建议修改SSH默认配置:
- # 编辑SSH配置文件
- sudo nano /etc/ssh/sshd_config
复制代码
修改以下配置项:
- # 禁止root登录
- PermitRootLogin no
- # 更改默认端口
- Port 2222
- # 仅允许特定用户登录
- AllowUsers adminuser
- # 禁用密码认证,使用密钥认证
- PasswordAuthentication no
- PubkeyAuthentication yes
复制代码
重启SSH服务:
- sudo systemctl restart sshd
复制代码
2. 网络服务搭建
2.1 Web服务器搭建(Apache/Nginx)
- # 安装Apache
- sudo zypper install apache2
- # 启动并启用Apache服务
- sudo systemctl start apache2
- sudo systemctl enable apache2
- # 检查Apache状态
- sudo systemctl status apache2
复制代码
配置Apache虚拟主机:
- # 创建网站目录
- sudo mkdir -p /srv/www/example.com/public_html
- sudo chown -R wwwrun:www /srv/www/example.com/public_html
- # 创建虚拟主机配置文件
- sudo nano /etc/apache2/vhosts.d/example.com.conf
复制代码
添加以下配置:
- <VirtualHost *:80>
- ServerName example.com
- ServerAlias www.example.com
- DocumentRoot /srv/www/example.com/public_html
- ErrorLog /var/log/apache2/example.com-error.log
- CustomLog /var/log/apache2/example.com-access.log combined
- </VirtualHost>
复制代码
启用配置并重启Apache:
- # 启用配置
- sudo a2enmod vhost_alias
- sudo a2ensite example.com
- # 重启Apache
- sudo systemctl restart apache2
复制代码- # 安装Nginx
- sudo zypper install nginx
- # 启动并启用Nginx服务
- sudo systemctl start nginx
- sudo systemctl enable nginx
- # 检查Nginx状态
- sudo systemctl status nginx
复制代码
配置Nginx虚拟主机:
- # 创建网站目录
- sudo mkdir -p /srv/www/example.com/public_html
- sudo chown -R wwwrun:www /srv/www/example.com/public_html
- # 创建虚拟主机配置文件
- sudo nano /etc/nginx/vhosts.d/example.com.conf
复制代码
添加以下配置:
- server {
- listen 80;
- server_name example.com www.example.com;
- root /srv/www/example.com/public_html;
- index index.html index.htm;
- location / {
- try_files $uri $uri/ =404;
- }
- access_log /var/log/nginx/example.com-access.log;
- error_log /var/log/nginx/example.com-error.log;
- }
复制代码
测试配置并重启Nginx:
- # 测试配置
- sudo nginx -t
- # 重启Nginx
- sudo systemctl restart nginx
复制代码
2.2 数据库服务器搭建(MariaDB/MySQL)
- # 安装MariaDB服务器和客户端
- sudo zypper install mariadb mariadb-client
- # 启动并启用MariaDB服务
- sudo systemctl start mariadb
- sudo systemctl enable mariadb
- # 运行安全安装脚本
- sudo mysql_secure_installation
复制代码
创建数据库和用户:
- # 登录MariaDB
- mysql -u root -p
- # 创建数据库
- CREATE DATABASE exampledb;
- # 创建用户并授权
- CREATE USER 'exampleuser'@'localhost' IDENTIFIED BY 'password';
- GRANT ALL PRIVILEGES ON exampledb.* TO 'exampleuser'@'localhost';
- FLUSH PRIVILEGES;
- EXIT;
复制代码- # 添加MySQL仓库
- sudo zypper addrepo https://dev.mysql.com/get/mysql-zypper-repo.noarch.rpm
- # 刷新仓库
- sudo zypper refresh
- # 安装MySQL服务器
- sudo zypper install mysql-community-server
- # 启动并启用MySQL服务
- sudo systemctl start mysql
- sudo systemctl enable mysql
- # 运行安全安装脚本
- sudo mysql_secure_installation
复制代码
2.3 PHP环境搭建
- # 安装PHP及常用扩展
- sudo zypper install php8 php8-fpm php8-mysql php8-gd php8-mbstring php8-xml php8-curl php8-zip
- # 启动并启用PHP-FPM服务
- sudo systemctl start php-fpm
- sudo systemctl enable php-fpm
复制代码
配置Nginx使用PHP-FPM:
- # 编辑Nginx配置
- sudo nano /etc/nginx/nginx.conf
复制代码
在http块中添加:
- upstream php-handler {
- server unix:/run/php-fpm/php-fpm.sock;
- }
复制代码
编辑虚拟主机配置:
- sudo nano /etc/nginx/vhosts.d/example.com.conf
复制代码
修改配置以支持PHP:
- server {
- listen 80;
- server_name example.com www.example.com;
- root /srv/www/example.com/public_html;
- index index.php index.html index.htm;
- location / {
- try_files $uri $uri/ =404;
- }
- location ~ \.php$ {
- fastcgi_pass php-handler;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
- access_log /var/log/nginx/example.com-access.log;
- error_log /var/log/nginx/example.com-error.log;
- }
复制代码
重启Nginx和PHP-FPM:
- sudo systemctl restart nginx
- sudo systemctl restart php-fpm
复制代码
2.4 文件服务器搭建(NFS/Samba)
- # 安装NFS服务器
- sudo zypper install nfs-kernel-server
- # 创建共享目录
- sudo mkdir -p /srv/nfs/share
- sudo chown nobody:nogroup /srv/nfs/share
- sudo chmod 777 /srv/nfs/share
- # 配置NFS共享
- sudo nano /etc/exports
复制代码
添加以下内容:
- /srv/nfs/share 192.168.1.0/24(rw,sync,no_subtree_check)
复制代码
启动并启用NFS服务:
- sudo systemctl start nfs-server
- sudo systemctl enable nfs-server
- # 更新NFS共享表
- sudo exportfs -a
复制代码
配置防火墙允许NFS:
- sudo firewall-cmd --permanent --add-service=nfs
- sudo firewall-cmd --permanent --add-service=mountd
- sudo firewall-cmd --permanent --add-service=rpc-bind
- sudo firewall-cmd --reload
复制代码- # 安装Samba
- sudo zypper install samba
- # 创建共享目录
- sudo mkdir -p /srv/samba/share
- sudo chown nobody:nogroup /srv/samba/share
- sudo chmod 777 /srv/samba/share
- # 配置Samba
- sudo nano /etc/samba/smb.conf
复制代码
添加以下配置:
- [global]
- workgroup = WORKGROUP
- server string = Samba Server
- security = user
- map to guest = bad user
- dns proxy = no
- [Share]
- path = /srv/samba/share
- browsable = yes
- writable = yes
- guest ok = yes
- read only = no
复制代码
启动并启用Samba服务:
- sudo systemctl start nmb smb
- sudo systemctl enable nmb smb
复制代码
配置防火墙允许Samba:
- sudo firewall-cmd --permanent --add-service=samba
- sudo firewall-cmd --reload
复制代码
2.5 邮件服务器搭建(Postfix/Dovecot)
- # 安装Postfix
- sudo zypper install postfix
- # 配置Postfix
- sudo nano /etc/postfix/main.cf
复制代码
修改以下配置:
- myhostname = mail.example.com
- mydomain = example.com
- myorigin = $mydomain
- inet_interfaces = all
- mydestination = $myhostname, localhost.$mydomain, localhost, $mydomain
- mynetworks = 127.0.0.0/8, 192.168.1.0/24
- home_mailbox = Maildir/
复制代码
启动并启用Postfix:
- sudo systemctl start postfix
- sudo systemctl enable postfix
复制代码- # 安装Dovecot
- sudo zypper install dovecot dovecot-imapd dovecot-pop3d
- # 配置Dovecot
- sudo nano /etc/dovecot/dovecot.conf
复制代码
确保以下配置正确:
- protocols = imap pop3 lmtp
- listen = *
复制代码
配置认证:
- sudo nano /etc/dovecot/conf.d/10-auth.conf
复制代码
修改以下配置:
- disable_plaintext_auth = no
- auth_mechanisms = plain login
复制代码
配置邮件位置:
- sudo nano /etc/dovecot/conf.d/10-mail.conf
复制代码
修改以下配置:
- mail_location = maildir:~/Maildir
复制代码
启动并启用Dovecot:
- sudo systemctl start dovecot
- sudo systemctl enable dovecot
复制代码
配置防火墙允许邮件服务:
- sudo firewall-cmd --permanent --add-service=smtp
- sudo firewall-cmd --permanent --add-service=pop3
- sudo firewall-cmd --permanent --add-service=imap
- sudo firewall-cmd --permanent --add-service=smtps
- sudo firewall-cmd --permanent --add-service=pop3s
- sudo firewall-cmd --permanent --add-service=imaps
- sudo firewall-cmd --reload
复制代码
3. 服务优化与性能调优
3.1 系统性能优化
编辑sysctl配置文件:
- sudo nano /etc/sysctl.d/99-server-tuning.conf
复制代码
添加以下内容:
- # 增加文件描述符限制
- fs.file-max = 100000
- # 网络调优
- net.core.rmem_max = 16777216
- net.core.wmem_max = 16777216
- net.ipv4.tcp_rmem = 4096 87380 16777216
- net.ipv4.tcp_wmem = 4096 65536 16777216
- net.ipv4.tcp_fin_timeout = 30
- net.ipv4.tcp_keepalive_time = 1200
- net.ipv4.tcp_max_syn_backlog = 65536
- net.core.netdev_max_backlog = 65536
- net.ipv4.tcp_max_tw_buckets = 1440000
- net.ipv4.tcp_tw_reuse = 1
- net.ipv4.tcp_tw_recycle = 0
- net.ipv4.tcp_syncookies = 1
- net.ipv4.tcp_synack_retries = 2
- net.ipv4.tcp_syn_retries = 2
- # 虚拟内存调优
- vm.swappiness = 10
- vm.dirty_ratio = 60
- vm.dirty_background_ratio = 2
复制代码
应用配置:
- sudo sysctl -p /etc/sysctl.d/99-server-tuning.conf
复制代码
为提高文件系统性能,可以调整挂载选项:
- # 编辑fstab文件
- sudo nano /etc/fstab
复制代码
修改根分区挂载选项,添加noatime和nodiratime:
- UUID=xxxx-xxxx / ext4 defaults,noatime,nodiratime 0 1
复制代码
重新挂载文件系统:
3.2 Web服务器性能优化
编辑Apache配置文件:
- sudo nano /etc/apache2/server-tuning.conf
复制代码
修改以下参数:
- <IfModule prefork.c>
- StartServers 5
- MinSpareServers 5
- MaxSpareServers 10
- MaxClients 150
- MaxRequestsPerChild 0
- </IfModule>
- <IfModule worker.c>
- StartServers 2
- MaxClients 150
- MinSpareThreads 25
- MaxSpareThreads 75
- ThreadsPerChild 25
- MaxRequestsPerChild 0
- </IfModule>
- KeepAlive On
- KeepAliveTimeout 5
- MaxKeepAliveRequests 100
复制代码
启用Apache缓存模块:
- sudo a2enmod cache
- sudo a2enmod cache_disk
- sudo a2enmod expires
- sudo a2enmod headers
- sudo a2enmod deflate
复制代码
配置缓存:
- sudo nano /etc/apache2/conf.d/cache.conf
复制代码
添加以下内容:
- <IfModule mod_cache.c>
- CacheEnable disk /
- CacheRoot /var/cache/apache2/mod_cache_disk
- CacheDirLevels 2
- CacheDirLength 1
- CacheDefaultExpire 3600
- CacheMaxFileSize 1000000
- CacheMinFileSize 1
- </IfModule>
- <IfModule mod_expires.c>
- ExpiresActive On
- ExpiresByType image/jpg "access plus 1 year"
- ExpiresByType image/jpeg "access plus 1 year"
- ExpiresByType image/gif "access plus 1 year"
- ExpiresByType image/png "access plus 1 year"
- ExpiresByType text/css "access plus 1 month"
- ExpiresByType application/pdf "access plus 1 month"
- ExpiresByType text/x-javascript "access plus 1 month"
- ExpiresByType application/x-shockwave-flash "access plus 1 month"
- ExpiresByType image/x-icon "access plus 1 year"
- ExpiresDefault "access plus 2 days"
- </IfModule>
- <IfModule mod_deflate.c>
- AddOutputFilterByType DEFLATE text/plain
- AddOutputFilterByType DEFLATE text/html
- AddOutputFilterByType DEFLATE text/xml
- AddOutputFilterByType DEFLATE text/css
- AddOutputFilterByType DEFLATE application/xml
- AddOutputFilterByType DEFLATE application/xhtml+xml
- AddOutputFilterByType DEFLATE application/rss+xml
- AddOutputFilterByType DEFLATE application/javascript
- AddOutputFilterByType DEFLATE application/x-javascript
- </IfModule>
复制代码
重启Apache:
- sudo systemctl restart apache2
复制代码
编辑Nginx主配置文件:
- sudo nano /etc/nginx/nginx.conf
复制代码
修改以下参数:
- user wwwrun;
- worker_processes auto;
- worker_rlimit_nofile 100000;
- events {
- worker_connections 4096;
- use epoll;
- multi_accept on;
- }
- http {
- # 基本设置
- sendfile on;
- tcp_nopush on;
- tcp_nodelay on;
- keepalive_timeout 30;
- keepalive_requests 100000;
- reset_timedout_connection on;
- client_body_timeout 10;
- send_timeout 2;
-
- # 缓冲区设置
- client_body_buffer_size 128k;
- client_max_body_size 10m;
- client_header_buffer_size 1k;
- large_client_header_buffers 4 4k;
- output_buffers 1 32k;
- postpone_output 1460;
-
- # Gzip压缩
- gzip on;
- gzip_min_length 10240;
- gzip_proxied expired no-cache no-store private auth;
- gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
- gzip_disable "MSIE [1-6]\.";
-
- # 缓存设置
- open_file_cache max=200000 inactive=20s;
- open_file_cache_valid 30s;
- open_file_cache_min_uses 2;
- open_file_cache_errors on;
- }
复制代码
重启Nginx:
- sudo systemctl restart nginx
复制代码
3.3 数据库性能优化
编辑MariaDB/MySQL配置文件:
添加以下内容:
- [mysqld]
- # 基本设置
- character-set-server = utf8mb4
- collation-server = utf8mb4_unicode_ci
- default-storage-engine = InnoDB
- # 内存设置
- innodb_buffer_pool_size = 2G
- innodb_buffer_pool_instances = 2
- innodb_log_file_size = 256M
- innodb_log_buffer_size = 8M
- innodb_flush_log_at_trx_commit = 2
- innodb_flush_method = O_DIRECT
- innodb_file_per_table = 1
- key_buffer_size = 256M
- max_allowed_packet = 16M
- thread_cache_size = 16
- table_open_cache = 2000
- query_cache_type = 1
- query_cache_size = 128M
- query_cache_limit = 2M
- # 连接设置
- max_connections = 200
- max_connect_errors = 100000
- wait_timeout = 300
- interactive_timeout = 300
- # 其他设置
- skip-name-resolve
- sync_binlog = 0
- slow_query_log = 1
- slow_query_log_file = /var/log/mysql/slow.log
- long_query_time = 2
复制代码
重启MariaDB/MySQL:
- sudo systemctl restart mariadb
复制代码
3.4 PHP性能优化
编辑PHP-FPM配置文件:
- sudo nano /etc/php8/php-fpm.d/www.conf
复制代码
修改以下参数:
- pm = dynamic
- pm.max_children = 100
- pm.start_servers = 20
- pm.min_spare_servers = 10
- pm.max_spare_servers = 30
- pm.max_requests = 1000
复制代码
编辑PHP配置文件:
- sudo nano /etc/php8/php.ini
复制代码
修改以下参数:
- memory_limit = 256M
- max_execution_time = 300
- max_input_time = 300
- upload_max_filesize = 64M
- post_max_size = 64M
- max_file_uploads = 20
- realpath_cache_size = 4096k
- realpath_cache_ttl = 120
- opcache.enable = 1
- opcache.memory_consumption = 128
- opcache.interned_strings_buffer = 8
- opcache.max_accelerated_files = 4000
- opcache.revalidate_freq = 60
- opcache.fast_shutdown = 1
- opcache.enable_file_override = 0
- opcache.validate_timestamps = 1
复制代码
重启PHP-FPM:
- sudo systemctl restart php-fpm
复制代码
4. 安全加固与防护
4.1 系统安全加固
创建管理员用户并禁用root远程登录:
- # 创建新用户
- sudo useradd -m -G wheel adminuser
- sudo passwd adminuser
- # 禁用root远程SSH登录
- sudo nano /etc/ssh/sshd_config
复制代码
修改以下配置:
重启SSH服务:
- sudo systemctl restart sshd
复制代码
编辑sudoers文件:
添加以下内容:
- # 允许wheel组成员使用sudo
- %wheel ALL=(ALL) ALL
- # 允许adminuser无需密码使用sudo
- adminuser ALL=(ALL) NOPASSWD: ALL
复制代码
安装并配置自动更新:
- # 安装自动更新工具
- sudo zypper install zypper-automatic
- # 配置自动更新
- sudo nano /etc/zypp/zypper-automatic.conf
复制代码
修改以下配置:
- [Main]
- UpdateInterval = daily
- RandomizeDelay = 60
- UpdateType = security
复制代码
启用并启动自动更新服务:
- sudo systemctl enable --now zypper-automatic.timer
复制代码
4.2 防火墙配置
配置更严格的防火墙规则:
- # 创建新的防火墙区域
- sudo firewall-cmd --permanent --new-zone=publicweb
- # 设置默认策略
- sudo firewall-cmd --permanent --zone=publicweb --set-target=DROP
- # 开放必要端口
- sudo firewall-cmd --permanent --zone=publicweb --add-service=http
- sudo firewall-cmd --permanent --zone=publicweb --add-service=https
- sudo firewall-cmd --permanent --zone=publicweb --add-service=ssh
- # 将网络接口分配到新区域
- sudo firewall-cmd --permanent --zone=publicweb --change-interface=eth0
- # 重新加载防火墙配置
- sudo firewall-cmd --reload
复制代码
如果需要配置端口转发:
- # 启用IP伪装(NAT)
- sudo firewall-cmd --permanent --zone=publicweb --add-masquerade
- # 配置端口转发
- sudo firewall-cmd --permanent --zone=publicweb --add-forward-port=port=80:proto=tcp:toport=8080:toaddr=192.168.1.100
- # 重新加载防火墙配置
- sudo firewall-cmd --reload
复制代码
4.3 Fail2Ban防护
- # 安装Fail2Ban
- sudo zypper install fail2ban
- # 创建本地配置文件
- sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
- # 编辑配置文件
- sudo nano /etc/fail2ban/jail.local
复制代码
修改以下配置:
- [DEFAULT]
- bantime = 1h
- findtime = 10m
- maxretry = 3
- [sshd]
- enabled = true
- port = 2222
- logpath = %(sshd_log)s
- maxretry = 3
- bantime = 1d
- [apache-auth]
- enabled = true
- port = http,https
- logpath = %(apache_error_log)s
- maxretry = 3
- bantime = 1d
- [nginx-http-auth]
- enabled = true
- port = http,https
- logpath = %(nginx_error_log)s
- maxretry = 3
- bantime = 1d
复制代码
启动并启用Fail2Ban:
- sudo systemctl start fail2ban
- sudo systemctl enable fail2ban
复制代码
4.4 SSL/TLS安全配置
使用Let’s Encrypt获取免费SSL证书:
- # 安装Certbot
- sudo zypper install certbot
- # 获取证书
- sudo certbot certonly --webroot -w /srv/www/example.com/public_html -d example.com -d www.example.com
复制代码
启用SSL模块:
创建SSL配置文件:
- sudo nano /etc/apache2/vhosts.d/example.com-ssl.conf
复制代码
添加以下内容:
- <IfModule mod_ssl.c>
- <VirtualHost *:443>
- ServerName example.com
- ServerAlias www.example.com
- DocumentRoot /srv/www/example.com/public_html
- ErrorLog /var/log/apache2/example.com-ssl-error.log
- CustomLog /var/log/apache2/example.com-ssl-access.log combined
-
- SSLEngine on
- SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
- SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
-
- # 安全配置
- SSLProtocol all -SSLv3 -TLSv1 -TLSv1.1
- SSLCipherSuite HIGH:!aNULL:!MD5
- SSLHonorCipherOrder on
- SSLCompression off
- SSLSessionTickets off
-
- # HSTS
- Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
- </VirtualHost>
- </IfModule>
复制代码
重启Apache:
- sudo systemctl restart apache2
复制代码
编辑Nginx虚拟主机配置:
- sudo nano /etc/nginx/vhosts.d/example.com.conf
复制代码
添加以下内容:
- server {
- listen 80;
- server_name example.com www.example.com;
- return 301 https://$host$request_uri;
- }
- server {
- listen 443 ssl http2;
- server_name example.com www.example.com;
- root /srv/www/example.com/public_html;
- index index.php index.html index.htm;
-
- ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
- ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
-
- # 安全配置
- ssl_protocols TLSv1.2 TLSv1.3;
- ssl_prefer_server_ciphers on;
- 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';
- ssl_session_timeout 1d;
- ssl_session_cache shared:SSL:50m;
- ssl_session_tickets off;
- ssl_stapling on;
- ssl_stapling_verify on;
-
- # HSTS
- add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
-
- location / {
- try_files $uri $uri/ =404;
- }
-
- location ~ \.php$ {
- fastcgi_pass php-handler;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- }
-
- access_log /var/log/nginx/example.com-ssl-access.log;
- error_log /var/log/nginx/example.com-ssl-error.log;
- }
复制代码
重启Nginx:
- sudo systemctl restart nginx
复制代码
4.5 安全扫描与审计
Lynis是一款强大的安全审计工具:
- # 安装Lynis
- sudo zypper install lynis
- # 运行安全审计
- sudo lynis audit system
- # 查看报告
- sudo cat /var/log/lynis-report.dat
复制代码
安装并配置auditd:
- # 安装auditd
- sudo zypper install auditd
- # 启动并启用auditd
- sudo systemctl start auditd
- sudo systemctl enable auditd
- # 添加审计规则
- sudo nano /etc/audit/rules.d/audit.rules
复制代码
添加以下内容:
- # 监控文件变更
- -w /etc/passwd -p wa -k identity
- -w /etc/group -p wa -k identity
- -w /etc/shadow -p wa -k identity
- -w /etc/sudoers -p wa -k identity
- -w /etc/ssh/sshd_config -p wa -k sshd_config
- # 监控系统调用
- -a always,exit -F arch=b64 -S chmod,fchmod,fchmodat -F auid>=1000 -F auid!=4294967295 -k perm_mod
- -a always,exit -F arch=b64 -S chown,fchown,lchown,fchownat -F auid>=1000 -F auid!=4294967295 -k perm_mod
复制代码
重新加载审计规则:
- sudo systemctl restart auditd
- sudo auditctl -R /etc/audit/rules.d/audit.rules
复制代码
5. 监控与维护
5.1 系统监控
- # 添加Zabbix仓库
- sudo rpm -Uvh https://repo.zabbix.com/zabbix/5.0/sles/15/x86_64/zabbix-release-5.0-1.sles15.noarch.rpm
- # 刷新仓库
- sudo zypper refresh
- # 安装Zabbix服务器、前端和代理
- sudo zypper install zabbix-server-mysql zabbix-web-mysql zabbix-apache-conf zabbix-agent
- # 创建数据库和用户
- mysql -u root -p
- CREATE DATABASE zabbix CHARACTER SET utf8mb4 COLLATE utf8mb4_bin;
- CREATE USER zabbix@localhost IDENTIFIED BY 'password';
- GRANT ALL PRIVILEGES ON zabbix.* TO zabbix@localhost;
- EXIT;
- # 导入初始架构和数据
- zcat /usr/share/doc/packages/zabbix-server-mysql/create.sql.gz | mysql -uzabbix -p zabbix
- # 配置Zabbix服务器
- sudo nano /etc/zabbix/zabbix_server.conf
复制代码
修改以下配置:
- DBHost=localhost
- DBName=zabbix
- DBUser=zabbix
- DBPassword=password
复制代码
配置PHP前端:
- sudo nano /etc/php7/php.ini
复制代码
修改以下配置:
- max_execution_time = 300
- memory_limit = 128M
- post_max_size = 16M
- upload_max_filesize = 2M
- max_input_time = 300
- date.timezone = Asia/Shanghai
复制代码
启动并启用Zabbix服务:
- sudo systemctl restart zabbix-server zabbix-agent apache2
- sudo systemctl enable zabbix-server zabbix-agent apache2
复制代码
访问http://server.example.com/zabbix完成Zabbix前端安装。
- # 安装Prometheus
- sudo zypper install prometheus prometheus-node_exporter
- # 配置Prometheus
- sudo nano /etc/prometheus/prometheus.yml
复制代码
添加以下内容:
- global:
- scrape_interval: 15s
- evaluation_interval: 15s
- rule_files:
- # - "first_rules.yml"
- # - "second_rules.yml"
- scrape_configs:
- - job_name: 'prometheus'
- static_configs:
- - targets: ['localhost:9090']
- - job_name: 'node_exporter'
- static_configs:
- - targets: ['localhost:9100']
复制代码
启动并启用Prometheus和Node Exporter:
- sudo systemctl start prometheus node_exporter
- sudo systemctl enable prometheus node_exporter
复制代码
安装Grafana:
- # 添加Grafana仓库
- sudo zypper addrepo https://packages.grafana.com/oss/rpm grafana
- # 刷新仓库
- sudo zypper refresh
- # 安装Grafana
- sudo zypper install grafana
- # 启动并启用Grafana
- sudo systemctl start grafana-server
- sudo systemctl enable grafana-server
复制代码
访问http://server.example.com:3000配置Grafana,并添加Prometheus作为数据源。
5.2 日志管理
安装Elasticsearch:
- # 添加Elasticsearch仓库
- sudo rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
- sudo zypper addrepo https://artifacts.elastic.co/packages/7.x/yum elasticsearch
- # 刷新仓库
- sudo zypper refresh
- # 安装Elasticsearch
- sudo zypper install elasticsearch
- # 配置Elasticsearch
- sudo nano /etc/elasticsearch/elasticsearch.yml
复制代码
修改以下配置:
- network.host: 0.0.0.0
- discovery.type: single-node
复制代码
启动并启用Elasticsearch:
- sudo systemctl start elasticsearch
- sudo systemctl enable elasticsearch
复制代码
安装Logstash:
- # 安装Logstash
- sudo zypper install logstash
- # 创建配置文件
- sudo nano /etc/logstash/conf.d/02-beats-input.conf
复制代码
添加以下内容:
- input {
- beats {
- port => 5044
- }
- }
复制代码
创建过滤器配置:
- sudo nano /etc/logstash/conf.d/10-syslog-filter.conf
复制代码
添加以下内容:
- filter {
- if [type] == "syslog" {
- grok {
- match => { "message" => "%{SYSLOGTIMESTAMP:syslog_timestamp} %{SYSLOGHOST:syslog_hostname} %{DATA:syslog_program}(?:\[%{POSINT:syslog_pid}\])?: %{GREEDYDATA:syslog_message}" }
- }
- date {
- match => [ "syslog_timestamp", "MMM d HH:mm:ss", "MMM dd HH:mm:ss" ]
- }
- }
- }
复制代码
创建输出配置:
- sudo nano /etc/logstash/conf.d/30-elasticsearch-output.conf
复制代码
添加以下内容:
- output {
- elasticsearch {
- hosts => ["localhost:9200"]
- index => "%{[@metadata][beat]}-%{+YYYY.MM.dd}"
- }
- }
复制代码
启动并启用Logstash:
- sudo systemctl start logstash
- sudo systemctl enable logstash
复制代码
安装Kibana:
- # 安装Kibana
- sudo zypper install kibana
- # 配置Kibana
- sudo nano /etc/kibana/kibana.yml
复制代码
修改以下配置:
- server.host: "0.0.0.0"
- elasticsearch.hosts: ["http://localhost:9200"]
复制代码
启动并启用Kibana:
- sudo systemctl start kibana
- sudo systemctl enable kibana
复制代码
访问http://server.example.com:5601配置Kibana。
- # 安装Filebeat
- sudo zypper install filebeat
- # 配置Filebeat
- sudo nano /etc/filebeat/filebeat.yml
复制代码
修改以下配置:
- filebeat.inputs:
- - type: log
- enabled: true
- paths:
- - /var/log/*.log
- - /var/log/messages
- - /var/log/secure
- - /var/log/maillog
- output.logstash:
- hosts: ["localhost:5044"]
复制代码
启动并启用Filebeat:
- sudo systemctl start filebeat
- sudo systemctl enable filebeat
复制代码
5.3 自动化备份
创建备份脚本:
- sudo nano /usr/local/bin/backup.sh
复制代码
添加以下内容:
- #!/bin/bash
- # 设置变量
- BACKUP_DIR="/backup"
- DATE=$(date +%Y%m%d)
- RETENTION_DAYS=30
- # 创建备份目录
- mkdir -p $BACKUP_DIR/$DATE
- # 备份系统配置文件
- tar -czf $BACKUP_DIR/$DATE/etc.tar.gz /etc
- # 备份网站文件
- tar -czf $BACKUP_DIR/$DATE/www.tar.gz /srv/www
- # 备份数据库
- mysqldump --all-databases | gzip > $BACKUP_DIR/$DATE/mysql.sql.gz
- # 删除旧备份
- find $BACKUP_DIR -type d -mtime +$RETENTION_DAYS -exec rm -rf {} \;
- # 记录日志
- echo "Backup completed on $(date)" >> /var/log/backup.log
复制代码
使脚本可执行:
- sudo chmod +x /usr/local/bin/backup.sh
复制代码
编辑crontab:
添加以下内容:
- # 每天凌晨2点执行备份
- 0 2 * * * /usr/local/bin/backup.sh
复制代码
6. 高级配置与故障排除
6.1 负载均衡配置
编辑Nginx配置:
- sudo nano /etc/nginx/conf.d/load-balancer.conf
复制代码
添加以下内容:
- upstream backend {
- least_conn;
- server backend1.example.com weight=5;
- server backend2.example.com weight=5;
- server backend3.example.com backup;
- }
- server {
- listen 80;
- server_name loadbalancer.example.com;
- location / {
- proxy_pass http://backend;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- proxy_set_header X-Forwarded-Proto $scheme;
- }
- }
复制代码
重启Nginx:
- sudo systemctl restart nginx
复制代码
安装HAProxy:
- sudo zypper install haproxy
复制代码
配置HAProxy:
- sudo nano /etc/haproxy/haproxy.cfg
复制代码
添加以下内容:
- global
- log 127.0.0.1 local2
- chroot /var/lib/haproxy
- pidfile /var/run/haproxy.pid
- maxconn 4000
- user haproxy
- group haproxy
- daemon
- 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
- ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
- defaults
- mode http
- log global
- option httplog
- option dontlognull
- option http-server-close
- option forwardfor except 127.0.0.0/8
- option redispatch
- retries 3
- timeout http-request 10s
- timeout queue 1m
- timeout connect 10s
- timeout client 1m
- timeout server 1m
- timeout http-keep-alive 10s
- timeout check 10s
- maxconn 3000
- frontend http-in
- bind *:80
- default_backend servers
- backend servers
- balance roundrobin
- server server1 192.168.1.101:80 check
- server server2 192.168.1.102:80 check
- server server3 192.168.1.103:80 check backup
复制代码
启动并启用HAProxy:
- sudo systemctl start haproxy
- sudo systemctl enable haproxy
复制代码
6.2 高可用性配置
安装Keepalived:
- sudo zypper install keepalived
复制代码
配置Keepalived(主节点):
- sudo nano /etc/keepalived/keepalived.conf
复制代码
添加以下内容:
- vrrp_script chk_nginx {
- script "killall -0 nginx"
- interval 2
- weight 2
- }
- vrrp_instance VI_1 {
- state MASTER
- interface eth0
- virtual_router_id 51
- priority 101
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass mysecret
- }
- virtual_ipaddress {
- 192.168.1.100/24 dev eth0
- }
- track_script {
- chk_nginx
- }
- }
复制代码
配置Keepalived(备节点):
- sudo nano /etc/keepalived/keepalived.conf
复制代码
添加以下内容:
- vrrp_script chk_nginx {
- script "killall -0 nginx"
- interval 2
- weight 2
- }
- vrrp_instance VI_1 {
- state BACKUP
- interface eth0
- virtual_router_id 51
- priority 100
- advert_int 1
- authentication {
- auth_type PASS
- auth_pass mysecret
- }
- virtual_ipaddress {
- 192.168.1.100/24 dev eth0
- }
- track_script {
- chk_nginx
- }
- }
复制代码
启动并启用Keepalived:
- sudo systemctl start keepalived
- sudo systemctl enable keepalived
复制代码
安装Corosync和Pacemaker:
- sudo zypper install corosync pacemaker
复制代码
配置Corosync:
- sudo nano /etc/corosync/corosync.conf
复制代码
添加以下内容:
- totem {
- version: 2
- cluster_name: mycluster
- transport: udpu
- interface {
- ringnumber: 0
- bindnetaddr: 192.168.1.0
- mcastport: 5405
- }
- }
- nodelist {
- node {
- ring0_addr: 192.168.1.101
- name: node1
- nodeid: 1
- }
- node {
- ring0_addr: 192.168.1.102
- name: node2
- nodeid: 2
- }
- }
- quorum {
- provider: corosync_votequorum
- two_node: 1
- }
- logging {
- to_logfile: yes
- logfile: /var/log/cluster/corosync.log
- to_syslog: yes
- }
复制代码
启动并启用Corosync和Pacemaker:
- sudo systemctl start corosync pacemaker
- sudo systemctl enable corosync pacemaker
复制代码
配置集群资源:
- # 禁用STONITH
- sudo pcs property set stonith-enabled=false
- # 设置无仲裁策略
- sudo pcs property set no-quorum-policy=ignore
- # 创建虚拟IP资源
- sudo pcs resource create virtualip ocf:heartbeat:IPaddr2 ip=192.168.1.100 cidr_netmask=24 op monitor interval=30s
- # 创建Web服务器资源
- sudo pcs resource create webserver systemd:nginx op monitor interval=30s
- # 设置资源约束
- sudo pcs constraint colocation add webserver with virtualip INFINITY
- sudo pcs constraint order virtualip then webserver
复制代码
6.3 常见故障排除
检查网络接口状态:
检查路由表:
检查DNS解析:
检查端口监听状态:
检查服务状态:
- sudo systemctl status servicename
复制代码
查看服务日志:
- sudo journalctl -u servicename
复制代码
检查服务配置文件:
- sudo -u servicename configfile
复制代码
检查系统负载:
检查内存使用:
检查磁盘I/O:
检查网络连接:
7. 总结
通过本文的详细介绍,您已经学会了如何使用openSUSE Tumbleweed搭建高效稳定的网络服务。我们从系统安装和基础配置开始,逐步介绍了Web服务器、数据库服务器、文件服务器和邮件服务器的搭建方法,然后深入探讨了服务优化与性能调优的技巧,接着介绍了安全加固与防护的措施,最后讲解了监控与维护的方法。
openSUSE Tumbleweed作为一款滚动发布的Linux发行版,其稳定性和安全性使其成为企业级网络服务的理想选择。通过合理配置和优化,您可以构建一个高性能、高可用且安全可靠的网络服务环境。
希望本文能够帮助您快速掌握openSUSE Tumbleweed的网络服务搭建技能,并在实际工作中应用这些知识,构建出满足企业需求的高效稳定网络服务。 |
|