# nginx开源版本安装
## 虚拟机备份
### 关闭虚拟机 init 0
### 克隆虚拟机
## 下载nginxk开源版
wget https://nginx.org/download/nginx-1.25.2.tar.gz
## 安装nginx
tar zxvf nginx-1.25.2.tar.gz
cd nginx-1.25.2
## 安装nginx到/usr/local/nginx下
[root nginx-1.25.2] ./configure --prefix=/usr/local/nginx
### 安装gcc依赖
[root nginx-1.25.2] ./configure --prefix=/usr/local/nginx
./configure: error: C compiler cc is not found
[root nginx-1.25.2]# yum install -y gcc
### 安装pcre依赖
[root nginx-1.25.2] ./configure --prefix=/usr/local/nginx
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
[root nginx-1.25.2]# yum install -y pcre pcre-devel
### 安装zlib依赖
[root nginx-1.25.2] ./configure --prefix=/usr/local/nginx
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
[root nginx-1.25.2]# yum install -y zlib zlib-devel
### 成功安装
[root nginx-1.25.2] ./configure --prefix=/usr/local/nginx
Configuration summary
+ using system PCRE library
+ OpenSSL library is not used
+ using system zlib library
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx modules path: "/usr/local/nginx/modules"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
### 根据Makefile编译运行
[root nginx-1.25.2]# cat Makefile
default: build
clean:
rm -rf Makefile objs
.PHONY: default clean
build:
$(MAKE) -f objs/Makefile
install:
$(MAKE) -f objs/Makefile install
modules:
$(MAKE) -f objs/Makefile modules
upgrade:
/usr/local/nginx/sbin/nginx -t
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
.PHONY: build install modules upgrade
[root nginx-1.25.2] make ## 根据Makefile编译
[root nginx-1.25.2] make install ## 编译后执行安装
## 启动nginx
[root nginx-1.25.2] cd /usr/local/nginx/sbin
[root@iZbp195d6ax86t20fssqyjZ sbin]# ll
total 3840
-rwxr-xr-x 1 root root 3930088 Aug 28 14:27 nginx
[root@iZbp195d6ax86t20fssqyjZ sbin]# ./nginx
## 安装nginx系统服务
### 创建服务脚本
vi /usr/lib/systemd/system/nginx.service
### 服务脚本内容
[Unit]
Description=nginx - web server
After=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 -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
### 重新加载系统服务
systemctl daemon-reload
### 停止原来启动的nginx
[root sbin]# ./nginx -s stop
[root sbin]# ps -ef | grep nginx
root 25580 24790 0 14:49 pts/0 00:00:00 grep --color=auto nginx
### 启动nginx服务
[root sbin]# systemctl start nginx.service
### 查看启动状态
[root sbin]# systemctl status nginx.service
### 开启启动nginx服务
[root sbin]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
### 解决网页中文乱码
[root@iZbp195d6ax86t20fssqyjZ conf]# vim nginx.conf
server {
listen 80;
server_name localhost;
charset utf-8; ## 字符集改成utf-8
[root@iZbp195d6ax86t20fssqyjZ sbin]# ./nginx -s reload