一键自动安装
CentOS 7.6
# yum 安装
yum update
yum install -y nginx
# apt-get 安装
apt-get update
apt-get install nginx
# 配置文件路径
/etc/nginx/nginx.conf
/etc/nginx/conf.d/*.conf
# 启动
systemctl start nginx
# 停止
systemctl stop nginx
# 重启
systemctl restart nginx
# 加载配置文件而不需要重启 nginx
service nginx reload
# 开机启动
systemctl enable nginx
# 各种配置,看单独的模块介绍,不再说明
配置PHP时注意
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
# 如果 conf.d 里面有一个 php-fpm.conf 文件,里面内容如下时,需要将 127.0.0.1:9000 替换成 php-fpm
# PHP-FPM FastCGI server
# network or unix domain socket configuration
upstream php-fpm {
server unix:/run/php-fpm/www.sock;
}
# 替换后的参数配置如下
location ~ \.php(.*)$ {
fastcgi_pass php-fpm;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
简单配置
server {
listen 80;
server_name laravel6.com;
root /data/www/laravel_6/public;
location / {
index index.php index.html index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=/$1 last;
}
}
location ~ \.php(.*)$ {
fastcgi_pass php-fpm;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
一些报错
出现 403 404 等,查 nginx 日志,有这么一条 is forbidden (13: Permission denied)
一、查看文件或文件夹权限,试着给 777
二、nginx 配置文件缺少指引文件 index index.php index.html index.htm;
三、坑爹的这个 selinux
,临时关闭一下 setenforce 0
关闭后如果正常,就是他的问题了
# 彻底关闭方法
vim /etc/selinux/config
# 修改 SELINUX=enforcing 改为 SELINUX=disabled
#修改后
SELINUX=enable
源码安装
# 安装必要扩展
yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 下载
wget http://nginx.org/download/nginx-1.18.0.tar.gz
# 解压
tar -zxvf nginx-1.18.0.tar.gz
# 进入
cd nginx-1.18.0
# 配置,其实默认执行 ./configure 即可,除非你要自定义
./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx-1.18.0 \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_gzip_static_module
# 编译 && 安装,默认安装位置 /usr/local/nginx
make
make && install
一些基本命令
# 查看版本
/usr/local/nginx/sbin/nginx -v
# 启动
/usr/local/nginx/sbin/nginx
# 重新载入配置文件
/usr/local/nginx/sbin/nginx -s reload
# 快速关闭 Nginx
/usr/local/nginx/sbin/nginx -s stop
# 关闭 Nginx
/usr/local/nginx/sbin/nginx -s quit
配置 systemctl
启动
vim /usr/lib/systemd/system/nginx.service
输入以下内容后保存:
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPost=/bin/sleep 0.1
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=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
后续启动可以用:systemctl start nginx
设置开机启动: systemctl enable nginx
附录:Nginx 编译所有配置
- 可以通过
./configure --help
查看
参数 | 描述 |
---|---|
--prefix=PATH | 设置安装前缀 |
--sbin-path=PATH | 设置nginx二进制路径名 |
--modules-path=PATH | 设置模块路径 |
--conf-path=PATH | nginx.conf配置文件路径 |
--error-log-path=PATH | 设置错误日志路径名 |
--pid-path=PATH | nginx.pid公司路径名 |
--lock-path=PATH | 引擎锁路径名 |
--user=USER | 为工作进程设置非特权用户 |
--group=GROUP | 为工作进程设置非特权组 |
--build=NAME | 设置生成名称 |
--builddir=DIR | 设置生成目录 |
--with-select_module | 启用选择模块 |
--without-select_module | 禁用选择模块 |
--with-poll_module | 启用轮询模块 |
--without-poll_module | 禁用轮询模块 |
--with-threads | 启用线程池支持 |
--with-file-aio | 启用文件AIO支持 |
--with-http_ssl_module | 启用ngx_http_ssl_模块 |
--with-http_v2_module | 启用ngx_http_v2_模块 |
--with-http_realip_module | 启用ngx_http_realip_模块 |
--with-http_addition_module | 启用ngx_http_addition_模块 |
--with-http_xslt_module | 启用ngx_http_xslt_模块 |
--with-http_xslt_module=dynamic | 启用动态ngx_http_xslt_模块 |
--with-http_image_filter_module | 启用ngx_http_image_filter_模块 |
--with-http_image_filter_module=dynamic | 启用动态ngx_http_image_filter_模块 |
--with-http_geoip_module | 启用ngx_http_geoip_模块 |
--with-http_geoip_module=dynamic | 启用动态ngx_http_geoip_模块 |
--with-http_sub_module | 启用ngx_http_子模块 |
--with-http_dav_module | 启用ngx_http_dav_模块 |
--with-http_flv_module | 启用ngx_http_flv_模块 |
--with-http_mp4_module | 启用ngx_http_mp4_模块 |
--with-http_gunzip_module | 启用ngx_http_gunzip_模块 |
--with-http_gzip_static_module | 启用ngx_http_gzip_static_模块 |
--with-http_auth_request_module | 启用ngx_http_auth_request_模块 |
--with-http_random_index_module | 启用ngx_http_random_index_模块 |
--with-http_secure_link_module | 启用ngx_http_secure_link_模块 |
--with-http_degradation_module | 启用ngx_http_降级模块 |
--with-http_slice_module | 启用ngx_http_slice_模块 |
--with-http_stub_status_module | 启用ngx_http_stub_status_模块 |
--without-http_charset_module | 禁用ngx_http_charset_模块 |
--without-http_gzip_module | 禁用ngx_http_gzip_模块 |
--without-http_ssi_module | 禁用ngx_http_ssi_模块 |
--without-http_userid_module | 禁用ngx_http_userid_模块 |
--without-http_access_module | 禁用ngx_http_访问模块 |
--without-http_auth_basic_module | 禁用ngx_http_auth_basic_模块 |
--without-http_mirror_module | 禁用ngx_http_mirror_模块 |
--without-http_autoindex_module | 禁用ngx_http_autoindex_模块 |
--without-http_geo_module | 禁用ngx_http_geo_模块 |
--without-http_map_module | 禁用ngx_http_map_模块 |
--without-http_split_clients_module | 禁用ngx_http_split_clients_模块 |
--without-http_referer_module | 禁用ngx_http_referer_模块 |
--without-http_rewrite_module | 禁用ngx_http_rewrite_模块 |
--without-http_proxy_module | 禁用ngx_http_proxy_模块 |
--without-http_fastcgi_module | 禁用ngx_http_fastcgi_模块 |
--without-http_uwsgi_module | 禁用ngx_http_uwsgi_模块 |
--without-http_scgi_module | 禁用ngx_http_scgi_模块 |
--without-http_grpc_module | 禁用ngx_http_grpc_模块 |
--without-http_memcached_module | 禁用ngx_http_memcached_模块 |
--without-http_limit_conn_module | 禁用ngx_http_limit_conn_模块 |
--without-http_limit_req_module | 禁用ngx_http_limit_req_模块 |
--without-http_empty_gif_module | 禁用ngx-http-empty-gif-u模块 |
--without-http_browser_module | 禁用ngx_http_browser_模块 |
--without-http_upstream_hash_module | 禁用ngx_http_upstream_hash_模块 |
--without-http_upstream_ip_hash_module | 禁用ngx_http_upstream_ip_hash_模块 |
--without-http_upstream_least_conn_module | 禁用ngx_http_upstream_least_conn_模块 |
--without-http_upstream_random_module | 禁用ngx_http_upstream_random_模块 |
--without-http_upstream_keepalive_module | 禁用ngx_http_upstream_keepalive_模块 |
--without-http_upstream_zone_module | 禁用ngx_http_upstream_zone_模块 |
--with-http_perl_module | 启用ngx_http_perl_模块 |
--with-http_perl_module=dynamic | 启用动态ngx_http_perl_模块 |
--with-perl_modules_path=PATH | 设置Perl模块路径 |
--with-perl=PATH | 设置perl二进制路径名 |
--http-log-path=PATH | 设置http访问日志路径名 |
--http-client-body-temp-path=PATH | 设置存储http客户端请求正文临时文件的路径 |
--http-proxy-temp-path=PATH | 设置存储http代理临时文件的路径 |
--http-fastcgi-temp-path=PATH | 设置存储http fastcgi临时文件的路径 |
--http-uwsgi-temp-path=PATH | 设置存储http uwsgi临时文件的路径 |
--http-scgi-temp-path=PATH | 设置存储http scgi临时文件的路径 |
--without-http | 禁用HTTP服务器 |
--without-http-cache | 禁用HTTP缓存 |
--with-mail | 启用POP3/IMAP4/SMTP代理模块 |
--with-mail=dynamic | 启用动态POP3/IMAP4/SMTP代理模块 |
--with-mail_ssl_module | 启用ngx_mail_ssl_模块 |
--without-mail_pop3_module | 禁用ngx_mail_pop3_模块 |
--without-mail_imap_module | 禁用ngx_mail_imap_模块 |
--without-mail_smtp_module | 禁用ngx_mail_smtp_模块 |
--with-stream | 启用TCP/UDP代理模块 |
--with-stream=dynamic | 启用动态TCP/UDP代理模块 |
--with-stream_ssl_module | 启用ngx_stream_ssl_模块 |
--with-stream_realip_module | 启用ngx_stream_realip_模块 |
--with-stream_geoip_module | 启用ngx_stream_geoip_模块 |
--with-stream_geoip_module=dynamic | 启用动态ngx_stream_geoip_模块 |
--with-stream_ssl_preread_module | 启用ngx_stream_ssl_preread_模块 |
--without-stream_limit_conn_module | 禁用ngx_stream_limit_conn_模块 |
--without-stream_access_module | 禁用ngx_stream_access_模块 |
--without-stream_geo_module | 禁用ngx_stream_geo_模块 |
--without-stream_map_module | 禁用ngx_stream_map_模块 |
--without-stream_split_clients_module | 禁用ngx_stream_split_clients_模块 |
--without-stream_return_module | 禁用ngx_stream_return_模块 |
--without-stream_upstream_hash_module | 禁用ngx_stream_upstream_hash_模块 |
--without-stream_upstream_least_conn_module | 禁用ngx_stream_upstream_least_conn_模块 |
--without-stream_upstream_random_module | 禁用ngx_stream_upstream_random_模块 |
--without-stream_upstream_zone_module | 禁用ngx_stream_upstream_zone_模块 |
--with-google_perftools_module | 启用ngx_google_perftools_模块 |
--with-cpp_test_module | 启用ngx-cpp-u测试模块 |
--add-module=PATH | 启用外部模块 |
--add-dynamic-module=PATH | 启用动态外部模块 |
--with-compat | 动态模块兼容性 |
--with-cc=PATH | 设置C编译器路径名 |
--with-cpp=PATH | 设置C预处理器路径名 |
--with-cc-opt=OPTIONS | 设置其他C编译器选项 |
--with-ld-opt=OPTIONS | 设置其他链接器选项 |
--with-cpu-opt=CPU | 为指定的CPU生成,有效值:pentium、pentiumpro、pentium3、pentium4、athlon、opteron、sparc32、sparc64、ppc64 |
--without-pcre | 禁用PCRE库使用 |
--with-pcre | 强制使用PCRE库 |
--with-pcre=DIR | 设置PCRE库源的路径 |
--with-pcre-opt=OPTIONS | 为PCRE设置其他生成选项 |
--with-pcre-jit | 使用JIT编译支持构建PCRE |
--with-zlib=DIR | 设置zlib库源的路径 |
--with-zlib-opt=OPTIONS | 为zlib设置其他生成选项 |
--with-zlib-asm=CPU | 使用为指定CPU优化的zlib汇编程序源,有效值:pentium、pentiumpro |
--with-libatomic | 强制使用libatomic_ops库 |
--with-libatomic=DIR | 设置libatomic_ops库源的路径 |
--with-openssl=DIR | 设置OpenSSL库源的路径 |
--with-openssl-opt=OPTIONS | 为OpenSSL设置其他生成选项 |
--with-debug | 启用调试日志记录 |