nginx安装与配置详解
时间: 2023-11-04 18:55:13 浏览: 61
Nginx是一款轻量级的高性能Web服务器软件,也可以作为反向代理服务器、负载均衡器和缓存使用。下面是nginx的安装与配置详解:
## 安装Nginx
1. 安装依赖
```
sudo apt update
sudo apt install curl gnupg2 ca-certificates lsb-release
```
2. 添加Nginx软件仓库
```
echo "deb http://nginx.org/packages/ubuntu `lsb_release -cs` nginx" \
| sudo tee /etc/apt/sources.list.d/nginx.list
```
添加Nginx的GPG密钥
```
curl -fsSL https://nginx.org/keys/nginx_signing.key | sudo apt-key add -
```
3. 安装Nginx
```
sudo apt update
sudo apt install nginx
```
4. 启动Nginx
```
sudo systemctl start nginx
```
如果没有启动,可以使用以下命令检查状态:
```
systemctl status nginx
```
## 配置Nginx
1. 配置文件目录
Nginx的配置文件通常位于`/etc/nginx`目录下,其中`/etc/nginx/nginx.conf`是主要的配置文件。
```
cd /etc/nginx
ls -l
```
2. 配置虚拟主机
虚拟主机可以将多个域名映射到同一台服务器上的不同Web应用程序,可以通过以下步骤实现:
* 创建一个新的配置文件
```
sudo nano /etc/nginx/conf.d/example.com.conf
```
在文件中添加以下内容:
```
server {
listen 80;
server_name example.com;
root /var/www/example.com;
index index.html;
}
```
其中:
* `listen`指令是Nginx监听的端口号。
* `server_name`指定了虚拟主机的域名。
* `root`指定了网站文件的根目录。
* `index`指定了默认的索引文件。
* 重载Nginx配置
```
sudo systemctl reload nginx
```
3. 配置SSL证书
SSL证书可以为虚拟主机提供安全的HTTPS连接,可以通过以下步骤实现:
* 安装Certbot
```
sudo apt install certbot python3-certbot-nginx
```
* 获取证书
```
sudo certbot --nginx -d example.com
```
Certbot会自动检测Nginx的虚拟主机配置文件,并为其生成SSL证书。
* 重载Nginx配置
```
sudo systemctl reload nginx
```
以上就是Nginx的安装和配置详解。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)