主机屋 ubuntu 14安装nginx

本文详细介绍了在Ubuntu系统上安装Nginx的过程,包括安装gcc、g++、pcre、zlib、ssl等依赖库,以及下载、解压、配置、编译、安装和启动Nginx的步骤。同时提供了启动、停止、重启Nginx的方法。

http://www.cnblogs.com/piscesLoveCc/p/5794926.html

安装gcc g++的依赖库

1 sudo apt-get install build-essential

2 sudo apt-get install libtool

安装 pcre依赖库

3 sudo apt-get update

4 sudo apt-get install libpcre3 libpcre3-dev

安装 zlib依赖库

5 sudo apt-get install zlib1g-dev

安装 ssl依赖库

6 sudo apt-get install openssl

安装Nginx

#下载最新版本:

wget http://nginx.org/download/nginx-1.13.1.tar.gz
#解压:
tar -zxvf nginx-1.13.1.tar.gz
#进入解压目录:
cd nginx-1.13.1
#配置:
./configure --prefix=/usr/local/nginx 
#编辑nginx:
make
注意:这里可能会报错,提示“pcre.h No such file or directory”,具体详见:http://stackoverflow.com/questions/22555561/error-building-fatal-error-pcre-h-no-such-file-or-directory
需要安装 libpcre3-dev,命令为:sudo apt-get install libpcre3-dev
#安装nginx:
sudo make install
#启动nginx:
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
注意:-c 指定配置文件的路径,不加的话,nginx会自动加载默认路径的配置文件,可以通过 -h查看帮助命令。
#查看nginx进程:
ps -ef|grep nginx
 

启动 Nginx

/usr/local/nginx/sbin/nginx

 
./sbin/nginx 

停止 Nginx

./sbin/nginx -s stop

 
./sbin/nginx -s quit

Nginx重新加载配置

./sbin/nginx -s reload

指定配置文件

./sbin/nginx -c /usr/local/nginx/conf/nginx.conf

 
 

转载于:https://www.cnblogs.com/xqnq2007/p/7073526.html

<think>我们正在Ubuntu 25.04上安装Nginx。根据引用[3]和引用[4]中的信息,我们可以选择源码安装或使用包管理器安装。但考虑到Ubuntu 25.04可能尚未有官方预编译包,我们这里提供两种方法:使用APT包管理器安装(如果可用)和源码编译安装。由于Ubuntu 25.04较新,我们优先尝试使用APT安装。 ### 方法1:使用APT安装(推荐,如果官方源已支持) 1. **更新系统包索引**: ```bash sudo apt update ``` 2. **安装Nginx**: ```bash sudo apt install nginx ``` 3. **验证安装**: ```bash nginx -v ``` 4. **启动并设置开机自启**: ```bash sudo systemctl start nginx sudo systemctl enable nginx ``` ### 方法2:源码编译安装(如果APT源中没有或需要特定版本) 参考引用[3]的步骤,但注意版本号可能需要更新(这里以1.26.2为例,但建议使用最新稳定版): 1. **安装依赖**: ```bash sudo apt update sudo apt install -y build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev ``` 2. **下载Nginx源码**(替换为最新版本URL): ```bash wget https://nginx.org/download/nginx-1.26.2.tar.gz tar -zxvf nginx-1.26.2.tar.gz cd nginx-1.26.2 ``` 3. **配置编译选项**(可根据需要调整): ```bash ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --with-compat --with-threads --with-http_ssl_module --with-http_v2_module ``` 4. **编译并安装**: ```bash make sudo make install ``` 5. **创建Systemd服务文件**(参考引用[3]): 创建文件`/lib/systemd/system/nginx.service`,内容如下: ```ini [Unit] Description=The nginx web server After=network.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/usr/sbin/nginx -s stop PrivateTmp=true [Install] WantedBy=multi-user.target ``` 6. **启用并启动服务**: ```bash sudo systemctl daemon-reload sudo systemctl enable nginx sudo systemctl start nginx ``` ### 配置防火墙(参考引用[4]) ```bash sudo ufw allow 'Nginx Full' sudo ufw enable sudo ufw status ``` ### 验证安装 打开浏览器访问服务器IP,应看到Nginx欢迎页。或使用: ```bash curl http://localhost ``` §§相关问题§§ 1. 如何在Ubuntu 25.04上配置Nginx虚拟主机? 2. 如何为Nginx启用HTTPS? 3. Nginx常见性能优化方法有哪些?</think>### 在Ubuntu 25.04上安装Nginx的完整指南 #### 方法1:通过APT仓库安装(推荐) ```bash # 1. 更新系统包索引 sudo apt update # 2. 安装Nginx sudo apt install nginx -y # 3. 验证安装 nginx -v # 应输出类似:nginx version: 1.26.x # 4. 启动并启用服务 sudo systemctl start nginx sudo systemctl enable nginx # 5. 验证运行状态 sudo systemctl status nginx # 应显示"active (running)" ``` #### 方法2:源码编译安装(自定义版本) ```bash # 1. 安装编译依赖 sudo apt install build-essential libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev -y # 2. 下载源码(替换版本号为最新) wget https://nginx.org/download/nginx-1.26.2.tar.gz tar -zxvf nginx-1.26.2.tar.gz cd nginx-1.26.2 # 3. 配置编译选项 ./configure \ --prefix=/etc/nginx \ --sbin-path=/usr/sbin/nginx \ --modules-path=/usr/lib/nginx/modules \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock # 4. 编译并安装 make sudo make install # 5. 创建systemd服务文件 sudo nano /etc/systemd/system/nginx.service ``` 粘贴以下服务配置[^3]: ```ini [Unit] Description=The nginx web server After=network.target [Service] ExecStart=/usr/sbin/nginx ExecReload=/usr/sbin/nginx -s reload ExecStop=/usr/sbin/nginx -s stop PIDFile=/var/run/nginx.pid PrivateTmp=true [Install] WantedBy=multi-user.target ``` ```bash # 6. 启用服务 sudo systemctl daemon-reload sudo systemctl enable nginx sudo systemctl start nginx ``` #### 基础配置与验证 1. 开放防火墙端口: ```bash sudo ufw allow 'Nginx Full' # HTTP+HTTPS sudo ufw reload ``` 2. 测试配置文件: ```bash sudo nginx -t # 应显示: syntax is ok, test is successful ``` 3. 访问测试: ```bash curl http://localhost ``` 浏览器访问服务器IP应看到Nginx欢迎页 #### 安全优化建议 1. 删除敏感信息: ```bash sudo sed -i 's/# server_tokens off;/server_tokens off;/g' /etc/nginx/nginx.conf ``` 2. 创建专用用户: ```bash sudo useradd --system --no-create-home nginxuser sudo sed -i 's/user www-data;/user nginxuser;/g' /etc/nginx/nginx.conf ``` 3. 重启服务生效: ```bash sudo systemctl restart nginx ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值