Nginx負載均衡+tomcat+apache+PHP+MySQL主從搭建php
192.168.11.15: Nginx+tomcat負載均衡 192.168.11.16:tomcat +apache+PHP+MySQL服務器1+mysql主 192.168.11.17:tomcat+apache+PHP+MySQL服務器2+mysql從
關閉防火牆:html
systemctl stop firewalld 192.168.11.15:nginx端 cd /usr/src/ 安裝PCRE庫支持 yum install pcre-devel pcre -y wget -c http://nginx.org/download/nginx-1.19.2.tar.gz tar -xzf nginx-1.19.2.tar.gz #進入解壓目錄,而後sed修改Nginx版本信息爲JWS cd nginx-1.19.2 ; sed -i -e 's/1.13.9//g' -e 's/nginx\//JWS/g' -e 's/"NGINX"/"JWS"/g' src/core/nginx.h #預編譯Nginx useradd www;./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module make && make install 配置java環境變量,vi /etc/profile 添加以下語句 export JAVA_HOME=/usr/java export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH rz –y 上傳 jdk包 tar –xzf jdk mv jdk /usr/java source /etc/profile //使環境變量馬上生效。 java -version //查看java版本,顯示版本爲1.6.0_18,證實安裝成功。 下載版本apache-tomcat-8.5.37.tar.gz tar -xzf apache-tomcat-8.5.37.tar.gz mv apache-tomcat-8.5.37 /usr/local/tomcat便可。 啓動tomcat,命令爲:/usr/local/tomcat/bin/startup.sh 查看ps -ef |grep tomcat 進程及端口是否存在 Tomcat默認測試頁面:
192.168.11.16端主 192.168.11.17端作如下一樣操做: 配置java環境變量,vi /etc/profile 添加以下語句 export JAVA_HOME=/usr/java export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH 上傳或下載 jdk1.8.0_131.tar.gz tar –xzf jdk1.8.0_131.tar.gz mv jdk1.8.0_131.tar.gz /usr/java source /etc/profile //使環境變量馬上生效。 java -version //查看java版本,顯示版本爲1.6.0_18,證實安裝成功 下載版本apache-tomcat-8.5.37.tar.gz wget -c http://mirrors.hust.edu.cn/apache/tomcat/tomcat-8/v8.5.49/bin/apache-tomcat-8.5.49.tar.gz tar -xzf apache-tomcat-8.5.37.tar.gz mv apache-tomcat-6.5.37 /usr/local/tomcat便可 啓動tomcat,命令爲:/usr/local/tomcat/bin/startup.sh 查看ps -ef |grep tomcat 進程及端口是否存在 測試Tomcat默認頁面
安裝apache web服務java
安裝apache模塊 yum install apr apr-devel apr-util-devel gcc ncurses-devel cmake libaio bison gcc-c++ git pcre* -y wget http://archive.apache.org/dist/httpd/httpd-2.4.29.tar.gz tar xzf httpd-2.4.24.tar.gz cd httpd-2.4.29 ./configure --prefix=/usr/local/apache/ --enable-rewrite --enable-so make && make install 編譯 安裝 /usr/local/apache/bin/apachectl restart 啓動Apache
建立虛擬主機配置文件httpd-vhosts.conf,該文件默認已存在,只需去掉httpd.conf配置文件中#號便可mysql
該配置文件在/usr/local/apache/conf
配置文件/usr/local/apache/conf/extra/httpd-vhosts.conf中代碼設置爲以下: <VirtualHost *:80> DocumentRoot "/usr/local/apache/docs/" ServerName www.jf1.com ServerAlias www.dummy-host.example.com ErrorLog "logs/dummy-host.example.com-error_log" CustomLog "logs/dummy-host.example.com-access_log" common </VirtualHost>
配置完成後從新啓動apache服務器: /usr/local/apache/bin/apachectlrestart mkdir -p /usr/local/apache/htdocs /{jf1,jf2}/ /usr/local/apache/bin/apachectl restart Windows客戶端設置Hosts映射
安裝MySQL數據庫nginx
yum install cmake ncurses-devel ncurses –y cd/usr/src/ wget http://down1.chinaunix.net/distfiles/mysql-5.5.20.tar.gz cd /mysql-5.5.20 cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql/ \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DMYSQL_DATADIR=/data/mysql \ -DSYSCONFDIR=/etc \ -DMYSQL_USER=mysql \ -DMYSQL_TCP_PORT=3306 \ -DWITH_XTRADB_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_PARTITION_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DWITH_EXTRA_CHARSETS=1 \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DEXTRA_CHARSETS=all \ -DWITH_BIG_TABLES=1 \ -DWITH_DEBUG=0 make make install cd /usr/local/mysql/ \cp -f support-files/my-large.cnf /etc/my.cnf \cp -f support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld chkconfig --level 35 mysqld on mkdir -p /data/mysql useradd mysql /usr/local/mysql55/scripts/mysql_install_db --user=mysql --datadir=/data/mysql --basedir=/usr/local/mysql55/ chown -R mysql /data/mysql/ ln -s /usr/local/mysql55/bin/* /usr/bin/ service mysqld restart
PHP服務安裝,PHP需與Apache、MySQL進行整合c++
yum -y install php-mcrypt libmcrypt libmcrypt-devel autoconf freetype gd jpegsrc libmcrypt libpng libpng-devel libjpeg libxml2 libxml2-devel zlib curl curl-devel -y cd /usr/src wget http://mirrors.sohu.com/php/php-5.6.8.tar.gz tar xzf php-5.3.28.tar.gz cd php-5.3.28 ; ./configure --prefix=/usr/local/php5 --with-config-file-path=/usr/local/php5/etc --with-apxs2=/usr/local/apache2/bin/apxs --with-mysql=/usr/local/mysql/ make && make install 源碼整合 爲了能讓Apache發佈PHP頁面 須要將PHP安裝完成後的libphp7.so模塊與Apach e進行整合,vim httpd.conf編輯配置文件,加入以下代碼: LoadModule php5_module modules/libphp5.so AddType application/x-httpd-php .php DirectoryIndex index.php index.html index.htm
測試Apache+PHP環境git
建立PHP測試頁面,在/usr/local/apache/htdocs目錄下建立index.php測試頁面, 執行以下命令自動建立: Vim /usr/local/apache/htdocs/index.php <?php phpinfo(); ?> /usr/local/apache/bin/apachectl restart 從新啓動Apache服務,瀏覽器輸入 Apache WEB的IP訪問,如圖12-4所示,即代 表LAMP源碼環境整合成功。
MySQL主從架構配置:web
一、MYSQL主從複製是一個異步複製的過程,整個過程須要開啓3個線程,Master 開啓I/O線程,SLAVE開啓I/O線程和SQL線程; 二、在Slave庫執行Start slave啓動主從複製,Slave以I/O線程鏈接到Master 而且 會請求從Master服務器指定bin-log文件名稱,Position點以後的內容; 三、BIN-log文件稱爲二進制日誌文件,只在Master開啓,功能是記錄在Master庫 作的增、刪、改SQL語句,執行的各類SQL修改語句被記錄到BIN-LOG日誌文 件中; 三、當Master服務器接收到Slave服務器請求的日誌內容以後,以I/O線程響應, 在Master庫中開啓dump log線程,讀取bin-log日誌的position點以後的數據,發 送給Slave數據庫; 四、Slave數據庫接收到bin-log日誌內容以後,將數據內容追加到本地Relay-log中 繼日誌的末尾,同時將master庫的IP、用戶名、密碼、bin-log文件名稱、Position 點,會寫入master.info文件(告訴本身下次同步去哪裏同步,而且從哪裏開始); Slave數據庫實時開啓SQL線程,SQL線程實時監控Relay-log日誌內容,發現日誌 內容有更新,讀取日誌而且解析出日誌中的SQL語句,在本地數據庫中去執行,最 後的效果是Slave和Master數據保持一致 192.168.11.16Master端配置文件中,開啓bin-log日誌功能,指令以下: vim /etc/my.cnf在[mysqld]段加入代碼: server-id = 1 log-bin = mysql-bin 配置完成後重啓mariadb服務 在192.168.11.16 Master數據庫服務器命令行中 建立tongbu用戶及密碼並設置權限,執行以下命令,查看bin-log文件及position點, 1)授予從庫鏈接主庫權限 grant replication slave on *.* to 'tongbu'@'192.168.11.15' identified by '123456'; grant all on *.* to root@'%' identified by "123456"; flush privileges; 刷新權限
執行show master status;命令,查看bin-log文件及position點sql
192.168.11.17 MySQL從庫操做以下:數據庫
Slave端使用源碼安裝MySQL-5-5版本軟件後,在/etc/my.cnf 配置文件中開啓server-id = 2 ,而後重啓MYSQL服務。
若是在安裝時cp my-large.cnf /etc/my.cnf,則需修改server-id,MASTER與Slave端server-id不能同樣,Slave端也無需開啓bin-log功能:
Slave指定Master IP、用戶名、密碼、bin-log文件名(mysql-bin.000028)及position點,
stop slave; 中止
change master to master_host='192.168.50.130',master_user='tongbu',master_password='123456',ma
ster_log_file='mysql-bin.000028',master_log_pos=257; grant all on *.* to root@'%' identified by "123456"; 受權 flush privileges; 刷新權限 slave start; 啓動主從同步
在slave啓動slave start,並執行show slave status\G查看Mysql主從狀態:
在主庫端建立jf1數據庫,查看從庫端是否同步
create database jfedu charset=utf8; show databases;
同步成功
192.168.11.16端JPress安裝
LAMP源碼整合完畢以後,開源PHP軟件包,將軟件包解壓併發布在tomcat ROOT發佈目錄 cd /usr/local/tomcat/webapps/ROOT unzip starter-tomcat-1.0.war
在瀏覽器中 輸入192.168.11.16:8080
主數據庫命令行中建立PHP鏈接MYSQL的用戶及密碼,受權192.168.11.11 LAP端; create database jperss charset=utf8; grant all on jpress.* to root@'192.168.11.16' identified by "123456";
Wordpress 配置 cd /usr/ local/apache2/htdocs 上傳 wordpress Tar –xzf wordpress -4.9.4-zh_CN.tar.gz mv wordpress/* . 解壓到當前目錄 瀏覽器訪問192.168.11.16/index.php
出現如下報錯
vim /usr/local/apache2/htdocs/ wp-config.php 複製代碼寫入
192.168.11.17端 拷貝192.168.11.16端apache/htdocs/目錄到本地 cd /usr/local/apache2/htdocs/ scp -r 192.168.11.16:/usr/local/apache2/htdocs/* . 瀏覽器訪問192.168.11.17/index.php
193.192.168.11.15端 Nginx配置
建立多個虛擬主機 vim /usr/local/nginx/conf 註銷server 建立mkdir vhost
cd vhost vim jpress.com.conf upstream jpress_com { server 192.168.11.16:8080 weight=1 max_fails=2 fail_timeout=15s; server 192.168.11.17:8080 weight=1 max_fails=2 fail_timeout=15s; } server { listen 80; server_name jpress.com; access_log logs/jpress.access.log; location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://jpress_com; } }
vim wordpress.com.conf upstream wordpress_com { server 192.168.11.16:80 weight=1 max_fails=2 fail_timeout=15s; server 192.168.11.17:80 weight=1 max_fails=2 fail_timeout=15s; } aserver { listen 80; server_name wordpress.com; access_log logs/wordpress.access.log; location / { proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://wordpress_com; } }
在本地C:\Windows\System32\drivers\etc\hosts 中綁定
193.192.168.11.15 wordpress.com jpress.com