新的linux需要如何配置新网站
新的linux需要如何配置新网站详细介绍
在新的 Linux 系统上配置新网站,通常需要以下步骤(以常见的 Ubuntu/Debian 或 CentOS 为例,涵盖 Apache/Nginx 服务器、域名绑定、SSL 等通用配置):
一、准备工作
- 更新系统
bashupdate upgrade
dnf update
- 安装 Web 服务器(选择 Apache 或 Nginx)
- Apachebash
apache2
dnf httpd
systemctl start httpd systemctl httpd
- Nginxbash
apt/dnf nginx
systemctl start nginx systemctl nginx
- Apachebash
二、创建网站目录与权限
- 创建网站根目录
bash/var/www/yourdomain.com/html - 设置权限(确保 Web 服务器用户可读写)
bashwww-data:www-data /var/www/yourdomain.com
www-data:www-data /var/www/yourdomain.com
nginx:nginx /var/www/yourdomain.com
/var/www/yourdomain.com
- 添加测试页面(可选)
bash/var/www/yourdomain.com/html/index.html
三、配置域名与虚拟主机
情况 1:使用 Apache
- 创建虚拟主机配置文件
bash/etc/apache2/sites-available/yourdomain.com.conf添加以下内容(替换
yourdomain.com和目录路径):
apache<VirtualHost *:80>ServerName yourdomain.com
ServerAlias www.yourdomain.com
DocumentRoot /var/www/yourdomain.com/html
ErrorLog ${APACHE_LOG_DIR}/yourdomain.com_error.log
CustomLog ${APACHE_LOG_DIR}/yourdomain.com_access.log combined
</VirtualHost>
- 启用虚拟主机并重启 Apache
basha2ensite yourdomain.com.confsystemctl restart apache2
情况 2:使用 Nginx
- 创建服务器块配置文件
bash/etc/nginx/sites-available/yourdomain.com添加以下内容:
nginxyourdomain.com www.yourdomain.com
/var/www/yourdomain.com/html
index.html index.php
/
/ =404
- 启用配置并重启 Nginx
bash/etc/nginx/sites-available/yourdomain.com /etc/nginx/sites-enabled/nginx systemctl restart nginx
四、配置 SSL(HTTPS)
- 安装 Certbot(Let's Encrypt 客户端)
bashapt/dnf certbot python3-certbot-apache
apt/dnf certbot python3-certbot-nginx
- 获取 SSL 证书并自动配置
bashcertbot --apache/-nginx yourdomain.com www.yourdomain.com- 按提示选择 “2”(同时启用 HTTPS 重定向)。
五、高级配置(可选)
1. 支持动态内容(如 PHP)
- 安装 PHP 及扩展(以 Nginx+PHP-FPM 为例)bash
apt/dnf php-fpm php-mysql php-cli php-mbstring php-gd php-curl php-xml - 修改 Nginx 配置(添加 PHP 解析)
在 Nginx 的服务器块中加入:nginx~ \.php$snippets/fastcgi-php.conf
unix:/run/php/php-fpm.sock
2. 防火墙设置
- UFW(Ubuntu/Debian)bash
ufw allowufw allow /tcp
ufw
- Firewalld(CentOS)bash
firewall-cmd --add-servicehttpfirewall-cmd --add-servicehttps
firewall-cmd
3. 上传网站文件
- 通过 SFTP/SSH 上传:bash
local-site-folder user@server:/var/www/yourdomain.com/html - 或使用 Git 克隆:bash
/var/www/yourdomain.com/html clone your-repo-url
六、测试与验证
- 访问网站
- HTTP:
http://yourdomain.com(自动跳转 HTTPS,若配置了重定向) - HTTPS:
https://yourdomain.com
- HTTP:
- 检查日志
- Apache 日志:
/var/log/apache2/yourdomain.com_error.log - Nginx 日志:
/var/log/nginx/yourdomain.com_error.log - PHP 错误:
/var/log/php-fpm/error.log(若启用 PHP)
- Apache 日志:
- 验证 SSL 证书
bashcertbot certificates
七、后续维护
- 定期更新系统与软件
bashapt/dnf update apt/dnf upgrade - 自动续签 SSL 证书(Let's Encrypt 默认每 60 天过期)
bashsystemctl certbot.timer - 备份网站数据
bashwebsite-backup- +%Y%m%d.tar.gz /var/www/yourdomain.com
总结
核心步骤:安装服务器 → 创建目录与权限 → 配置域名 → 启用 SSL → 防火墙设置 → 上传文件。根据需求选择 Apache/Nginx 及是否支持动态语言(PHP/Node.js 等),确保安全配置(如 HTTPS、权限控制)和定期维护。