Nginx是一款高性能、轻量级Web服务软件,稳定性高,系统资源消耗低,对HTTP并发连接的处理能力高,单台物理服务器可支持30000~50000个并发请求
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
###nginx的配置及运行需要pcre、zlib、openssl等软件包的支持,因此需要安装这些软件的开发包,以便提供相应的库和头文件
useradd -M -s /sbin/nologin nginx
cd /opt
tar zxvf nginx-1.12.0.tar.gz -C /opt/
cd nginx-1.12.0/
./configure \
--prefix=/usr/local/nginx \ ###指定nginx的安装路径
--user=nginx \ ###指定用户名
--group=nginx \ ###指定组名
--with-http_stub_status_module ###启用http_stub_status_module模块以支持状态统计
make [-j2] && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/ ###让系统识别nginx的操作命令
nginx -t ###检查配置文件是否配置正确
nginx ###启动
###停止
cat /usr/local/nginx/logs/nginx.pid ###先查看nginx的PID号
kill -3
kill -s QUIT
killall -3 nginx
killall -s QUIT nginx
###重载
kill -1
kill -s HUP
killall -1 nginx
killall -s HUP nginx
kill -USR1 ###日志分割,重新打开日志文件
kill -USR2 ###平滑升级
###新版本升级
tar -zxvf nginx-1.xx.xx.tar.gz
cd nginx-1.xx.xx/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module
make
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx_old
cp objs/nginx /usr/local/nginx/sbin/nginx
make upgrade
###要保证当前nginx进程是通过/usr/local/nginx/sbin/nginx启动的,而不是通过查找环境变量中那个nginx命令启动的,或者先killall nginx,再/usr/local/nginx/sbin/nginx
方法一:
vim /etc/init.d/nginx.sh
#!/bin/bash
#chkconfig: - 99 20
#description:Nginx Service Control Script
COM="/usr/local/nginx/sbin/nginx"
PID="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$COM
;;
stop)
kill -s QUIT $(cat $PID)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PID)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
chmod +x /etc/init.d/nginx
chkconfig --add nginx ###添加为系统服务
systemctl stop nginx
systemctl start nginx
方法二:
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
--------------------------------------------------------------
[Unit]:服务的说明
Description:描述服务
After:依赖,当依赖的服务启动之后再启动自定义的服务
[Service]服务运行参数的设置
Type=forking是后台运行的形式,使用此启动类型应同时指定PIDFile=,以便systemd能够跟踪服务的主进程。
ExecStart为服务的具体运行命令
ExecReload为重启命令
ExecStop为停止命令
PrivateTmp=True表示给服务分配独立的临时空间
注:启动、重启、停止命令全部要求使用绝对路径
[Install]服务安装的相关设置,可设置为多用户
全局块:全局配置,对全局生效
events块:配置影响Nginx服务器与用户的网络连接
http块:配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置
server块:配置虚拟主机的相关参数,一个http块中可以有多个server块
location块:用于配置匹配的uri
upstream:配置后端服务器具体地址,负载均衡配置不可或缺的部分
vim /usr/local/nginx/conf/nginx.conf
全局配置:
#user nobody; ###运行用户,若编译时未指定则默认为nobody
worker_processes 1; ###工作进程数量,一般设置为和CPU核数一样;设置为auto,nginx将会自己获取这个数值
#error_log logs/error.log; ###错误日志文件的位置
#pid logs/nginx.pid; ###PID 文件的位置
worker_rlimit_nofile 60000; ###设置所有worker进程最大可以打开的文件数,默认为1024
I/O事件配置:
events {
use epoll; ###使用epoll I/O模型,2.6及以上版本的系统内核,建议使用epoll模型以提高性能
worker_connections 60000; ###每个进程处理60000个连接
multi_accept on; ###是否一次性将监听到的连接全接收进来,默认为off,关闭时一次接收一条连接
accept_mutex on; ###默认为on,开启时表示以串行方式接入新连接,否则将通报给所有worker,这可能会浪费资源并产生不可预计的后果,例如惊群问题
}
###如提高每个进程的连接数还需执行“ulimit -n 65535”命令临时修改本地每个进程可以同时打开的最大文件数
###在Linux平台上,在进行高并发TCP连接处理时,最高的并发数量都要受到系统对用户单一进程同时可打开文件数量的限制(这是因为系统为每个TCP连接都要创建一个socket句柄,每个socket句柄同时也是一个文件句柄)
###可使用ulimit -a命令查看系统允许当前用户进程打开的文件数限制
###epoll是Linux内核为处理大批句柄而作改进的poll,是Linux下多路复用IO接口select/poll的增强版本,它能显著的减少程序在大量并发连接中只有少量活跃的情况下的系统CPU利用率
###worker_processes的值和work_connections的值决定了最大并发数量,最大并发数计算方式为:worker_processes*worker_connections。但在反向代理场景中计算方法不同,因为nginx既要维持和客户端的连接,又要维持和后端服务器的连接,因此处理一次连接要占用2个连接,所以最大并发数计算方式为:worker_processes*worker_connections/2
HTTP配置:
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"';
#access_log logs/access.log main; ###访问日志位置
sendfile on; ###开启文件传输模式
#tcp_nopush on; ###只在sendfile on时有效。调用tcp_cork方法,让数据包不会马上传送出去,等到数据包最大时,一次性的传输出去,这样有助于解决网络堵塞。默认为off
#keepalive_timeout 0; ###连接保持超时时间,单位是秒
keepalive_timeout 65;
#gzip on; ###gzip模块设置,设置是否开启gzip压缩输出
server { ###Web服务的监听配置
listen 80; ###监听地址及端口
server_name www.xc.com; ###站点域名,可以有多个,用空格隔开
charset utf-8; ###网页的默认字符集
location / { ###根目录配置
root html; ###网站根目录的位置/usr/local/nginx/html
index index.html index.php; ###默认首页文件名
}
error_page 500 502 503 504 /50x.html; ###内部错误的反馈页面
location = /50x.html { ###错误页面配置
root html;
}
}
}
日志格式设定:
$remote_addr与$http_x_forwarded_for用以记录客户端的ip地址
$remote_user:用来记录客户端用户名称
$time_local: 用来记录访问时间与时区
$request: 用来记录请求的url与http协议
$status: 用来记录请求状态;成功是200
$body_bytes_sent :记录发送给客户端文件主体内容大小
$http_referer:用来记录从哪个页面链接访问过来的
$http_user_agent:记录客户浏览器的相关信息
通常web服务器放在反向代理的后面,这样就不能获取到客户的IP地址了,通过$remote_add拿到的IP地址是反向代理服务器的iP地址。反向代理服务器在转发请求的http头信息中,可以增加x_forwarded_for信息,用以记录原有客户端的IP地址和原来客户端的请求的服务器地址
location常见配置指令,root、alias、proxy_pass
root(根路径配置):root /var/www/html
请求www.xc.com/test/1.html,会返回文件/var/www/html/test/1.html
alias(别名配置):alias /var/www/html
请求www.xc.com/test/1.html,会返回文件/var/www/html/1.html
proxy_pass(反向代理配置)
先使用命令/usr/local/nginx/sbin/nginx -V查看已安装的Nginx是否包含HTTP_STUB_STATUS模块
cat /opt/nginx-1.12.0/auto/options | grep YES ###可查看nginx已安装的所有模块
修改nginx.conf配置文件,指定访问位置并添加stub_status配置
cd /usr/local/nginx/conf
cp nginx.conf nginx.conf.bak
vim /usr/local/nginx/conf/nginx.conf
http {
server {
listen 80;
server_name www.kgc.com;
charset utf-8;
location / {
root html;
index index.html index.php;
}
###添加stub_status配置
location /status { ###访问位置为/status
stub_status on; ###打开状态统计功能
access_log off; ###关闭此位置的日志记录
}
systemctl restart nginx
浏览器访问 http://192.168.230.3/status
Active connections:表示当前的活动连接数,即当前与 Nginx 服务器建立的连接数
server accepts handled requests :表示已经处理的连接信息,三个数字依次表示服务器已接收的连接数;服务器成功处理的连接数;服务器累计处理的总请求数(在保持连接模式下,请求数量可能会大于连接数量)
Reading:表示当前正在从客户端读取数据的连接数
Writing:表示当前正在向客户端写入数据的连接数
Waiting:表示当前空闲并等待请求的连接数
可curl -Ls http://192.168.230.3/status 结合awk与if语句进行性能监控
yum install -y httpd-tools
htpasswd -c /usr/local/nginx/passwd.db zhangsan
chown nginx /usr/local/nginx/passwd.db
chmod 400 /usr/local/nginx/passwd.db
vim /usr/local/nginx/conf/nginx.conf
server {
location / {
###添加以下认证配置
auth_basic "secret"; ###设置密码提示框文字信息
auth_basic_user_file /usr/local/nginx/passwd.db;
nginx -t
systemctl restart nginx
浏览器访问 http://192.168.230.3
访问控制规则如下:
deny IP/IP 段:拒绝某个 IP 或 IP 段的客户端访问
allow IP/IP 段:允许某个 IP 或 IP 段的客户端访问
规则从上往下执行,如匹配则停止,不再往下匹配
vim /usr/local/nginx/conf/nginx.conf
server {
location / {
###添加控制规则
allow 192.168.230.5; ###允许访问的客户端IP
deny all; ###拒绝其它IP客户端访问
systemctl restart nginx
echo "192.168.230.3 www.xc.com" >> /etc/hosts
mkdir -p /var/www/html/xc
echo "www.xc.com
" > /var/www/html/xc/index.html
vim /usr/local/nginx/conf/nginx.conf
http {
server {
listen 80;
server_name www.xc.com; ###置域名www.xc.com
charset utf-8;
access_log logs/www.xc.access.log; ###设置日志名
location / {
root /var/www/html/xc; ###设置www.xc.com 的工作目录
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 80;
server_name www.benet.com; #设置域名www.benet.com
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
systemctl restart nginx
浏览器访问 http://www.xc.com
ifconfig ens33:0 192.168.230.10 netmask255.255.255.0
vim /usr/local/nginx/conf/nginx.conf
http {
server {
listen 192.168.230.3:80; ###设置监听地址192.168.230.3
server_name www.xc.com;
charset utf-8;
access_log logs/www.xc.access.log;
location / {
root /var/www/html/xc;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.230.10:80;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
systemctl restart nginx
浏览器访问
http://192.168.230.3
http://192.168.230.10
vim /usr/local/nginx/conf/nginx.conf
http {
server {
listen 192.168.230.3:8080; ###设置监听8080端口
server_name www.xc.com;
charset utf-8;
access_log logs/www.xc.access.log;
location / {
root /var/www/html/xc;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.230.3:8888;
server_name www.benet.com;
charset utf-8;
access_log logs/www.benet.access.log;
location / {
root /var/www/html/benet;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
systemctl restart nginx
浏览器访问
http://192.168.230.3:8080
http://192.168.230.3:8888
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
yum -y install pcre-devel zlib-devel gcc gcc-c++ make
useradd -M -s /sbin/nologin nginx
cd /opt
tar xf nginx-1.24.0.tar.gz
cd nginx-1.24.0/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
make [-j2] && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
yum -y install \
ncurses \
ncurses-devel \
bison \
cmake
useradd -M -s /sbin/nologin mysql
cd /opt
tar zxvf mysql-boost-5.7.20.tar.gz
cd /opt/mysql-5.7.20/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
make [-j2] && make install
vim /etc/my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf
echo 'export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
source /etc/profile
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
systemctl daemon-reload
systemctl start mysqld.service
systemctl enable mysqld
mysqladmin -u root -p password "123"
###Enter键确认即可
mysql -u root -p
yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel
cd /opt
tar xf php-7.1.10.tar.bz2
cd php-7.1.10/
./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip
make [-j2] && make install
ln -s /usr/local/php/bin/* /usr/local/bin/
ln -s /usr/local/php/sbin/* /usr/local/sbin/
调整主配置文件:
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
vim /usr/local/php/lib/php.ini
date.timezone = Asia/Shanghai ###939行取消注释,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock ###1170行修改
php -m ###验证安装的模块
调整进程服务配置文件:
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
vim php-fpm.conf
pid = run/php-fpm.pid ###17行去掉";"注释
调整扩展配置文件:
cd /usr/local/php/etc/php-fpm.d/
cp www.conf.default www.conf
/usr/local/php/sbin/php-fpm -c /usr/local/php/lib/php.ini
netstat -anpt | grep 9000
cd /opt/php-7.1.10/sapi/fpm
cp php-fpm.service /usr/lib/systemd/system/php-fpm.service
systemctl restart php-fpm.service
###PHP-FPM(FastCGI Process Manager:FastCGI 进程管理器)是一个PHPFastCGI管理器, 由于Nginx服务器不能处理动态页面,需要由Nginx把动态请求交给php-fpm进程进行解析
vim /usr/local/nginx/conf/nginx.conf
###65行取消注释,修改
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
###将/scripts修改为nginx的工作目录
###fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ###$document_root 代表当前请求在root指令中指定的值
include fastcgi_params;
}
systemctl restart nginx.service
vim /usr/local/nginx/html/index.php
浏览器访问
http://192.168.230.3/index.php
mysql -u root -p ###输入密码
show databases;
CREATE DATABASE bbs;
GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
flush privileges;
show databases;
quit
###替换原来的测试页内容
vim /usr/local/nginx/html/index.php
Success!!";
else echo "Fail!!";
?>
浏览器访问
http://192.168.230.3/index.php
cd /opt
unzip Discuz_X3.4_SC_UTF8.zip -d /opt/dis
cd /opt/dis/dir_SC_UTF8/
cp -r upload/ /usr/local/nginx/html/bbs/
调整论坛目录的权限:
cd /usr/local/nginx/html
chown -R nobody bbs/
cd /usr/local/nginx/html/bbs/
chown -R nginx config/
chown -R nginx data/
chown -R nginx uc_client/
chown -R nginx uc_server/
论坛页面访问
http://192.168.230.3/bbs/install/index.php
数据库服务器:localhost ###本地架设就用localhost,如何不是在在本机上就要填写IP地址和端口号
数据库名字:bbs
数据库用户名:bbsuser
数据库密码:admin123
访问论坛页面:
http://192.168.230.3/bbs/index.php
Nginx的PHP解析功能实现如果是交由FPM处理的,为了提高PHP的处理速度,可对FPM模块进行参数的调整,根据服务器的内存与服务负载,调整FPM模块参数
vim /usr/local/php/etc/php-fpm.conf
pid = run/php-fpm.pid
vim /usr/local/php/etc/php-fpm.d/www.conf
pm = dynamic ###96行,fpm进程启动方式,动态的
pm.max_children=20 ###107行,fpm进程启动的最大进程数
pm.start_servers = 5 ###112行,动态方式下启动时默认开启的进程数,在最小和最大之间
pm.min_spare_servers = 2 ###117行,动态方式下最小空闲进程数
pm.max_spare_servers = 8 ###122行,动态方式下最大空闲进程数
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid` ###重启php-fpm
netstat -anpt | grep 9000
ps -elf | grep php-fpm