Nginx安装配置合集

安装

Ubuntu

1
sudo apt install nginx

Manjaro

1
yaourt -S nginx

安装完毕后查看是否成功

1
systemctl status nginx

模块

查看当前使用的Nginx支持的模块

1
2
3
4
5
ubuntu@VM-8-2-ubuntu:~$ nginx -V
nginx version: nginx/1.18.0 (Ubuntu)
built with OpenSSL 1.1.1f 31 Mar 2020
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-KTLRnK/nginx-1.18.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module

如果没有我们需要的就要自己编译源码了。

SSL

在/etc/nginx/sites-enabled/nginx.conf中添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 443;
server_name feater.top;
ssl on;
root /var/www/html;
index index.html index.htm;
ssl_certificate cert/*.pem;
ssl_certificate_key cert/*.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

location / {
index index.html index.htm;
}
}

http重定向到https

1
2
3
4
5
server {
listen 80;
server_name feater.top;
rewrite ^(.*)$ https://feater.top;
}

PHP-FPM

安装软件包

1
yaourt -S php php-cgi php-fpm

在server{}下添加

1
2
3
4
5
6
7
8
9
#当请求网站下php文件的时候,反向代理到php-fpm
location ~ \.php$ {
root /var/www/html;
fastcgi_pass 127.0.0.1:9000; #nginx fastcgi进程监听的IP地址和端口,要和php-fpm配置文件中的一致
fastcgi_index index.php;
#include /usr/local/etc/nginx/fastcgi.conf; #加载nginx的fastcgi模块,如果没有fastcgi.conf配置文件的话,就需要动态配置了,如果有的话,就只需要include配置文件即可
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

注意:

  • nginx.conf最顶部的user要和/etc/php/php-fpm.conf.d/www.conf中的user一致
  • fastcgi_pass值要和www.conf中listen字段的值一致
  • 网站源文件的用户和权限要和配置文件的一致

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!