Nginx安装配置

标签: Nginx 分类: 后台服务 创建时间:2020-02-05 03:12:35 更新时间:2023-10-20 11:23:26

1.Deepin安装卸载Nginx

(1) 参考文章1中,使用apt安装。

1
2
3
4
5
6
## 安装
sudo apt install nginx
## 文档一般在 /usr/share
## 可执行文件 /usr/bin
## 配置文件 /etc
##lib文件 /usr/lib
参考文章:
1.如何在Ubuntu 18.04上安装Nginx
2.Ubuntu 16.04安装Nginx
3.CentOS7设置nginx开机自启动 (这里有如何使用systemctl控制管理nginx的开机启动)

(2) 卸载nginx

1
2
3
4
5
6
## 停止nginx
sudo systemctl stop nginx
## 查询
dpkg --get-selections| grep nginx
## 卸载主程序
sudo apt-get --purge remove nginx-full

再次使用dpkg,查看nginx相关文件,然后使用autoremove卸载其他相关文件。

1
sudo apt-get autoremove

2.CentOS编译安装

下载源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
## 安装依赖
yum -y install gcc automake autoconf libtool make pcre pcre-devel zlib zlib-devel openssl openssl-devel

# 如果离线安装的话,可能需要下载相应的开发包,可以通过whereis 查看是否安装相关依赖
whereis zlib

## 进入源码目录
cd nginx
## 配置
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/usr/local/nginx/lock/nginx.lock \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-http_realip_module \
--with-threads \
--with-http_gzip_static_module

## 编译
make
## 安装
make install

## 卸载时,查看安装日志,文件都被放到了/usr/local/nginx中了,删除 /usr/local/nginx 文件夹就好了


## 进入安装目录
cd /usr/local/nginx
## 查看启动参数
sbin/nginx -h
## 启动
sbin/nginx
## 查看进程
ps aux |grep nginx

## 停止
sbin/nginx -s stop

## 重读配置文件
sbin/nginx -s reload

出现这个情况,我还以为是安装失败了呢

问题

(1) ./configure: error: SSL modules require the OpenSSL library.You can either do not enable the modules, or install the OpenSSL library into the system, or build the OpenSSL library statically from the source with nginx by using –with-openssl= option.
在我执行离线安装的时候,出现了这个问题,因为我的Openssl 不是通过 yum执行安装的。

【解决方法】
解决方法就是通过yum安装 Openssl,但是因为是离线状态,没法使用yum怎么办呢?那就是下载 openssl 源码包,然后在 configure 的时候指定交易源码包的位置,比如我的openssl源码包:/home/soft/soft/nginx/openssl-1.1.1g,那么我就可以 修改配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--error-log-path=/usr/local/nginx/logs/error.log \
--pid-path=/usr/local/nginx/logs/nginx.pid \
--lock-path=/usr/local/nginx/lock/nginx.lock \
--with-http_ssl_module \
--with-openssl=/home/soft/soft/nginx/openssl-1.1.1g \
--with-http_stub_status_module \
--with-http_realip_module \
--with-threads \
--with-http_gzip_static_module

3.使用systemctl管理

编译安装之后,是无法直接使用systemctl管理nginx服务的,只能使用/usr/local/nginx/sbin/nginx命令加上参数进行管理和启动。如果非要使用systemctl进行管理,进入/usr/lib/systemd/system目录,新建nginx.service文件(vim /usr/lib/systemd/system/nginx.service),内容如下:(内容的具体含义可以查看参考文章)

1
2
3
4
5
6
7
8
9
10
11
12
[Unit]
Description=nginx - high performance web server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
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

[Install]
WantedBy=multi-user.target

执行启动停止命令:

1
2
3
4
5
6
7
8
## 停止
systemctl stop nginx
## 启动
systemctl start nginx
## 重启
systemctl restart nginx
## 开机启动
systemctl enable nginx

4.查找nginx安装位置

(1) 在linux上配置nginx,需要首先知道nginx的安装位置,
手动编译安装的一般是/usr/local/nginx路径下。使用apt安装的时,配置文件的路径可能就不一样

apt安装:

1
2
3
4
5
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志
/etc/nginx/sites-available: 虚拟主机

yum安装:

1
2
3
4
5
Nginx配置路径:/etc/nginx/
PID目录:/var/run/nginx.pid
错误日志:/var/log/nginx/error.log
访问日志:/var/log/nginx/access.log
默认站点目录:/usr/share/nginx/html

事实上,只需知道Nginx配置路径,其他路径均可在/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf 中查询到。

(2) 如果找不到,就用:find /|grep nginx.conf 命令查找看看。

(3) 还可以查看nginx启动进程,然后通过启动的进程查看配置文件,第一条命令:netstat -tnlp|grep nginx,获取到进程号后,在使用第二条命令:ps -aux |grep 进程号,同样也可以查找到nginx所使用的配置文件,及其安装路径。

