Centos7安装nginx
2019-04-02 本文已影响0人
纬一
前言:本篇文章介绍了nginx安装以及简单配置
以下,Enjoy:
一:安装
1.下载nginx安装包
官网传送门:http://nginx.org/en/download.html
nginx官网.png2.下载安装包放至
user/local/nginx
3.执行解压:
tar -zxvf nginx-1.15.10.tar.gz
4.进入解压后的目录:
cd nginx-1.15.10
执行
./configure --prefix=/usr/local/nginx
----------
此步骤可能会抛出如下几种错误:
1.nginx error: the HTTP rewrite module requires the PCRE library.
解决方法:安装pcre-devel解决问题:yum -y install pcre-devel
2.nginx error: the HTTP gzip module requires the zlib library
解决方法:安装pcre-devel解决问题:yum install -y zlib-devel
----------
解决错误后再次执行
./configure --prefix=/usr/local/nginx
6.执行编译
make install
7.启动nginx:
/usr/local/nginx/sbin/nginx
基本命令(nginx/sbin目录下):
启动:
./nginx
停止:
./nginx -s stop
修改配置后重新加载配置:
./nginx -s reload
8.查看nginx 是否启动(查看端口运行):netstat -ntlp
9.完成安装,浏览器输入:服务器地址(默认80端口),显示Welcome to nginx !
二:nginx.conf配置
1.打开nginx.conf文件
vi /usr/local/nginx/conf/nginx.conf
文件内容:
#user nobody;
#工作进程:数目。根据硬件调整,通常等于CPU数量或者2倍于CPU。
worker_processes auto;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# pid进程ID路径
pid logs/nginx.pid;
events {
#使用epoll的I/O 模型。linux建议epoll,FreeBSD建议采用kqueue,window下不指定。
use epoll;
#每个工作进程的最大连接数量。根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。每个进程允许的最多连接数,理论上每台nginx服务器的最大连接数为。worker_processes*worker_connections
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#上传文件大小限制,默认不配置为1m,http{} 中控制着所有nginx收到的请求。而报文大小限制设置在server{}中,则控制该server收到的请求报文大小,同理,如果配置在location中,则报文大小限制,只对匹配了location 路由规则的请求生效。
client_max_body_size 10m;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
# 监听端口
listen 80;
# 监听地址
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
# 拦截所有URL请求
location / {
# 转发地址
proxy_pass http://127.0.0.1:9000;
}
# 拦截具体URL
location /server/json/queryStaticResourceInfo {
# 转发
proxy_pass http://www.baidu.com;
}
#error_page 404 /404.html;
2.加载配置:
./nginx -s reload
浏览器输入:服务器地址(默认80端口),成功跳转http://服务器IP:9000.
三: 简化nginx启动方式
使用systemd配置nginx.service,将服务启动方式简化
1.进入目录
cd /usr/lib/systemd/system
下使用touch命令创建nginx.service文件
touch nginx.service
2.打开文件:
vi nginx.servcie
编辑内容
[Unit]
# 描述
Description=The nginx HTTP and reverse proxy server
# 描述服务类别
After=network.target remote-fs.target nss-lookup.target
[Service]
# forking:后台运行的形式
Type=forking
# pid路径,需跟nginx.conf中的pid的目录对应
PIDFile=/usr/local/nginx/logs/nginx.pid
# 启动前准备
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
# 启动命令
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# 重启命令
ExecReload=/usr/local/nginx/sbin/nginx -s reload
# 停止命令
ExecStop=/usr/local/nginx/sbin/nginx -s stop
# 优雅停止
ExecQuit=/usr/local/nginx/sbin/nginx -s quit
# 为服务分配临时空间
PrivateTmp=true
[Install]
# 服务用户的模式
WantedBy=multi-user.target
3.修改完成 执行刷新命令:
systemctl daemon-reload
4.启动nginx服务:
#启动
systemctl start nginx
#重启
#systemctl reload nginx
#停止
#systemctl stop nginx
5.完成
四:随记
待续.....2019-4-2 15:48:52