CentOS下nginx简单安装

时间:2023-02-03 21:07:01

说明:
环境

系统:Centos 6
软件包:nginx-1.2.4

配置系统yum源

#/etc/yum.repos.d/
#rm -rf ./*
vi localhost.repos.d
[yumyuan]
name=CentOS-$releasever - Media #自定义名称
baseurl=file:///mnt/cdrom/ #本地光盘挂载路径
gpgcheck=0 #检查GPG-KEY,0为不检查,1为检查
enabled=1 #启用yum源,0为不启用,1为启用

2: 安装步骤
因为nginx模块需要第三方库的支持,需要安装下列库.
安装gcc编译器及相关工具

#yum -y install gcc gcc-c++ autoconf automake make

安装相关依赖的模块

yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

创建nginx系统账户:
由于nginx是系统服务,运行此服务需要系统账户。

# groupadd nginx
# useradd -r -g nginx -s /sbin/nologin -M nginx
软件源码包存放位置:/opt/
#cd /opt/
# tar -zxvf nginx-1.2.4.tar
#cd nginx-1.2.4
#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module //通过编译源码的方式进行安装
#make
#make install
启动Nginx
#启动nginx
#/usr/local/nginx/sbin/nginx -t
显示以下信息为正确的 the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx.conf test is successful #/usr/local/nginx/sbin/nginx
启动成功后,查看nginx进程信息: # ps -ef | grep nginx ,看是否存在nginx的进程来确认是否成功启动。 1.在配置nginx 时提示如下错误时:
#/usr/local/nginx/sbin/nginx -t
nginx: [emerg] getpwnam(“nginx”) failed

解决方案

在nginx.conf中 把user nobody的注释去掉既可

2.重启nginx后丢失nginx.pid问题解决

/usr/local/nigix/sbin/nginx -c /usr/local/nigix/conf/nginx.conf

重启动nginx

#/usr/local/nginx/sbin/nginx -s reload

测试http://ip地址/
同时记得检查centos防火墙设置,是否开启了相应端口,可使用setup命令来设置防火墙、dns、网络等信息。如果默认的配置文件未做任何改动,使用浏览器直接访问nginx
server,会出现提示:Welcome to Nginx  

nginx.conf文件详解:

########nginx全局配置,将影响其他所有设置###################################
#user nobody; # worker进程运行用户以及用户组
worker_processes 2; #启动进程,通常设置成和cpu的数量相等
error_log /var/log/nginx/error.log; ##全局错误日志及PID文件
pid /var/run/nginx.pid; #指定进程id的存储文件位置 #工作模式及连接数上限
events {
use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能
worker_connections 1024; #单个后台worker process进程的最大并发链接数
#multi_accept on;
}
#对http服务器进行配置
http {
#设定mime类型,类型由mime.type文件定义
include /etc/nginx/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 /var/log/nginx/access.log main #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用,
#必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime.
sendfile on;
#tcp_nopush on; #连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on; #开启gzip压缩
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
#主机设置
server {
#侦听80端口
listen 80;
#定义使用www.xx.com访问
server_name www.xx.com;
#设定本虚拟主机的访问日志
access_log logs/www.xx.com.access.log main;
#默认请求(url匹配设置)
location / {
root /usr/local/nginx/html; #定义服务器的默认网站根目录位置
index index.php index.html index.htm; #定义首页索引文件的名称 } # 定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/html;
}
} #server
}