NGINX
编译安装nginx:
# yum -y install pcre pcre-devel
安装pcre是为了使nginx支持URI重写功能的rewrite模块。
# yum -y install openssl openssl-devel
Nginx在使用HTTPS时用到此模块,若不安装openssl相关包,安装nginx会报错。
# yum -y install gcc
# wget http://nginx.org/download/nginx-1.16.1.tar.gz //下载稳定版的nginx源码包
# useradd nginx -s /sbin/nologin -M
# tar -xf nginx-1.16.1.tar.gz
# cd nginx-1.16.1
# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx1.16.1 --with-http_stub_status_module --with-http_ssl_module
# meke
# make install
# ln -s /usr/local/nginx-1.16.1 /usr/local/nginx
# /usr/local/nginx/sbin/nginx -t //启动前检查配置文件语法
# lsof -i :80 //查看nginx对应的端口是否启动成功
# netstat -lnt|grep 80
Nginx虚拟主机配置:
1.基于域名的虚拟主机:
# cd /usr/local/nginx
# diff conf/nginx.conf.default conf/nginx.conf //初始这两个文件是一致的
# egrep -V "#|^$" conf/nginx.conf.default>conf/nginx.conf
# vim conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
}
# mkdir html/www -p
# echo "http://www.etiantian.org" > html/www/index.html
# sbin/nginx -t
# sbin/nginx -s reload
# curl www.etiantian.org //先在访问的客户端做hosts解析
http://www.etiantian.org
2.基于端口的虚拟主机
# vim conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 81;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
}
# mkdir html/blog -p
# echo "http://blog.etiantian.org" > html/blog/index.html
# sbin/nginx -t
# sbin/nginx -s reload
# curl blog.etiantian.org:81 //先在访问的客户端做hosts解析
http://blog.etiantian.org
3.基于IP的虚拟主机
# vim conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 81;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
server {
listen 10.0.0.9:82;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
}
# mkdir html/bbs -p
# echo "http://bbs.etiantian.org" > html/bbs/index.html
# sbin/nginx -t
# sbin/nginx -s reload
# curl 10.0.0.9:82
http://bbs.etiantian.org
# mkdir conf/extra
# sed -n '10,17p' conf/nginx.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
# sed -n '10,17p' conf/nginx.conf > conf/extra/www.conf
# sed -n '18,25p' conf/nginx.conf
server {
listen 81;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}
# sed -n '18,25p' conf/nginx.conf > conf/extra/blog.conf
# sed -n '26,33p' conf/nginx.conf
server {
listen 10.0.0.9:82;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
# sed -n '26,33p' conf/nginx.conf > conf/extra/bbs.conf
# sed -i '10,33d' conf/nginx.conf
# vim conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/blog.conf;
include extra/bbs.conf;
}
# sbin/nginx -t
# sbin/nginx -s reload
# tail -1 /etc/hosts
10.0.0.9 www.etiantian.org blog.etiantian.org bbs.etiantian.org
# curl www.etiantian.org
# curl blog.etiantian.org:81
# curl 10.0.0.9:82
301跳转:
以往我们是通过别名的方式实现etiantian.org访问同一个地址的,事实上,撤了这个方式外,还可以使用Nginx rewrite 301跳转的方式来实现:
# vim conf/extra/www.conf
server {
listen 80;
server_name etiantian.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent; //或者rewrite ^(.*)$ http://www.etiantian.org permanent;
}
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
访问认证:
# vim conf/extra/www.comf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
auth_basic "etiantian";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
}
# htpasswd -cm /usr/local/nginx/htpasswd tom // -c 创建用户文件 -m默认采用md5加密
输入密码
# htpasswd -m /usr/local/nginx/htpasswd jerry //追加用户,不使用-c选项
输入密码
# firefox www.etiantian.com
输入认证用户及密码
SSL虚拟主机:
[root@svr5 ~]# cd /usr/local/nginx/conf
[root@svr5 conf]# openssl genrsa -out cert.key //生成私钥
[root@svr5 conf]# openssl req -new -x509 -key cert.key -out cert.pem //生成证书
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
… …
server {
listen 443 ssl;
server_name www.etiantian.com;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload
Nginx错误日志配置:
# vim conf/nginx.conf
worker_processes 1;
error_log logs/error.log; //配置这一行即可,默认日志级别为error
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
Nginx访问日志:
# sed -n '21,23s/#//gp' nginx.conf.defaut
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# vim conf/nginx.conf
worker_processes 1;
events {
worker_connections 1024;
error_log logs/error.log
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' //添加日志格式
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
sendfile on;
keepalive_timeout 65;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
# vim conf/extra/www.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
auth_basic "etiantian";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
access_log logs/access_www.log main; //添加访问日志
}
对访问日志进行日切割:
# vim cut_nginx_log.sh
#!/bin/bash
Dateformat=`date +%y%m%d`
Basedir="/usr/local/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload
# crontab -e
00 00 * * * /bin/sh /usr/local/nginx/cut_nginx_log.sh > /dev/null 2>&1
LNMP组合工作原理:
当LNMP组合工作时,首先是用户通过浏览器输入域名请求Nginx Web服务,如果是静态资源,则由Nginx解析返回给用户;如果是动态资源,那么Nginx就会把它通过FastCGI接口发送给PHP引擎服务(FastCGI进程php-fpm)进行解析,如果这个动态请求要读取数据库数据,那么PHP就会继续向后请求MySQL数据库,以读取数据,并最终通过Nginx服务把获取的数据返回给用户。
LNMP之PHP(FastCGI方式)服务的安装准备:
每个lib一般都会存在对应的以“*-devel*”命名的包,安装lib对应的-devel包后,对应的lib包就会自动安装。
# yum -y install zlib-devel libxm12-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
# yum -y install freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel
# rpm -qa //发现仅libiconv-devel没有安装,因为默认yum源没有此包,我们采用编译安装
# wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.16.tar.gz
# tar -zxf libiconv-1.16.tar.gz
# cd libiconv-1.16
# ./configure --prefix=/usr/local/libiconv
# make && make install
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
# yum -y install libmcrypt-devel
# yum -y install mhash //安装mhash加密扩展库
# yum -y install mcrypt //安装工mcrypt加密扩展库
安装PHP
# wget https://www.php.net/distributions/php-7.3.10.tar.gz
# tar -xf php-7.3.10.tar.gz
# cd php-7.3.10
# ./configure --prefix=/usr/local/php7.3.10 --with-mysql=/usr/local/mysql --with-iconv-dir=/usr/local/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-fpm-user=nginx --with-fpm-group=nginx --enable-ftp
编译常见问题解决:
{
configure: error: libxml2 not found. Please check your libxml2 installation.
|
1 |
|
configure: error: Please reinstall the BZip2 distribution
|
1 |
|
configure: error: cURL version 7.15.5 or later is required to compile php with cURL support
|
1 |
|
configure: error: jpeglib.h not found.
|
1 |
|
configure: error: png.h not found.
|
1 |
|
configure: error: freetype-config not found.
|
1 |
|
configure: error: xslt-config not found. Please reinstall the libxslt >= 1.1.0 distribution
|
1 |
|
configure: error: Please reinstall the libzip distribution
|
1 |
|
(
checking for libzip... configure: error: system libzip must be upgraded to version >= 0.11
|
1 2 3 4 5 6 7 8 9 |
|
如果是下载1.5.*以上版本,则需要采用如下安装方式
yum -y install cmake
wget https://libzip.org/download/libzip-1.5.1.tar.gz
tar -zxvf libzip-1.5.1.tar.gz
cd libzip-1.5.1
mkdir build
cd build
cmake ..
make && make install
cmake报错升级
# wget https://github.com/Kitware/CMake/releases/download/v3.14.7/cmake-3.14.7-Linux-x86_64.tar.g
# tar -zxf cmake-3.14.7-Linux-x86_64.tar.gz
# vim /etc/profile
export CMAKE_HOME=/opt/cmake-3.14.7-Linux-x86_64/bin //在文件末尾追加
# source /etc/profile
# vim /etc/bashrc //设置环境变量生效
export CMAKE_HOME=/opt/cmake-3.14.7-Linux-x86_64/bin
)
off_t undefined 报错
echo '/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64'>>/etc/ld.so.conf
# ldconfig -v //更新配置
}
# make && make install
# ln -s /usr/local/php7.3.10 /usr/local/php
# cp php.ini-production /usr/local/php/lib/php.ini
# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
# cp /usr/locl/php/etc/php-fpm.d/www.conf.default /usr/locl/php/etc/php-fpm.d/www.conf
# sed -n "65,71s/#//p" /usr/local/nginx/conf/nginx.conf.default
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;
}
# vim /usr/local/nginx/conf/extra/www.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
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.conf;
}
}
# echo '<?php phpinfo();?>' > /usr/local/nginx/html/www/test.php
# firefox www.etiantian.org/test.php //可查看到php信息页
Nginx优化:
# cd /usr/local/nginx
关闭nginx版本号显示:
# vim conf/extra/www.conf
http
{
......
server_tokens off; //关闭http respon header内的Web服务版本信息显示,及错误嘻嘻中的Web服务版本信息显示
......
}
更改nginx软件名及版本号:
# vim nginx-1.61.1/src/core/nginx.h
13 #define NGINX_VERSION "1.16.1" //修改1.16.1为想要的版本号,如3.00.0
14 #define NGINX_VER "nginx/" NGINX_VERSION //修改nginx为想要的软件名称,如ows
15
16 #ifdef NGX_BUILD
17 #define NGINX_VER_BUILD NGINX_VER " (" NGX_BUILD ")"
18 #else
19 #define NGINX_VER_BUILD NGINX_VER
20 #endif
21
22 #define NGINX_VAR "NGINX" //修改NGINX为想要的软件名称,如OWS
23 #define NGX_OLDPID_EXT ".oldbin"
# grep -n 'Server: nginx' ngx_http_header_filter_module.c
49 static u_char ngx_http_server_string[] = "Server: nginx" CRLF; //修改nginx为想要的软件名称,如ows
# vim nginx-1.16.1/src/http/ngx_http_special_response.c
22 "<hr><center>" NGINX_VER "</center>" CRLF //修改"NGINX_VER"为"NGINX_VER"(http://oldboy.blog.51cto.com)
23 "</body>" CRLF
24 "</html>" CRLF
25 ;
26
27
28 static u_char ngx_http_error_build_tail[] =
29 "<hr><center>" NGINX_VER_BUILD "</center>" CRLF
30 "</body>" CRLF
31 "</html>" CRLF
32 ;
33
34
35 static u_char ngx_http_error_tail[] =
36 "<hr><center>nginx</center>" CRLF //修改nginx为ows
这三个文件修改后再编译安装nginx,如果是已安装好的服务,需要重新编译安装。
优化nginx服务的woker进程个数:
woker_processes 1; //指定了Nginx要开启的进程数,设置其等于cpu核数
# grep processor /proc/cpuinfo|wc -l //grep -c processor /proc/cpuinfo ,top按1显示多核cpu
4 //表示1颗CPU4核
# grep 'physical id' /proc/cpuinfo | sort | uniq | wc -l
1 //表示1颗CPU
# ps -ef | grep nginx |grep -v grep 出现的nginx: woker process数即为开启的进程数,nginx: master process 为主管理进程
Nginx反向代理:
.. ..
http {
.. ..
upstream webserver {
# ip_hash; 相同客户端访问相同Web服务器,后面不会轮询
server 192.168.2.100 weight=1 max_fails=2 fail_timeout=10;
server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10 #down 关闭此条;
}
.. ..
server {
listen 80;
server_name www.etiantian.com;
location / {
proxy_pass http://webserver;
}
}
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s reload
[root@client ~]# curl http://10.0.0.9 //使用该命令多次访问查看轮询效果
Nginx分配方式:
轮询(默认)
weight 给定访问权重
ip_hash 根据客户端IP分配固定的后端服务器
状态类型:
max_fails 允许请求失败的次数(默认为1)
fail_timeout max_fails失败后,暂停提供服务的时间
backup 备份服务器
down 停用状态,不参与负载
浙公网安备 33010602011771号