Nginx服务器
Nginx服务
1. Nginx简介
- Nginx是一个高性能的Web和反向代理服务器
- Nginx支持HTTP、HTTPS和电子邮件代理协议
- OpenResty是Nginx重要的发行版本,是基于Nginx和Lua实现的Web应用网关,集成了大量的第三方模块
2. Nginx服务
2.1 安装OpenResty
yum install yum-utils -y
# 添加openresty的repo
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum install openresty -y
2.2 OpenResty的目录
Nginx目录:/usr/local/openresty/nginx/
- 主程序目录:sbin
- 配置文件目录:conf
- 日志文件目录:logs
- 网站根目录:html
配置文件
/usr/local/openresty/nginx/conf/nginx.conf
worker_processes 1; # nginx worker进程数,如果用户量大,需要增加,建议和CPU逻辑核心数相同
events {
worker_connections 1024; # 每个worker进程能支持的并发连接数
}
http {
# http的统一配置,对所有server生效
# 一般最多会修改日志格式
server { # 每个服务的配置
listen 80; # 监听端口
server_name localhost; # 域名,多域名可以配置为某个域名监听某个端口
charset utf-8; # 页面呈现编码
location / { # 定义URL
root html; # 网站根目录
index index.php index.html index.htm;
}
}
}
一般修改完配置文件,需要做语法检查
nginx -t
启动nginx
nginx
# 修改配置文件后
nginx -s reload
2.3 典型场景
- 基于域名的虚拟主机配置
server {
listen 80;
server_name www.servera.com;
location / {
root html/servera;
index index.html;
}
}
server {
listen 80;
server_name www.serverb.com;
location / {
root html/serverb;
index index.html;
}
}
测试时,可以暂时使用/etc/hosts
文件进行临时域名解析。
192.168.74.11 www.servera.com www.serverb.com
- LNMP环境
- 数据库安装
yum install mariadb mariadb-server -y
修改数据库默认编码
vi /etc/my.cnf
# 在[mysqld]下添加
character_set_server=utf8
init_connect='SET NAMES utf8'
启动数据库
systemctl start mariadb
- PHP安装
yum install php-fpm php-mysql -y
启动php-fpm
systemctl start php-fpm
- Nginx配置
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
- 添加一个PHP测试页面
vi /usr/local/openresty/nginx/html/index.php
<?php
phpinfo();
?>