nginx安装与配置详解
时间: 2023-10-20 22:59:16 浏览: 118
好的,下面是nginx安装与配置的详细步骤:
1. 安装必要的依赖:
```bash
sudo apt-get update
sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev
```
2. 下载nginx源码包:
```bash
wget http://nginx.org/download/nginx-1.18.0.tar.gz
```
3. 解压源码包并进入目录:
```bash
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
```
4. 配置并编译:
```bash
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre --with-stream
make
sudo make install
```
说明:
- `--prefix`:指定安装路径
- `--with-http_ssl_module`:支持HTTPS协议
- `--with-http_stub_status_module`:支持获取nginx状态
- `--with-pcre`:使用pcre库,支持正则表达式
- `--with-stream`:支持TCP/UDP代理
5. 启动nginx:
```bash
sudo /usr/local/nginx/sbin/nginx
```
6. 配置nginx:
- 配置文件路径:/usr/local/nginx/conf/nginx.conf
- 修改配置文件后,重启nginx:`sudo /usr/local/nginx/sbin/nginx -s reload`
样例配置:
```bash
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
}
```
7. 停止nginx:
```bash
sudo /usr/local/nginx/sbin/nginx -s stop
```
希望这些步骤能够帮助你完成nginx的安装与配置。
阅读全文