如何在centos7下源码安装nginx?
时间: 2023-06-18 10:06:26 浏览: 60
以下是在CentOS 7下源码安装Nginx的步骤:
1. 安装必要的依赖包:
```
sudo yum install -y gcc gcc-c++ make automake autoconf libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel
```
2. 下载并解压Nginx源码包:
```
wget http://nginx.org/download/nginx-1.18.0.tar.gz
tar -zxvf nginx-1.18.0.tar.gz
cd nginx-1.18.0
```
3. 配置编译参数:
```
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module
```
以上参数的含义:
- `--prefix=/usr/local/nginx`:指定Nginx安装目录为`/usr/local/nginx`
- `--with-http_ssl_module`:启用SSL功能
- `--with-http_v2_module`:启用HTTP/2功能
4. 编译安装:
```
make && make install
```
5. 启动Nginx:
```
/usr/local/nginx/sbin/nginx
```
6. 验证Nginx是否已经成功启动:
在浏览器中访问服务器IP地址或域名,如果能够看到"Welcome to nginx!"的页面,说明Nginx已经成功安装并启动了。
7. 将Nginx设置为系统服务(可选):
```
sudo vi /etc/systemd/system/nginx.service
```
将以下内容复制到文件中:
```
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
```
保存并退出。
使用以下命令重新加载systemd配置:
```
sudo systemctl daemon-reload
```
使用以下命令启动Nginx服务:
```
sudo systemctl start nginx
```
使用以下命令设置开机启动:
```
sudo systemctl enable nginx
```
至此,在CentOS 7下源码安装Nginx完成。