参考文章:
1.ubuntu安装nginx及其默认目录结构
2.记录使用yum安装nginx之后的目录问题
3.nginx服务器详细安装过程(使用yum 和 源码包两种安装方式,并说明其区别) yum 安装是在线安装,好处是:安装方式简单,不易出错;源码包 安装是先将 nginx 的源码下载下来,在自己的系统里编译生成可执行文件,然后执行,好处是:因为是在自己的系统上编译的,更符合自己系统的性能,也就是说在自己的系统上执行 nginx 服务性能效率更好。

5.403 Forbidden

打开nginx之后,访问网站,总是提示Forbidden权限错误。

解决方法:
(1) 查看nginx启动用户

1
ps aux | grep "nginx: worker process" | awk '{print $1}'

使用sudo启动nginx服务的时候,用户应该是root,所以,需要修改/etc/nginx/nginx.conf(手动安装的时候就找相应的配置文件),将第一行的user www-data改为user root

(2) 另一个需要注意的地方就是,如果nginx.conf配置文件中没有配置index index.html这种默认文档,就需要写html的全路径,比如:http://localhost/test/test.html。如果设置了默认文档,就可以输入:http://localhost/test/ 

6.Windows安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
## 进入下载安装目录

## 可以双击启动,也可以使用命令行启动
start nginx
## 或者直接运行nginx,但是这样的方法就是会一直占用这个cmd命令
nginx.exe


## 停止
nginx.exe -s stop

## 或者
nginx.exe -s quit

## 重载
nginx.exe -s reload

## 杀死所有的nginx进程
taskkill /f /im nginx.exe

## 查看是否启动
tasklist /fi “imagename eq nginx.exe”

问题

(1) “proxy_pass” cannot have URI part in location given by regular expression, or inside named location, or inside “if” statement, or inside “limit_except” block in /etc/nginx/sites-enabled/default:91

nginx中的配置

根据参考文章3中的意思,就是如果location包含了正则表达式,则 “proxy_pass”不能包含URI part(shop_goods),也就是端口后面不能有斜杠之后的东西。


(2) error: this statement may fall through [-Werror=implicit-fallthrough=]
在Docker容器中,进行编译安装的时候,进行make的时候,出现了错误:

【解决方法】
在进行make的时候,加上参数

1
make CFLAGS='-Wno-implicit-fallthrough'

【终极解决方案】
原来是我nginx版本过低,我下载了1.10.1版本,而gcc版本是8,所以找最新的nginx 1.20.1就可以了。

参考文章:
1.linux下编译nginx1.0.15报错:Implicit fallthrough error gcc版本问题,有这几种解决方法:更换gcc版本,具体是升级还是降级还需测试;在报错的switch-case块中加上break。此时要对代码逻辑理解清楚,否则加上break有可能会破坏原来的逻辑; 编译时忽略Implicit fallthrough error这个错误。
2.报错解决:error: this statement may fall through [-Werror=implicit-fallthrough=] make CFLAGS=’-Wno-implicit-fallthrough’
3.Error: this statement may fall through [-Werror=implicit-fallthrough=] You should add keyword break to each case statement, if you don’t do that, the code will run from case which it matches condition and continue to meet the.您应该在每个 case 语句中添加关键字 break,如果不这样做,代码将从与条件匹配的 case 运行并继续满足
小额赞助
本人提供免费与付费咨询服务,感谢您的支持!赞助请发邮件通知,方便公布您的善意!
**光 3.01 元
Sun 3.00 元
bibichuan 3.00 元
微信公众号
广告位
诚心邀请广大金主爸爸洽谈合作
每日一省
isNaN 和 Number.isNaN 函数的区别?

1.函数 isNaN 接收参数后,会尝试将这个参数转换为数值,任何不能被转换为数值的的值都会返回 true,因此非数字值传入也会返回 true ,会影响 NaN 的判断。

2.函数 Number.isNaN 会首先判断传入参数是否为数字,如果是数字再继续判断是否为 NaN ,不会进行数据类型的转换,这种方法对于 NaN 的判断更为准确。

每日二省
为什么0.1+0.2 ! == 0.3,如何让其相等?

一个直接的解决方法就是设置一个误差范围,通常称为“机器精度”。对JavaScript来说,这个值通常为2-52,在ES6中,提供了Number.EPSILON属性,而它的值就是2-52,只要判断0.1+0.2-0.3是否小于Number.EPSILON,如果小于,就可以判断为0.1+0.2 ===0.3。

每日三省
== 操作符的强制类型转换规则?

1.首先会判断两者类型是否**相同,**相同的话就比较两者的大小。

2.类型不相同的话,就会进行类型转换。

3.会先判断是否在对比 null 和 undefined,是的话就会返回 true。

4.判断两者类型是否为 string 和 number,是的话就会将字符串转换为 number。

5.判断其中一方是否为 boolean,是的话就会把 boolean 转为 number 再进行判断。

6.判断其中一方是否为 object 且另一方为 string、number 或者 symbol,是的话就会把 object 转为原始类型再进行判断。

每日英语
Happiness is time precipitation, smile is the lonely sad.
幸福是年华的沉淀,微笑是寂寞的悲伤。