linux nginx+apache | 动静分离 | 负载均衡 | 超详细

服务器一:LAMP架构部署一键脚本

echo "-------编译安装Apache httpd服务--------------"
echo "关闭防火墙"
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
yum remove httpd

echo "安装环境依赖包"
yum -y install gcc gcc-c++ make pcre pcre-devel expat-devel perl            

echo "配置软件模块"
cd /opt/
tar zxvf apr-1.6.2.tar.gz
tar zxvf apr-util-1.6.0.tar.gz
tar jxvf httpd-2.4.29.tar.bz2

mv apr-1.6.2 /opt/httpd-2.4.29/srclib/apr
mv apr-util-1.6.0 /opt/httpd-2.4.29/srclib/apr-util

echo "指定安装路径"
cd /opt/httpd-2.4.29/
./configure --prefix=/usr/local/httpd --enable-so --enable-rewrite --enable-charset-lite --enable-cgi --enable-deflate --enable-expires

echo "编译及安装"
make && make install				


echo "优化配置文件路径,并把httpd服务的可执行程序文件放入路径环境变量的目录中便于系统识别"
ln -s /usr/local/httpd/conf/httpd.conf /etc/
ln -s /usr/local/httpd/bin/* /usr/local/bin/

echo "添加httpd系统服务"

echo '
[Unit]
Description=The Apache HTTP Server						
After=network.target									
[Service]
Type=forking											
PIDFile=/usr/local/httpd/logs/httpd.pid					
ExecStart=/usr/local/bin/apachectl $OPTIONS			
ExecReload=/bin/kill -HUP $MAINPID						
[Install]
WantedBy=multi-user.target ' > /lib/systemd/system/httpd.service

systemctl start httpd.service
systemctl enable httpd.service


echo "修改httpd 服务配置文件"

IP=`ifconfig | grep "inet" | awk 'NR==1{print $2}'`
cp /etc/httpd.conf{,.bak}
sed -i "52c Listen ${IP}:80" /etc/httpd.conf.bak
sed -i '52 s/^/#/' /etc/httpd.conf.bak

sed -i "197d" /etc/httpd.conf.bak
sed -i "196a ServerName www.kgc.com:80" /etc/httpd.conf.bak
cat /etc/httpd.conf.bak > /etc/httpd.conf
httpd -t  
cat /usr/local/httpd/htdocs/index.html 
systemctl restart httpd.service
echo "${IP} www.kgc.com" >> /etc/hosts
netstat -anpt | grep 80

echo "apache安装结束"

echo "-------------编译安装mysqld 服务---------------"
echo "安装环境依赖包"
yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake

echo "配置软件模块"
cd /opt
tar zxvf mysql-5.7.17.tar.gz
tar zxvf boost_1_59_0.tar.gz
echo "重命名"
cd /opt
mv boost_1_59_0 /usr/local/boost		
echo "正在编译"
cd /opt/mysql-5.7.17/
cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8  \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_EXTRA_CHARSETS=all \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=/usr/local/boost \
-DWITH_SYSTEMD=1
echo "正在安装"
make && make install

echo "创建mysql用户"
useradd -M -s /sbin/nologin  mysql

echo "修改mysql 配置文件"

echo "[client]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock

[mysql]
port = 3306
default-character-set=utf8
socket=/usr/local/mysql/mysql.sock
auto-rehash

[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES " > /etc/my.cnf

echo "更改mysql安装目录和配置文件的属主属组"
chown -R mysql:mysql /usr/local/mysql/
chown mysql:mysql /etc/my.cnf

echo "设置路径环境变量"
echo "export PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH" >> /etc/profile	
source /etc/profile

echo "初始化数据库"
cd /usr/local/mysql/bin/
./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data

echo "添加mysqld系统服务"
cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/	
source /etc/profile
systemctl daemon-reload           
systemctl start mysqld.service 
systemctl enable mysqld         
netstat -anpt | grep 3306      

yum -y install expect

ehco "修改mysql 的登录密码"

mimaput() {
passwd=$1
/usr/bin/expect <<-EOF
spawn mysqladmin -u root -p password $passwd 
expect "Enter password" {send "\r"}
expect eof
EOF
}
read -p "mysql 的登录密码:" mima
mimaput "$mima"



echo "授权远程登录"

funtion() {
a=$1
/usr/bin/expect <<-EOF  
spawn mysql -u root -p
expect "Enter password" {send "${a}\r"}
expect "mysql>" {send "grant all privileges on *.* to 'root'@'%' identified by ${a};\r"}
expect "mysql>" {send "show databases;\r"}
expect "mysql>" {send "quit\r"}

expect eof
EOF

}
funtion "$mima"

		

echo "--------编译安装PHP 解析环境--------"



echo "安装GD库和GD库关联程序,用来处理和生成图片"
yum -y install \
gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel

echo "配置软件模块"
cd /opt
tar jxvf php-7.1.10.tar.bz2
cd /opt/php-7.1.10/
./configure \
--prefix=/usr/local/php7 \
--with-apxs2=/usr/local/httpd/bin/apxs \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-config-file-path=/usr/local/php7 \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip

echo "编译及安装"
make && make install

echo "复制模版文件作为PHP 的主配置文件,并进行修改"
cp /opt/php-7.1.10/php.ini-development /usr/local/php7/php.ini	

sed -i '1170d' /usr/local/php7/php.ini
sed -i '1169a mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php7/php.ini
sed -i '939d' /usr/local/php7/php.ini
sed -i '938a date.timezone = Asia/Shanghai' /usr/local/php7/php.ini

echo "优化把PHP 的可执行程序文件放入路径环境变量的目录中便于系统识别"
ln -s /usr/local/php7/bin/* /usr/local/bin/
php -m 			

echo "修改httpd 服务的配置文件,让apache支持PHP"
cp /etc/httpd.conf /etc/httpd.conf.bak
sed -i "256c DirectoryIndex index.html index.php" /etc/httpd.conf.bak
sed -i "394c AddType application/x-httpd-php .php" /etc/httpd.conf.bak
sed -i "395i AddType application/x-httpd-php-source .phps" /etc/httpd.conf.bak
cat /etc/httpd.conf.bak > /etc/httpd.conf



echo "验证PHP 测试页"
cd /usr/local/httpd/htdocs
mv index.html index.html.bak

echo "<?php
phpinfo();
?> "   > /usr/local/httpd/htdocs/index.php

systemctl restart httpd.service

服务器二:Nginx一键部署:

echo "*************安装 Nginx 服务******************"
systemctl stop firewalld.service;
systemctl disable firewalld.service;
setenforce 0;

yum remove -y httpd;

yum -y install pcre-devel zlib-devel gcc gcc-c++ make;


useradd -M -s /sbin/nologin nginx;


cd /opt;
tar zxvf nginx-1.12.0.tar.gz;

cd nginx-1.12.0/;
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module

make; 
make install;

ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/;


echo '[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecrReload=/bin/kill -s HUP $MAINPID
ExecrStop=/bin/ki11 -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target' > /lib/systemd/system/nginx.service;

chmod 754 /lib/systemd/system/nginx.service;
systemctl start nginx.service;
systemctl enable nginx.service;

nginx服务器配置:

httpd{}配置:
   upstream apache_server {
        server 192.168.78.44:80 weight=1;
    }

server{}配置

server {
        listen       80;
        server_name  www.accp.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location ~ \.php$ {
                proxy_pass http://apache_server;
                proxy_set_header HOST $host;

        }

在这里插入图片描述
修改:/usr/local/nginx/html/index.html(默认路径,我未改动)
在这里插入图片描述

访问www.accp.com
在这里插入图片描述

LAMP设置

修改 /usr/local/httpd/conf/httpd.conf
在这里插入图片描述
其他不需要修改,如果觉得/usr/local/httpd/htdocs/index.php,网页内容不咋地,可以修改或者重新创建一个php网页,我都是保持默认路径,想修改的话,可以给修改,但是要保持nginx和apache的跳转路径、端口保持一致。

在这里插入图片描述
访问192.168.78.44/index.php(lamp服务器地址为192.168.78.44)

在这里插入图片描述

动静分离、负载均衡效果(在nginx服务器测试)

在这里插入图片描述
在这里插入图片描述
我只用了2台服务器,理论上还需要一台,也是LAMP架构,俩台配置一样,不过nginx服务器在 负载均衡设置的时候多写一个ip地址的问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值