以下是LAMP服务器网络配置的核心步骤,分系统说明:
CentOS/RHEL:
编辑网络接口文件 /etc/sysconfig/network-scripts/ifcfg-<接口名>(如 ifcfg-eth0),设置静态IP或DHCP:
BOOTPROTO=static # 静态IP
IPADDR=192.168.1.100 # IP地址
NETMASK=255.255.255.0 # 子网掩码
GATEWAY=192.168.1.1 # 网关
DNS1=8.8.8.8 # DNS
ONBOOT=yes # 开机自启
保存后重启网络:sudo systemctl restart network。
Ubuntu/Debian:
编辑 /etc/netplan/01-netcfg.yaml(或对应文件):
network:
version: 2
ethernets:
eth0:
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
应用配置:sudo netplan apply。
CentOS:
开放HTTP(80)、HTTPS(443)端口:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
Ubuntu:
使用UFW允许Apache流量:
sudo ufw allow 'Apache Full'
sudo ufw enable
/etc/httpd/conf/httpd.conf(CentOS)或 /etc/apache2/apache2.conf(Ubuntu):
Listen 80 为所需端口(如8080)。/etc/httpd/conf.d/(CentOS)或 /etc/apache2/sites-available/(Ubuntu)中创建配置文件,指定域名和文档根目录,例如:<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example.com
<Directory /var/www/example.com>
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
sudo systemctl restart httpd(CentOS)或 sudo systemctl restart apache2(Ubuntu)。/etc/mysql/my.cnf(CentOS)或 /etc/mysql/mysql.conf.d/mysqld.cnf(Ubuntu):
bind-address = 0.0.0.0 允许远程连接(默认仅本地)。port = 3306 为自定义端口(需在防火墙中放行)。sudo systemctl restart mysql。ping 8.8.8.8 # 测试互联网访问
curl -I http://localhost # 测试Web服务
mysql -h 192.168.1.100 -u root -p # 测试数据库远程连接(需配置正确权限)
NetworkManager,可通过 nmcli 命令管理网络。systemd-networkd,需通过 networkctl 配置。参考来源:[1,2,3,6,8,9,10,11,12,14]