NGINX编译安装
1. 基础环境准备
NGINX服务软件安装在/soft/package目录下
/soft/package/src目录为软件包目录
mkdir /soft/package/src -p
NGINX使用www用户
useradd -s /sbin/nologin -M www
2. 依赖
# centos
yum -y install libxml2
yum -y install libxml2-devel
yum -y install openssl
yum -y install openssl-devel
yum -y install curl
yum -y install curl-devel
yum -y install libjpeg
yum -y install libpng
yum -y install libjpeg-devel
yum -y install libpng-devel
yum -y install freetype-devel
yum -y install bzip2-devel
yum -y install libmcrypt
yum -y install libmcrypt-devel
yum -y install postgresql-devel
yum -y install aspell-devel
yum -y install readline-devel
yum -y install libxslt-devel
yum -y install net-snmp-devel
yum -y install unixODBC-devel
yum -y install libicu-devel
yum -y install libc-client-devel
yum -y install libXpm-devel
yum -y install libvpx-devel
yum -y install enchant-devel
yum -y install openldap
yum -y install openldap-devel
yum -y install db4-devel
yum -y install gmp-devel
yum -y install wget
yum -y install make
yum -y install gcc
yum -y install gcc-c++
yum -y install zlib
yum -y install zlib-devel
yum -y install pcre
yum -y install pcre-devel
yum -y install kernel
yum -y install keyutils
yum -y install patch
yum -y install perl
yum -y install tcl
yum -y install GeoIP-devel
yum -y install libevent
yum -y install libevent-devel
yum -y install ncurses-devel
yum -y install cmake
yum -y install libaio
yum -y install bison
# deepin ubuntu
#gcc g++的依赖库
apt-get -y install build-essential
apt-get -y install libtool
#安装pcre依赖库
apt-get -y install libpcre3 libpcre3-dev
#安装zlib依赖库
apt-get -y install zlib1g-dev
#openssl环境 安装ssl依赖库
apt-get -y install libssl-dev
apt-get -y install openssl
apt-get -y install libxml2
apt-get -y install libxml2-dev
apt-get -y install libxslt-dev
apt-get -y install libgd2-xpm-dev
apt-get -y install libgeoip-dev
apt-get -y install libcurl4-gnutls-dev
apt-get -y install libcurl4-gnutls-dev
apt-get -y install gcc build-essential
3. 安装NGINX1.18.0
下载地址:
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=www \
--group=www \
--prefix=/soft/package/nginx-1.18.0 \
--pid-path=/soft/package/nginx-1.18.0/logs/pid \
--error-log-path=/soft/package/nginx-1.18.0/logs/error.log \
--http-log-path=/soft/package/nginx-1.18.0/logs/access.log \
--http-proxy-temp-path=/soft/package/nginx-1.18.0/proxy_temp \
--http-fastcgi-temp-path=/soft/package/nginx-1.18.0/fastcgi_temp \
--http-client-body-temp-path=/soft/package/nginx-1.18.0/client_body_temp \
--with-threads \
--with-file-aio \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_ssl_module \
--with-http_xslt_module \
--with-http_geoip_module \
--with-http_realip_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_secure_link_module \
--with-http_stub_status_module \
--with-http_auth_request_module \
--with-http_random_index_module \
--with-http_image_filter_module \
--without-http_uwsgi_module \
--without-http_scgi_module \
--with-mail \
--with-mail_ssl_module \
--with-stream \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-stream_realip_module \
--with-stream_geoip_module \
--with-stream_ssl_preread_module \
--with-compat \
--with-select_module \
--with-poll_module \
--with-http_v2_module \
--with-http_mp4_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_addition_module
编译安装
make
make install
4. centos 创建service文件
vim /usr/lib/systemd/system/nginx-1.18.0.service
内容如下:
[Unit]
Description=nginx-The High-performance HTTP Server
After=network.target
[Service]
Type=forking
PIDFile=/soft/package/nginx-1.18.0/logs/pid
ExecStartPre=/soft/package/nginx-1.18.0/sbin/nginx -t -c /soft/package/nginx-1.18.0/conf/nginx.conf
ExecStart=/soft/package/nginx-1.18.0/sbin/nginx -c /soft/package/nginx-1.18.0/conf/nginx.conf
ExecReload=/soft/package/nginx-1.18.0/sbin/nginx -s reload
ExecStop=/soft/package/nginx-1.18.0/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
重新加载服务文件
systemctl daemon-reload
启动
systemctl start nginx-1.18.0.service
5. centos 防火墙设置(外网访问
添加80端口:
firewall-cmd --add-port=80/tcp --permanent
更新防火墙规则:
firewall-cmd --reload
6. NGINX配置
进入安装目录:
cd /soft/package/nginx-1.18.0/conf
编辑配置文件:
vim nginx.conf
把httd段下的server段剪切一下,并写入同级目录的80_default.conf文件内(没有此文件就创建)。
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
并替换为:
include 80*.conf;
目的是方便以后扩展:
conf目录下的以80开头并以.conf结尾的文件均为nginx配置文件,方便扩展,就不用每次都改nginx.conf文件而烦恼。
配置PHP
1. NGINX 与 PHP-FPM集成
php-fpm 与 nginx 通信方式有两种:
一种是基于tcp的 internet domain socket 方式。
一种是 unix domain socket 方式。
unix domain socket 可以使同一台操作系统上的两个或多个进程进行数据通信。
unix domain socket 的接口和 internet domain socket 很像,但它不使用网络底层协议来通信。
服务器压力不大的情况下,这两种方式性能差别不大,但在压力比较满的时候,用unix domain socket方式,效果确实比较好。
unix domain socket 方式:
找到php的socket文件路径:
如/soft/package/php-7.2.34/var/run/php-fpm.sock
编辑配置文件(location ~ .php$路由):
cd /soft/package/nginx-1.18.0/conf/
vim nginx.conf
location ~ \.php$ {
fastcgi_pass unix:/soft/package/php-7.2.34/var/run/php-fpm.sock;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
重启服务
NGINX常用命令
nginx加入全局变量后,可以直接使用nginx。
否则则需要将进入nginx的安装目录。
如/soft/package/nginx-1.18.0/sbin/nginx
就是把nginx替换为/soft/package/nginx-1.18.0/sbin/nginx即可
nginx -?,-h #打开帮助信息
nginx -s reopen #重启Nginx
nginx -s reload #重新加载Nginx配置文件,然后以优雅的方式重启Nginx
nginx -s stop #强制停止Nginx服务
nginx -s quit #优雅地停止Nginx服务(即处理完所有请求后再停止服务)
nginx -t #检测配置文件是否有语法错误,然后退出
nginx -v #显示版本信息并退出
nginx -V #显示版本和配置选项信息,然后退出
nginx -t #检测配置文件是否有语法错误,然后退出
nginx -T #检测配置文件是否有语法错误,转储并退出
nginx -q #在检测配置文件期间屏蔽非错误信息
nginx -p prefix #设置前缀路径(默认是:/usr/share/nginx/)
nginx -c filename #设置配置文件(默认是:/etc/nginx/nginx.conf)
nginx -g directives #设置配置文件外的全局指令
killall nginx #杀死所有nginx进程