header-bg.jpg
Linux虚拟主机中使用Nginx服务器
发表于 2017-02-17 21:11
|
分类于 Linux
|
评论次数 0
|
阅读次数 1437

attachment/2017/03/10/51991489118719.jpg

关闭防火墙 service iptables stop

关闭selinux vi /etc/selinux/config->SELINUX=disabled

关闭selinux防火墙 setenforce 0

重启apache service httpd restart
******安装Lnmp环境******
1.安装wget软件
yum -y install wget
2.配置CentOS第三方yum源(CentOS默认的标准源里没有nginx软件包)
wget http://www.atomicorp.com/installers/atomic
sh atomic
3.安装nginx
yum install -y nginx
service nginx start //启动
chkconfig --level 235 nginx on //启动级别
4.安装mysql
yum install -y mysql mysql-server mysql-devel
service mysqld start
chkconfig --level 235 mysqld on
5.安装php和所需组件使php支持Mysql、FastCGI模式
yum install -y php lighttpd-fastcgi php-common php-devel php-fpm php-mysql
service php-fpm start
chkconfig --level 235 php-fpm on
6.配置nginx支持php
//将原配置文件改为备份文件
mv /etc/nginx/nginx.conf /etc/nginx/nginx.confbak
//使用默认的配合文件作为原配置文件
cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
7.修改nginx配置文件,添加fastcgi支持Php
vi /etc/nginx/nginx.conf
修改内容如下:
location / {
root   /www;
index  index.php index.html index.htm;
}
location ~ \.php$ {
root           /www;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  /www$fastcgi_script_name;
include        fastcgi_params;
}
8.进行重启,关闭防火墙
service nginx restart
service php-fpm restart
service iptables stop
9.创建/www目录并且创建文件index.php并且echo '后盾网';
10.配置nginx支持php
vi /etc/php.ini
在文件末尾添加cgi.fix_pathinfo = 1
11.进行重启
service nginx restart
service php-fpm restart
12.在浏览器输入:192.168.31.31/index.php
******显示php界面 安装Lnmp环境完成  备份虚拟机******
13.注意:Nginx扩展配置文件不会自动加载需要在主配置文件中加载(了解不操作)
http {
# Load config files from the /etc/nginx/conf.d directory include /etc/nginx/conf.d/*.conf;
}
14.Nginx默认不开启目录,开启目录列表方式:
vi /etc/nginx/nginx.conf
插入
http {
autoindex on; //自动显示目录
autoindex_exact_size off; //人性化方式显示文件大小否则以byte显示
autoindex_localtime on; //按服务器时间显示,否则以gmt时间显示
}
15.进行重启
service nginx restart
service php-fpm restart
16.删除/www/index.php查看浏览器中是否有目录存在
17.再次创建/www/index.php文件 里面写错Php代码浏览器显示网页无法正常运行
18.开启php错误
修改php配置文件
vi /etc/php.ini
display_errors = On
修改php-fpm.conf配置文件
vi /etc/php-fpm.d/www.conf
php_flag[display_errors] = on
进行重启
service nginx restart
service php-fpm restart
19.操作虚拟主机
a.删除/www里面的所有文件
rm -rf *
b.创建qq和hd目录b并且创建文件
mkdir qq
mkdir hd
c.主配置文件中修改配置文件
vi /etc/nginx/nginx.conf
http下新增如下代码:
server {
listen 80;
server_name www.qq.com; index index.html;
root /www/qq;
} server {
listen 80;
server_name www.hd.com; index index.html;
root /www/hd;
}
d.进行重启
service nginx restart
service php-fpm restart
f.更改hosts文件
windows中:C:\Windows\System32\drivers\etc\hosts
mac中:/etc/hosts
g.在网页中登录www.qq.com/www.hd.com进行测试

******负载均衡******
//1.准备好三台服务器
nginx master: 配好域名 www.hd.com
nginx1: 192.168.31.100
nginx2: 192.168.31.200
//2.配置主服务器nginx.conf(如有必要注释虚拟主机的配置)
vi /etc/nginx/nginx.conf
//内容如下
upstream hd.com {
server 192.168.31.100:80 weight=2;
server 192.168.31.200:80 weight=3;
}
server{
listen 80;
server_name hd.com;
location /{
proxy_pass http://hd.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
进行重启
service nginx restart
3.操作两个负载服务器nginx.conf(注释虚拟主机的配置)
vi /etc/nginx/nginx.conf
内容如下
server{
listen 80;
server_name hd.com;
index index.html;
root /www;

}
进行重启
service nginx restart
4.分别给两个负载服务器建立/www/index.html进行测试

发布评论
还没有评论,快来抢沙发吧!