CentOS7使用yum安装nginx1.16及Nginx配置

  •   
  • 746
  • linux
  • 0
  • dodobook
  • 2019/07/01

随着技术的更新我们希望在新的服务器上面使用yum搭建最新版本的nginx1.16.0

#查看当前nginx在yum中的版本
yum info nginx


#更新一下yum源
yum update -y
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel


#安装epel
yum install -y epel-release


#编辑nginx.repo(也可能是新建文件)
vim /etc/yum.repos.d/nginx.repo

#内容如下=====
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
#内容结束=====


#再次查看nginx的信息
yum info nginx


#安装nginx
yum install -y nginx


#安装完之后查看一下nginx版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.16.0

#设置开机自动启动
systemctl enable nginx.service


#查看nginx的状态
systemctl status nginx.service


#启动、关闭、重启nginx
systemctl start nginx.service
systemctl stop nginx.service
systemctl restart nginx.service

nginx
安装Nginx比较容易,安装完成之后也很容易出现nginx的欢迎页面。但是Nginx的配置负载均衡等等模块功能强大,此处就不延伸了。下面贴出常见的简易版的配置项nginx.conf test.dodokook.net.conf

Ngtinx的配置文件:/etc/nginx/nginx.conf

user  nginx;
worker_processes  8;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
	worker_connections  1024;
}


http {
	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        on;
	#tcp_nopush     on;

	keepalive_timeout  65;

	#gzip  on;

#    include /etc/nginx/proxy.conf;
	include /etc/nginx/conf.d/*.conf;
}

Ngtinx的配置文件:/etc/nginx/conf.d/test.dodokook.net.conf


server {
	listen       80;
	server_name  test.dodobook.net;
	root /www/test.dodobook.net;

	charset utf-8;
	error_log  /var/log/nginx/test.dodobook.net.error.log;
	access_log  /var/log/nginx/test.dodobook.net.access.log  main;

	location / {
		index  index.php index.html index.htm;
        #以下几行为Yii2框架支持
	#	try_files $uri $uri/ /index.php$is_args$args;
	#	if (!-e $request_filename){  
	#		rewrite ^(/assets|/css).* last;
	#		rewrite ^/(.*) /index.php last;  
	#	}  
	}

	#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   /usr/share/nginx/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;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		include        fastcgi_params;
	}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	location ~ /.(ht|svn|git) {
		deny all;
	}
}

参考网址如下:

https://blog.csdn.net/qq_26245325/article/details/84633853

https://blog.csdn.net/WuLex/article/details/90139141

https://www.yiichina.com/doc/guide/2.0/start-installation