centos7.9 编译安装nginx
时间: 2024-05-16 17:11:04 浏览: 153
centos7编译安装nginx
好的,下面是centos7.9编译安装nginx的步骤:
1. 安装编译nginx所需的依赖库:
```
yum install -y gcc gcc-c++ autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxslt libxslt-devel gd gd-devel perl perl-devel perl-ExtUtils-Embed
```
2. 下载nginx源码包:
可以到官网(https://nginx.org/en/download.html)下载最新的稳定版nginx源码包。
3. 解压源码包:
```
tar zxvf nginx-xxx.tar.gz
```
4. 进入nginx源码目录,执行以下命令进行编译和安装:
```
./configure --with-http_stub_status_module --with-http_ssl_module
make && make install
```
其中,--prefix参数指定nginx安装目录,--with-http_stub_status_module参数启用状态页模块,--with-http_ssl_module参数启用SSL加密支持。
5. 配置nginx:
进入/usr/local/nginx/conf目录,编辑nginx.conf文件,进行配置。这里给出一个简单的配置文件示例:
```
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /usr/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/nginx/conf/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root /usr/local/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
}
}
```
其中,listen参数指定监听端口,server_name参数指定域名,root参数指定网站根目录,ssl_certificate和ssl_certificate_key参数指定SSL证书和私钥文件路径。
6. 启动nginx:
进入/usr/local/nginx/sbin目录,执行以下命令启动nginx:
```
./nginx
```
7. 检查nginx是否启动成功:
执行以下命令查看nginx进程是否存在:
```
ps -ef | grep nginx
```
执行以下命令检查nginx监听的端口是否打开:
```
netstat -tlnp | grep nginx
```
如果输出类似以下内容,则说明nginx已经成功安装并启动:
```
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN xxxx/nginx
tcp 0 0 :::443 :::* LISTEN xxxx/nginx
```
阅读全文