Linux 如何搭建网站
网站使用 HTTP 协议将 HTML 语言编写的 HTML 文件映射到网络上,使用 IP 地址或者域名来访问这些文件,同时使用 CSS 语言对网页的样式进行点缀,使用 JavaScript 语言编写用户与网页交互行为的脚本。
本文着重于讲解如何将 HTML 映射到网络上,关于 HTML 及其其他语言的教程,推荐 菜鸟教程 来学习上述语言
在搭建网站前,您需要准备以下条件:
- Linux 服务器:一台运行 Linux 的服务器(可以是云服务器、本地服务器或虚拟机)
- 基本的 Linux 命令行知识:能够使用命令行进行基本操作
- 域名及DNS配置(可选):用于通过域名访问网站,而不是仅使用 IP 地址
- SSL 证书(建议):用于实现 HTTPS 加密访问
Web 服务器基础
Section titled “Web 服务器基础”Web 服务器是运行在 Linux 上的程序,它监听客户端的 HTTP 请求,然后返回相应的 HTML 文件和资源。
常见的 Web 服务器
Section titled “常见的 Web 服务器”| 服务器 | 特点 | 适用场景 |
|---|---|---|
| Nginx | 轻量级、高性能、反向代理能力强 | 高流量网站、反向代理 |
| Apache | 功能全面、配置灵活、插件丰富 | 传统网站、需要多模块支持 |
| Caddy | 自动HTTPS、配置简单、现代化 | 小型网站、快速部署 |
Nginx 搭建网站
Section titled “Nginx 搭建网站”在编译安装 Nginx 前,需要安装必要的依赖库。
对于 Ubuntu/Debian 系统
sudo apt updatesudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev对于 Centos 系统
sudo yum groupinstall "Development Tools"sudo yum install pcre-devel zlib-devel编译安装 Nginx
Section titled “编译安装 Nginx”前往 Nginx 官网获取下载链接,并使用 wget 命令进行下载
wget http://nginx.org/download/nginx-1.24.0.tar.gz对安装包进行解压。
tar -xzf nginx-1.24.0.tar.gz前往该目录。
cd nginx-1.24.0配置编译选项
最小化编译(推荐):
./configure --with-http_ssl_module—with-http_ssl_module 参数将在编译时为 nginx 安装 https 模块,如果没有这个需求,可以直接运行 ./configure
关于完整的编译配置选项(按需可选):
编译选项说明:
--prefix- Nginx 安装的根目录--sbin-path- Nginx 可执行文件的路径--conf-path- 主配置文件路径--error-log-path- 错误日志路径--http-log-path- 访问日志路径--pid-path- PID 文件路径--with-http_ssl_module- 支持 HTTPS(可选)--with-http_v2_module- 支持 HTTP/2(可选,性能提升)--with-http_gzip_static_module- 支持静态 Gzip 压缩(可选)--with-http_realip_module- 获取真实客户端 IP(可选)--with-http_geoip_module- IP 地理位置识别(可选)--with-http_secure_link_module- 安全链接保护(可选)
接下来就是编译安装,使用如下命令编译并安装。
sudo make && sudo make install安装完成后,默认会被安装在 /usr/local/nginx 目录下,前提是你没有通过 configure 更改安装目录。
为 nginx 创建软链接,方便随时使用 nginx。
sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx然后运行此命令验证安装。
nginx -v启动和管理 Nginx
Section titled “启动和管理 Nginx”# 启动 Nginxsudo /usr/sbin/nginx
# 重新加载配置(重启 Nginx)sudo /usr/sbin/nginx -s reload
# 优雅关闭sudo /usr/sbin/nginx -s quit
# 强制关闭sudo /usr/sbin/nginx -s stop编译安装的 Nginx 的主配置文件位于 /usr/local/nginx/conf/nginx.conf。
配置后使用以下命令重新加载 nginx
nginx -s reload简单的网站配置示例
Section titled “简单的网站配置示例”在 /usr/local/nginx/conf/nginx.conf 的 http {} 块中编辑以下内容:
server { listen 80; server_name example.com www.example.com;
# 网站文件所在目录 root /home/web; index index.html index.htm;
location / { }
# 错误页面 error_page 404 /404.html;}部署网站文件
Section titled “部署网站文件”# 创建网站目录sudo mkdir -p /home/web
# 设置权限sudo chown -R $USER:$USER /home/websudo chmod -R 755 /home/webApache 搭建网站
Section titled “Apache 搭建网站”安装 Apache
Section titled “安装 Apache”# Ubuntu/Debiansudo apt install apache2
# CentOS/RHELsudo yum install httpd启动 Apache
Section titled “启动 Apache”# 启动服务sudo systemctl start apache2 # 或 httpd
# 设置开机自启sudo systemctl enable apache2
# 重启sudo systemctl restart apache2Apache 的默认网站目录为 /var/www/html
虚拟主机配置示例
Section titled “虚拟主机配置示例”创建文件 /etc/apache2/sites-available/example.conf:
<VirtualHost *:80> ServerName example.com ServerAlias www.example.com DocumentRoot /var/www/example.com
<Directory /var/www/example.com> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory>
ErrorLog ${APACHE_LOG_DIR}/example_error.log CustomLog ${APACHE_LOG_DIR}/example_access.log combined</VirtualHost>启用配置:
sudo a2ensite example.confsudo apache2ctl configtestsudo systemctl restart apache2# 使用 UFW(Ubuntu)sudo ufw allow 80/tcpsudo ufw allow 443/tcp
# 使用 firewalld(CentOS)sudo firewall-cmd --permanent --add-service=httpsudo firewall-cmd --permanent --add-service=httpssudo firewall-cmd --reloadSSL/TLS 配置(HTTPS)
Section titled “SSL/TLS 配置(HTTPS)”可以使用 Let’s Encrypt 提供的免费证书:
# 安装 Certbotsudo apt install certbot python3-certbot-nginx
# 申请证书sudo certbot --nginx -d example.com -d www.example.com1. 编译时出现错误
Section titled “1. 编译时出现错误”如果编译时报错”cannot find -lpcre”或类似错误,需要安装相应的开发库:
# Ubuntu/Debiansudo apt install libpcre3-dev zlib1g-dev libssl-dev libgd-dev
# CentOS/RHELsudo yum install pcre-devel zlib-devel openssl-devel gd-devel./configure 找不到
Section titled “./configure 找不到”确保在解压的 nginx 源码目录中运行 configure:
cd ~/nginx-build/nginx-1.24.0./configure ...2. 网站无法访问
Section titled “2. 网站无法访问”- 检查 Nginx 是否运行:
ps aux | grep nginx - 检查防火墙配置是否开放了 80/443 端口
- 查看错误日志:
sudo tail -f /var/log/nginx/error.log - 检查配置文件语法:
sudo /usr/sbin/nginx -t
3. 权限问题
Section titled “3. 权限问题”确保网站文件所在目录的权限正确:
# 创建网站目录sudo mkdir -p /var/www/html
# 设置权限(www-data 为常见的 web 用户)sudo chown -R www-data:www-data /var/www/htmlsudo chmod -R 755 /var/www/html4. 域名无法解析
Section titled “4. 域名无法解析”- 检查 DNS 记录是否正确指向服务器 IP
- 使用
nslookup或dig命令测试 DNS 解析
nslookup example.com# 或dig example.com5. 升级或重新编译 Nginx
Section titled “5. 升级或重新编译 Nginx”如果需要添加新的编译模块,可以重新编译:
cd ~/nginx-build/nginx-1.24.0
# 重新配置(添加新的 --with-xxx 选项)./configure ...新选项...
# 编译(注意不要 make install,要使用 make 然后备份替换)make
# 备份旧的 Nginx 二进制文件sudo cp /usr/sbin/nginx /usr/sbin/nginx.bak
# 复制新的二进制文件sudo cp objs/nginx /usr/sbin/nginx
# 检查新版本sudo /usr/sbin/nginx -v
# 重新启动sudo /usr/sbin/nginx -s reload- 学习更多 Web 服务器配置选项
- 了解数据库集成(MySQL、PostgreSQL 等)
- 学习如何部署 Node.js、Python 等应用框架
- 配置 SSL/TLS 实现安全的 HTTPS 连接