Install LANMP and load balancing under CentOS6.5

 
This article installs three servers, which are respectively equipped with Nginx, Apache, and Nginx as the front-end server to process the front-end resource files, and Apache as the back-end server, which is more stable and handles PHP. Then use Nginx's reverse proxy for load balancing.

 

Suppose the server:

192.168.1.111 (as the main server and load balancing control) -> Nginx, Apache installed

192.168.1.112 (as slave server 1) -> installed with Nginx, Apache
192.168.1.113 (as slave server 2) -> installed with Nginx, Apache

 

1. Before installation, turn off the firewall and clean up the installed packages:

chkconfig iptables off
rpm -e httpd
rpm -e mysql
rpm -e php
yum -y remove httpd
yum -y remove mysql
yum -y remove php #Search

apache package
rpm -qa http* #Force

uninstall apache package
rpm -e --nodeps Query the file name #Check

whether to uninstall cleanly
rpm -qa|grep http*

 

2. Configure CentOS 6.0 third-party yum source (there is no nginx package in the default standard source of CentOS)

wget http://www.atomicorp.com/installers/atomic
sh ./atomic
yum check-update

3. Install Apache and set the port to 8080

yum install httpd
vim /etc/httpd/conf/httpd.conf
Listen 80 #Remove the comment and change the port to 8080
ServerName www.example.com:80 #Remove the comment and change it to the corresponding port and domain name
service httpd start
chkconfig httpd on
#Install Apache extension library
yum -y install httpd-manual mod_ssl mod_perl mod_auth_mysql

4. Install Mysql

yum install mysql mysql-server mysql-devel
service mysqld start
chkconfig mysqld on #After
installation, the default user name is root and the password is empty, and the password needs to be reset for him.
mysql_secure_installation

 

5. Install php

yum install php php-devel php-mysql gd php-gd gd-devel php-common php-mbstring php-mcrypt php-ldap php-pear php-xml php-xmlrpc php-imap php-curl

 

6. Configure php

vim /etc/php.ini
cgi.fix_pathinfo = 1 #将注释去掉,开启PHP的pathinfo功能,伪静态要用到。
max_execution_time = 0  #脚本运行的最长时间,默认30秒
max_input_time = 300#脚本可以消耗的时间,默认60秒
memory_limit = 256M#脚本运行最大消耗的内存,根据你的需求更改数值,默认128M
post_max_size = 100M  #单提交的最大数据,此项不是限制上传单个文件的大小,而是针对整个表单的提交数据进行限制的。限制范围包括表单提交的所有内容.例如:发表贴子时,贴子标题,内容,附件等…默认8M,这个值一定大于upload_max_filesize
upload_max_filesize = 10M#上载文件的最大许可大小 ,默认2M

 

7.安装Nginx

yum install nginx
service nginx start
chkconfig nginx on

 

8.主服务器(192.168.1.111)配置Nginx支持负载均衡

#修改主服务器的主配置nginx.conf,在http{}里添加apache组的upstream,因为nginx占用了80端口,所以我们的服务器apache端口都用8080。
vim /etc/nginx/nginx.conf
#在http里增加upstream
#phpfuzai upstream主要负责交给apache来处理
upstream phpfuzai{
        server 192.168.1.111:8080; #主服务器也负责处理php端的运行,正规情况下,都是专用一台主服务器,主要做负载用,其他的都处理,不过我们这里为了测试,所以也把主服务器加上。
        server 192.168.1.112:8080; #从服务器1
        server 192.168.1.113:8080; #从服务器2
}
#htmlfuzai upstream主要负责交给nginx来处理,由于主服务器监听了80端口,所以避免无限循环,就不需要加入htmlfuzai组。
upstream htmlfuzai{
        server 192.168.1.112:80;#从服务器1
        server 192.168.1.113:80;#从服务器2
}

#修改虚拟主机配置default.conf,进行proxy_pass配置
vim /etc/nginx/conf.d/default.conf

location / {
            proxy_pass  http://htmlfuzai;
            #保留用户真实信息
            proxy_set_header Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ~ \.php$ {
            proxy_pass  http://phpfuzai;
            #保留用户真实信息
            proxy_set_header Host $host;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
}

 

9.从服务器1和2,配置html以及php的处理

从服务器安装有Nginx和Apache,并且都在运行,Nginx是80端口,负责处理静态资源,Apache是8080端口,负责处理php后台语言。
#修改Nginx配置,让其根目录与Apache的目录相同
vim /etc/nginx/conf.d/default.conf
#修改location / 为以下这样。
location / {
            root   /var/www/html;
            index  index.html index.htm;
}

 

 

10.安装phpmyadmin

#到https://www.phpmyadmin.net/downloads/下载对应版本,我下载的是支持php5.3以上的版本。

cd /usr/share/nginx/html/
wget https://files.phpmyadmin.net/phpMyAdmin/4.4.15.5/phpMyAdmin-4.4.15.5-all-languages.zip
unzip phpMyAdmin-4.4.15.5-all-languages.zip
mv phpMyAdmin-4.4.15.5-all-languages zhaonimei

 

11.重启Nginx Apache

service nginx restart
service httpd restart

 

12.测试Nginx是否解析php

html测试:在从服务器(1、2)上分别建立不同内容的haha.html
本地浏览器输入:192.168.1.111/haha.html,看看每次刷新的内容是不是变化的,如果变化了,则测试成功。

php测试:在主从服务器上分别建立不同的test.php
本地浏览器输入:192.168.1.111/test.php,看看每次刷新的内容是不是变化的,如果变化了,则测试成功。

本地浏览器输入:192.168.1.111/zhaonimei
显示phpmyadmin登录界面 环境搭建成功

 

 相关文章:

Nginx和Apache伪静态配置参考

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326691352&siteId=291194637