在Linux系统中安装Nginx可以通过包管理器或源码编译两种方式。以下是详细步骤:
更新软件包列表:
sudo apt update
安装Nginx:
sudo apt install nginx
启动Nginx并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
验证安装:
sudo systemctl status nginx # 检查服务状态
curl -I 127.0.0.1 # 测试HTTP响应
添加EPEL仓库(CentOS 7/RHEL 7):
sudo yum install epel-release
安装Nginx:
sudo yum install nginx
启动并设置开机自启:
sudo systemctl start nginx
sudo systemctl enable nginx
验证安装(同上)。
安装依赖工具:
sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev
下载Nginx源码包(以1.25.1为例):
wget https://nginx.org/download/nginx-1.25.1.tar.gz
tar -xzvf nginx-1.25.1.tar.gz
cd nginx-1.25.1
配置编译选项(默认安装到/usr/local/nginx
):
./configure --prefix=/usr/local/nginx --with-http_ssl_module
编译并安装:
make && sudo make install
启动Nginx:
sudo /usr/local/nginx/sbin/nginx
验证:
curl -I 127.0.0.1
/etc/nginx/nginx.conf
(包管理器安装)或/usr/local/nginx/conf/nginx.conf
(源码安装)。bash
sudo ufw allow 80/tcp # Ubuntu
sudo firewall-cmd --add-port=80/tcp --permanent # CentOS
PATH
:
bash
echo 'export PATH=$PATH:/usr/local/nginx/sbin' >> ~/.bashrc
访问服务器IP或http://localhost
,看到Nginx欢迎页即表示安装成功。
根据需求选择合适的方式,包管理器适合快速部署,源码编译适合深度定制。