1.Nginx是一个轻量级、高性能的web服务器软件
(1)Nginx安装依赖包:gcc、gcc-c++、openssl、openssl-devel、pcre、pcre-devel、zlib、zlib-devel
1)查看是否安装:yum list installed | grep gcc
安装命令: yum install -y gcc gcc-c++
(2)上传软件包并解压(将nginx-1.22.1.tar.gz上传到服务器的/root目录下)
解压软件包:tar -zxvf nginx-1.22.1.tar.gz -C /usr/local/src/
(3)创建虚拟用户、预编译、安装
创建Nginx运行账户www用户并加入到www用户组,不允许www用户直接登录系统,其中--prefix选项是指定Nginx的安装路径
[root@web01 ~]# groupadd www #创建www用户组
[root@web01 ~]# useradd www -g www -s /sbin/nologin
[root@web01 ~]# cd /usr/local/src/nginx-1.22.1/
[root@web01 nginx-1.22.1]# ./configure --prefix=/usr/local/nginx --user=www --group=www
[root@web01 nginx-1.22.1]# make && make install
(4)启动、关闭、重载、停止Nginx
1).启动:切换到Nginx安装目录的sbin目录下,执行:
[root@web01 ~]# cd /usr/local/nginx/sbin
[root@web01 sbin]# ./nginx //运行脚本
[root@web01 ~]# netstat -antlp | grep 80 //查看端口号(最右列出现master成功)
2)关闭
[root@web01 ~]# ps -ef | grep nginx //查看nginxPID进程
[root@web01 ~]# kill -9 PID
3)重载
[root@web01 ~]# nginx -s reload 或 [root@web01 ~]# killall -1 nginx 或
[root@web01 ~]# killall -s HUP nginx
4)停止
[root@web01 ~]# nginx -s stop 或 [root@web01 ~]# killall -3 nginxi 或
[root@web01 ~]# killall -s QUIT nginx
(5)对Nginx配置进行检查
[root@web01 ~]# /usr/local/nginx/sbin/nginx -t
(6)优化Nginx命令执行路径
[root@web01 ~]# ln -s /usr/local/nginx/sbin/nginx /usr/sbin/
[root@web01 ~]# nginx -t
[root@web01 ~]# echo $PATH
(7)网站测试
需要事先关闭firewalld防火墙和SELinux,在物理机客户端浏览器的地址栏中执行,如下所示:
http://192.168.30.11:80 出现欢迎界面
2.Nginx配置文件
Nginx的/usr/local/nginx/cong/nginx.conf配置文件主要包括全局配置、I/O事件配置、HTTP配置三部分组成;配置格式为“关键字” 值
路径简记:用户本地Nginx配置中的Nginx配置文件
(1)全局配置(运行用户、工作进程数、错误日志、PID存放位置等基本设置,常用配置项)
运行用户,Nginx的运行用户实际是编译时指定的www,若编译时未指定则默认为nobody,即它是维护Nginx运行的默认用户
#user nobody;
指定Nginx启动的工作进程数量,建议按照CPU核数来指定,一般为CPU核数或CPU核数的倍数,即它是Nginx的工作进程数
worker_processes 1;
这个配置项是指在一个Nginx工作进程中可以打开的最多文件的数目,与ulimit -n的值保持一致
#worker_rlimit_nofile 102400;
Nginx错误日志记录
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
Nginx进程的pid存放的文件
#pid logs/nginx.pid;
(2)I/O事件配置
使用“events {}”来界定标记,用来指定Nginx进程的I/O响应模型,每个进程的连接数等设置
events {
//Nginx底层采用epoll事件处理机制,提高单个进程的并发性
use epoll;
每个工作进程允许的最大连接数(默认为1024)Nginx的最大连接数为:worker进程数量 * 单个worker进程的最大连接数
worker_connections 1024;
}
(3)HTTP配置
使用“http{}”界定标记,包括访问日志、HTTP端口、网页目录、默认字符集、连接保持、以及虚拟主机、PHP解析等一系列设置。
其中大部分配置语句包含在子界定标记“server {}”内
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;
//keepalive超时时间
keepalive_timeout 65;
//压缩
#gzip on;
//设定虚拟主机,即server关键字是用来配置虚拟主机的
server {
//web服务的监听端口
listen 80;
//网站名称
server_name www.lnmp.com;
//字符集格式
charset utf-8;
//记录访问成功的日志
access_log logs/host.access.log main;
//location的功能是用来匹配不同的URI请求,进而对请求做不同的处理和响应
location / {
//网页根目录位置,默认为Nginx安装目录下的html子目录,root语句用来设置默认网页文档存放的位置,根据需要可改为/var/www/html等其它路径
root html;
//网站的默认首页面
index index.html index.htm;
}
#error_page 404 /404.html;
//状态码为500 502 503 504,访问50x.html页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
(4)配置完后需检查
[root@web01 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
或
[root@web01 ~]# nginx -t
(5)检查无误后可重启让配置文件起效
[root@web01 ~]# nginx -s reload 或 [root@web01 ~]# killall -1 nginx 或
[root@web01 ~]# killall -s HUP nginx
3.虚拟主机
虚拟主机是指在一台服务器上运行多个web站点,虚拟主机的类型包括:基于域名的虚拟主机、基于端口号的虚拟主机和基于IP地址的虚拟主机
(1)配置网站IP
[root@web01 ~]# vi /etc/hosts
[root@web01 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.30.11 www.benet.com
192.168.30.11 www.accp.com
(2)建立网站存放目录、制作一个简单网页
[root@web01 ~]# mkdir -p /var/www/html/benetcom
[root@web01 ~]# mkdir -p /var/www/html/accpcom
[root@web01 ~]# echo "<h1>www.benet.com</h1>" > /var/www/html/benetcom/index.html
[root@web01 ~]# echo "<h1>www.accp.com</h1>" > /var/www/html/accpcom/index.html
(3)修改配置文件、备份、一个虚拟主机就是一个server{}
[root@web01 ~]# cd /usr/local/nginx/conf
[root@web01 ~]# cp -rf nginx.conf nginx.conf.bak
[root@web01 ~]# vi nginx.conf
worker_processes 1;
events {
use epoll;
worker_connections 1024;
}
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;
server {
listen 80;
server_name www.benet.com;
charset utf-8;
access_log logs/benet.com.access.log main;
location / {
root /var/www/html/benetcom;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.accp.com;
charset utf-8;
access_log logs/accp.com.access.log main;
location / {
root /var/www/html/accpcom;
index index.html index.htm;
}
}
}
(4)每次配置完成后都要检查是否有错误
[root@web01 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
或
[root@web01 ~]# nginx -t
(5)测试访问
1)在物理机客户端Windows上修改hosts文件,添加web站点域名和IP地址的映射
C:\Windows\System32\drivers\etc\hosts //可修改文件属性来写入保存
192.168.30.11 www.benet.com
192.168.30.11 www.accp.com
2)在物理机客户端Windows主机上打开浏览器测试访问