nginx Linux安装
一、卸载NGINX
卸载nginx程序的详细步骤
1、停止Nginx软件
/usr/local/nginx/sbin/nginx -s stop
如果不知道nginx安装路径,可以通过执行ps命令找到nginx程序的PID,然后kill其PID
ps -aux |grep nginx

2、查找根下所有名字包含nginx的文件
find / -name nginx

3、执行命令 rm -rf *删除nignx安装的相关文件
说明:全局查找往往会查出很多相关文件,但是前缀基本都是相同,后面不同的部分可以用*代替,以便快速删除~
[root@qll251 ~]# rm -rf /usr/local/sbin/nginx
[root@qll251 ~]# rm -rf /usr/local/nginx
[root@qll251 ~]# rm -rf /usr/src/nginx-1.11.1
[root@qll251 ~]# rm -rf /var/spool/mail/nginx
4、其他设置
如果设置了Nginx开机自启动的话,可能还需要下面两步
chkconfig nginx off
rm -rf /etc/init.d/nginx
删除之后,便可重新安装nginx了
重新安装nginx
1、下载nginx安装包
wget http://nginx.org/download/nginx-1.28.0.tar.gz
cd /home
2、把压缩包解压到/home
tar -zxvf nginx-1.28.0.tar.gz
3、切换到cd /home/nginx-1.28.0/下面
执行三个命令:
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
如果要求升级openssl
系统是centos7.7,原生openssl版本是1.0.2k,已经对openssh和openssl做了编译安装,现在的版本是openssh8.3p1,openssl1.1.1t
以前我编译安装nginx是不带其他条件,如下
./configure --prefix=/usr/local/nginx
是因为服务器是新的,先编译安装的nginx,后升级的openssl
如上图,如果先升级openssl,再编译安装nginx,需要带上openssl源码路径,注意,是openssl源码路径,不是安装后的路径
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/home/openssl-1.1.1w
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-openssl=/home/openssl-1.1.1w --with-http_gzip_static_module --with-file-aio --with-http_realip_module --with-pcre=/usr/local/pcre --with-zlib=/usr/local/zlib
make && make install
4、切换到/usr/local/nginx安装目录

5、配置nginx的配置文件nginx.conf文件,主要也就是端口
可以按照自己服务器的端口使用情况来进行配置
ESC键,wq!强制保存并退出
6、启动nginx服务
切换目录到/usr/local/nginx/sbin下面

启动nginx命令:
./nginx
8、访问你的服务器IP
显示

location ^~/file/ {
#允许跨域请求的域,* 代表所有
add_header 'Access-Control-Allow-Origin' *;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;
proxy_pass http://xxx/;
}
说明安装和配置都没问题OK了
9、nginx常用命令
1.启动nginx命点:./nginx
2.重启nginx命令:./nginx -s reload
3. 停止 nginx 命令: ./nginx -s stop 或 ./nginx -s quit
4. 关闭nginx进程:
ps -ef|grep nginx
命令 kill -9 8725 (进程号 上面的) 则关闭nginx
如果已经安装nginx
2 nginx SSL模块是否支持
查看nginx是否安装http_ssl_module模块
./nginx -V
如果看到我上图标记得 configure arguments: --with-http_ssl_module, 则已安装(可以直接跳过nginx安装ssl支持步骤,进入 nginx.conf 配置)。
3,nginx安装
3.1 nginx官网地址
下载地址:http://nginx.org/en/download.html
这里下载nginx-1.28.0.tar.gz
3.2 下载tar包到服务器目录
cd /home
wget http://nginx.org/download/nginx-1.28.0.tar.gz
3.3 解压安装包
tar -zxvf nginx-1.28.0.tar.gz
3.4 配置ssl模块
cd nginx-1.28.0
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
我们只需要执行以上命令即可,由于我服务器上还有其他模块依赖
3.5 接下来执行命令编译
使用 make 命令编译(使用make install会重新安装nginx),此时当前目录会出现 objs 文件夹。用新的 nginx 文件覆盖当前的 nginx 文件。
3.6 命令拷贝复制
cp ./objs/nginx /usr/local/nginx/sbin/
3.7 再次查看安装的模块(configure arguments: --with-http_ssl_module说明ssl模块已安装)。
./nginx -V
nginx version: nginx/1.28.0
…
configure arguments: –with-http_ssl_module
4. ssl 证书部署
下载申请好的 ssl 证书文件压缩包到本地并解压(这里是用的 pem 与 key 文件,文件名可以更改)。
在 nginx 目录新建 cert 文件夹存放证书文件。
cd /usr/local/nginx
mkdir cert
Nginx 提示host not found in upstream 错误解决方法
vi /etc/hosts
//oss配置
xx.xx.xx.xxx 你要配置的网站
Nginx ===> make: *** No rule to make target `build‘, needed by `default‘. Stop.
make: *** No rule to make target `build', needed by `default'. Stop.
make: *** 没有规则可以创建“default”需要的目标“build”。 停止。
原因分析:
缺少依赖
Nginx所需依赖包
预处理、编译、连接、汇编
gcc
用于网站加密通讯
openssl
用于支持解析正则表达式
pcre
用于对数据进行解压缩。网站之间通信时,数据先压缩再传输,通过消耗CPU的方式来节省网络带宽
zlib
解决方法:
安装依赖包
安装下面配置
yum -y install make zlib-devel gcc-c++ libtool openssl openssl-devel
# 或者
yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel
安装后验证
./configure
#或者
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
编译
make && make install
其他:
如果这个方法还是不行,可以检查./configure后面的选项是否有误,或者选项中的路径是否正确,也可以看一下系统提示哪些是 —not found的,看缺少哪些依赖包

浙公网安备 33010602011771号