CentOS7-安装Nginx
1、需要的依赖包
- pcre-8.32.tar.gz 正则表达式库
- openssl-1.0.2t.tar.gz 进行安全通信
- openssl-devel 编译的时候连接的库之类的文件
- zlib-1.2.7.tar.gz 提供数据压缩用的函式库
- zlib-devel 编译依赖
- make 编译
- gcc-c++ C语言编译器
- libtool 通用库支持脚本
上面的依赖包,我在虚拟机中安装系统时,可能是选择了 开发工具,所以就都给我装上了
虽然显示有了,但在 nginx文件下内,执行./configure时报错了
于是又使用 yum 一键安装四个依赖
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2、安装 Nginx
Nginx 下载地址
https://nginx.org/en/download.html
1、 在 / 目录下,新建 data 文件夹,将 nginx-1.12.2.tar.gz,复制到 /data目录下;
2、使用 tar -zxvf nginx-1.12.2.tar.gz 解压缩;
3、进入到解压的 nginx-1.12.2文件夹内,执行 ./configure
4、使用 make && make install 编译、安装后,在 /usr/local/ 目录下,生成 nginx文件夹;
5、在 /usr/local/nginx/sbin/ 目录下,执行 ./nginx 运行 Nginx;
6、使用 ps -ef | grep nginx 查看进程信息;
3、本地访问
- 在本地浏览器中,通过虚拟机CentOS7的IP访问;
- 发现访问不了,没关系不要慌,因为被 防火墙挡住了;
-- 查看 防火墙开放的端口
firewall-cmd --list-all
nginx默认使用的是 80端口,那我们就把它加上去;
-- 防火墙新增,可通行的 80端口
--permanent表示永久生效,不加这个参数的话只会针对本次执行完命令生效,重启后就不管用了
firewall-cmd --add-port=80/tcp --permanent
-- 刷新配置
firewall-cmd --reload
-- 再次查看防火墙允许端口通行列表
firewall-cmd --list-all
本地浏览器,再次访问
4、 防火墙配置指令
-- 查看 防火墙开放的端口
firewall-cmd --list-all
-- 防火墙新增可通行的 80端口
--permanent表示永久生效,不加这个参数的话只会针对本次执行完命令生效,重启后就不管用了
firewall-cmd --add-port=80/tcp --permanent
-- 刷新配置,不然新增的端口配置,不会生效
firewall-cmd --reload
5、nginx常用指令
-- nginx指令使用前提:必须在 /usr/local/nginx/sbin 目录下
-- 查看 nginx的版本号
./nginx -v
-- 启动 nginx
./nginx
-- 关闭 nginx
./nginx -s stop
-- 重新加载 nginx
./nginx -s reload
总结
-- nginx指令使用前提:必须在 /usr/local/nginx/sbin 目录下
-- 查看 nginx的版本号
./nginx -v
-- 启动 nginx
./nginx
-- 关闭 nginx
./nginx -s stop
-- 重新加载 nginx
./nginx -s reload
6、nginx配置文件
nginx配置文件位置:/usr/local/nginx/conf/ nginx.conf文件
第一部分 全局块
#user nobody;
# Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,
# 可以支持的并发处理量越多,但会受到硬件、软件等设备的制约;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
第二部分 events 块
# 涉及的指令主要影响 Nginx服务器与用户的网络连接
events {
# 支持的最大连接数
worker_connections 1024;
}
第三部分 http 块
Nginx服务器配置最频繁的部分,HTTP 块可以包括 http全局块、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_timeout 0;
keepalive_timeout 65;
# 开启gzip压缩输出
#gzip on;
# 全局 server 块
# 每个 server 块就相当于一个虚拟主机
# 可以包含多个 location 块
# 最常见的配置是本虚拟主机的监听配置、名称、IP配置
server {
# 监听的端口号
listen 80;
# 域名可以有多个,用空格隔开
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# location 块
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;
#}
}
关于 nginx.conf 配置文件的详细描述,可参考下方连接