1、配置yum源
sudo rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
2、安装nginx
sudo yum install -y nginx
3、启动nginx
sudo systemctl start nginx.service
如果一切进展顺利的话,现在你可以通过你的域名或IP来访问你的Web页面来预览一下Nginx的默认页面;
4、添加虚拟主机
/etc/nginx/nginx.conf nginx配置文件
nginx.conf 里面包含了 include /etc/nginx/conf.d/*.conf,所以把每个域名的配置文件都放在 /etc/nginx/conf.d/*.conf里面。以下是一份完整的配置文件,适用于所有php的mvc框架(TP,yii2,laravel),注意修改你自己的server_name和root这两项
server{
listen 80;
server_name shanghai_erp.com;
root /data/www/shanghai_erp/public;
index index.php index.html;
access_log /var/log/nginx/shanghai_erp.log;
location / {
try_files $uri/ /index.php?$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $uri =404;
}
}
5、负载均衡
配置文件如下:(更多负载均衡的信息请大家自行百度)
#负载均衡
upstream tp_hosts {
server 14.215.177.38:80 weight=3; #反向代理到百度首页 weight是权重
server 117.121.101.41:80 weight=2;
}
server{
listen 80;
server_name tp.maomin.xyz;
root /data/shop;
index index.html index.php;
access_log /var/log/nginx/access_tp.log tp; /*日志文件*/
location /{
proxy_set_header Host www.maomin.online;
proxy_pass http://tp_hosts; #反向代理 tp_hosts代表上边的upstream_tp_hosts的ip地址,也可以直接写ip地址。
}
}