摘要信息 | IP地址 | 做用 | 系统版本 |
---|---|---|---|
Nginx | 192.168.2.128 | 负载均衡,反向代理 | CentOS7.2 |
Apache+PHP5.6.36 | 192.168.2.129 | 解析wordpress,phpmyadmin动态页面 | CentOS7.2 |
Nginx+PHP5.6.36 | 192.168.2.130 | 解析wordpress,phpmyadmin动态页面 | CentOS7.2 |
Nginx+MySQL | 192.168.2.133 | 解析静态页面,提供MySQL数据库 | CentOS7.2 |
本次博客涉及全部程序包提供:连接:https://pan.baidu.com/s/1-xBK6Ti5IL_KxyQNjzKd1A
提取码:1ano php
1. 禁用iptables,selinux,配置epel源 2. 安装开发工具包 Centos7: yum groupinstall "Development Tools" Centos6: yum groupinstall "Development Tools" "Server Platform Development"
安装pcre正则支持及openssl支持: [root@nginx ~]# yum -y install pcre-devel openssl-devel [root@nginx ~]# useradd -r nginx 编译参数: [root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-threads --with-file-aio [root@nginx nginx-1.14.2]# make && make install 配置PATH: [root@nginx nginx-1.14.2]# echo "export PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile.d/nginx.sh [root@nginx nginx-1.14.2]# . /etc/profile.d/nginx.sh 配置nginx目录: [root@nginx nginx-1.14.2]# mkdir -p /data/nginx
(1) apr-1.50 [root@ap apr-1.5.0]# ./configure --prefix=/usr/local/apr [root@ap apr-1.5.0]# make -j 4&& make install (2) apr-util-1.5.3 [root@ap apr-util-1.5.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr [root@ap apr-util-1.5.3]# make -j 4&& make install (3) httpd-2.4.38 [root@ap httpd-2.4.38]# yum install pcre-devel openssl-devel worker,event模块: [root@ap httpd-2.4.38]# ./configure --prefix=/usr/local/apache24 --sysconfdir=/etc/httpd24 --enable-so --enable-ssl --enable-cgi --enable-rewrite --with-zlib --with-pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util --enable-modules=most --enable-mpms-shared=all --with-mpm=event 注意:--with-mpm=xxx ##apahce有三种模型prefork,worker,event根据须要填写,可是须要注意的是若是是worker/event那么编译php时则须要添加 --enable-maintainer-zts 参数 [root@ap httpd-2.4.38]# make -j 4 && make install [root@ap httpd-2.4.38]# echo "export PATH=/usr/local/apache24/bin:$PATH" >> /etc/profile.d/apache24.sh [root@ap httpd-2.4.38]# . /etc/profile.d/apache24.sh 添加php支持,准备测试页: [root@ap httpd-2.4.38]# vim /etc/httpd/httpd.conf ... AddType application/x-httpd-php .php ##添加对.php文件的支持 AddType application/x-httpd-php-source .phps <IfModule dir_module> DirectoryIndex index.html index.php ##配置默认识别主文件 </IfModule> Options Indexes FollowSymLinks ##注释掉这一行,禁止显示apache目录表 ... 配置php测试页面,配置远端数据库的链接信息 [root@ap httpd-2.4.38]# vim /usr/local/apache24/htdocs/index.php <?php // 当数据库链接正常页面输出OK,不然Failure $conn = mysql_connect('192.168.2.133','wordpress','123'); if ($conn) echo "OK"; else echo "Failure"; phpinfo(); ?> ### 启动脚本:CentOS6复制epel安装脚本修改便可,7先用apachectl来管理 [root@ap httpd-2.4.38]# apachectl start 编译安装的模块不多,在配置文件httpd.conf自行启用便可
###解决依赖关系 [root@ap php-5.6.36]# yum install bzip2-devel libmcrypt-devel libxml2-devel -y ###编译安装PHP(DSO模式) -- 安装M->P模式(php和MySQL不在一台主机时) [root@ap php-5.6.36]# ./configure --prefix=/usr/local/php --with-mysql --with-openssl --with-mysqli=mysqlnd --enable-mbstring --with-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache24/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts 注意: --enable-maintainer-zts 若是httpd编译的是prefork不须要该项,若为worker,event则须要 --with-apxs2 apxs是一个为Apache服务器编译和安装扩展模块的工具,DSO模式须要指定,FPM模式不须要 [root@ap php-5.6.36]# make -j4 && make install [root@ap php-5.6.36]# echo "export PATH=/usr/local/php/bin:$PATH" > /etc/profile.d/php.sh [root@ap php-5.6.36]# . /etc/profile.d/php.sh [root@ap php-5.6.36]# httpd -M ##查看是否有libphp5模块,有则php编译完成 配置文件准备: [root@ap php-5.6.36]# cp php.ini-production /etc/php.ini [root@ap php-5.6.36]# mkdir -p /etc/php.d/ -- 安装M+P模式(php和MySQL在一台主机时) # ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-apxs2=/usr/local/apache24/bin/apxs --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts -- 关于PHP如何多版本共存:DSO+FPM两种形式存在,将编译目录稍加区分便可 # ./configure --prefix=/usr/local/php5 --with-mysql=/usr/local/mysql --with-openssl --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php5 --with-bz2 --enable-fpm
### xcache准备: [root@ap xcache-3.2.0]# phpize --clean && phpize ##执行此步,生成configure文件 [root@ap xcache-3.2.0]# ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config [root@ap xcache-3.2.0]# make -j4 && make install 注意:编译最后会获得so的地址:/usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so ### 将xcache的库添加到php扩展: [root@ap xcache-3.2.0]# mkdir /etc/php.d -p [root@ap xcache-3.2.0]# cp xcache.ini /etc/php.d/ ### 修改xcache的.so的详细路径 [root@ap xcache-3.2.0]# vim /etc/php.d/xcache.ini ... extension = /usr/local/php/lib/php/extensions/no-debug-zts-20131226/xcache.so xcache.admin.enable_auth = Off xcache.size = 60M ... ### 编译完成重启apache或重载 [root@ap xcache-3.2.0]# pkill httpd [root@ap xcache-3.2.0]# apachectl start
### 安装pcre正则支持及openssl支持: [root@np nginx-1.14.2]# yum install -y pcre-devel openssl-devel [root@np nginx-1.14.2]# useradd -r nginx ### 编译参数: [root@np nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-threads --with-file-aio [root@np nginx-1.14.2]# make && make install ### 配置PATH: [root@np nginx-1.14.2]# echo "export PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile.d/nginx.sh [root@np nginx-1.14.2]# . /etc/profile.d/nginx.sh ### 配置nginx目录: [root@np nginx-1.14.2]# mkdir -p /data/nginx
### 解决依赖关系: [root@np php-5.6.36]# yum install bzip2-devel libmcrypt-devel libxml2-devel -y ### 编译参数: [root@np php-5.6.36]# ./configure --prefix=/usr/local/php --with-mysql --with-openssl --with-mysqli=mysqlnd --enable-mbstring --with-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php5 --with-bz2 --enable-fpm [root@np php-5.6.36]# make -j4 && make install ### 配置PATH: [root@np php-5.6.36]# echo "export PATH=/usr/local/php/bin:$PATH" > /etc/profile.d/php.sh [root@np php-5.6.36]# . /etc/profile.d/php.sh [root@np php-5.6.36]# httpd -M ##查看是否有libphp5模块,有则php编译完成 ### 配置文件准备: [root@np php-5.6.36]# cp php.ini-production /etc/php.ini [root@np php-5.6.36]# mkdir -p /etc/php.d/ [root@np php-5.6.36]# cp sapi/fpm/php-fpm /usr/local/bin [root@np php-5.6.36]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf [root@np php-5.6.36]# mkdir -p /var/lib/php/session;chown -R nginx.nginx /var/lib/php/session [root@np php-5.6.36]# vim /usr/local/php/etc/php-fpm.conf ... [www] listen = 127.0.0.1:9000 ##不建议对外开放,能够开放局域网,这里仅侦听127.0.0.1 listen.allowed_clients = 127.0.0.1 user = nginx ##以nginx用户启动,不然nginx无权访问fpm group = nginx pm = dynamic ##动态管理 slowlog = /var/log/php-fpm/www-slow.log php_admin_value[error_log] = /var/log/php-fpm/www-error.log php_admin_flag[log_errors] = on php_value[session.save_handler] = files php_value[session.save_path] = /var/lib/php/session ##建立目录并赋予nginx权限 ... ### 配置php测试页面,配置远端数据库的链接信息 [root@ap httpd-2.4.38]# vim /data/nginx/index.php <?php // 当数据库链接正常页面输出OK,不然Failure $conn = mysql_connect('192.168.2.133','wordpress','123'); if ($conn) echo "OK"; else echo "Failure"; phpinfo(); ?>
### 安装pcre正则支持及openssl支持: [root@mysql nginx-1.14.2]# yum -y install pcre-devel openssl-devel [root@mysql nginx-1.14.2]# useradd -r nginx ### 编译参数: [root@mysql nginx-1.14.2]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-threads --with-file-aio [root@mysql nginx-1.14.2]# make -j4 && make install ### 配置PATH: [root@mysql nginx-1.14.2]# echo "export PATH=/usr/local/nginx/sbin:$PATH" >> /etc/profile.d/nginx.sh [root@mysql nginx-1.14.2]# . /etc/profile.d/nginx.sh ### 配置nginx目录: [root@mysql nginx-1.14.2]# mkdir -p /data/nginx
[root@mysql pkg]# tar xf mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ [root@mysql local]# ln -s mysql-5.6.36-linux-glibc2.5-x86_64 mysql [root@mysql local]# mkdir -pv /data/mysql [root@mysql local]# useradd -r mysql [root@mysql local]# chown -R mysql.mysql /usr/local/mysql-5.6.36-linux-glibc2.5-x86_64 [root@mysql local]# chown -R mysql.mysql /data/mysql [root@mysql mysql]# ./scripts/mysql_install_db --datadir=/data/mysql/ --user=mysql [root@mysql mysql]# cp support-files/my-default.cnf /etc/my.cnf ##my.cnf主要修改datadir,socket位置等常规选项 [root@mysql mysql]# cp support-files/mysql.server /etc/init.d/mysqld ##改pid位置 [root@mysql mysql]# echo "export PATH=/usr/local/mysql/bin:$PATH" > /etc/profile.d/mysql.sh [root@mysql mysql]# . /etc/profile.d/mysql.sh [root@mysql mysql]# chkconfig --add mysqld [root@mysql mysql]# service mysqld restart [root@mysql mysql]# ln -sv /usr/local/mysql/mysql.sock /tmp/mysql.sock [root@mysql mysql]# mysql_secure_installation ##初始化安全设置并配置root密码 [root@mysql mysql]# mysql -uroot -proot ##配置wordpress库及用户、密码 mysql> CREATE DATABASE wordpress; mysql> GRANT ALL ON wordpress.* TO 'wordpress'@'192.168.2.%' IDENTIFIED BY '123';
192.168.2.128css
[root@nginx pkg]# unzip phpMyAdmin-4.8.5-all-languages.zip [root@nginx pkg]# unzip wordpress-5.0.3-zh_CN.zip ### 配置phpMyadmin [root@nginx pkg]# cd phpMyAdmin-4.8.5-all-languages [root@nginx phpMyAdmin-4.8.5-all-languages]# cp config.sample.inc.php config.inc.php [root@nginx phpMyAdmin-4.8.5-all-languages]# vim config.inc.php +17 ... $cfg['blowfish_secret'] = 'asfusd87fsdfgbsdfbkjsdfsd5513wefasdfbsuf763jwegfway6adgfa'; /* YOU MUST FILL IN THIS FOR COOKIE AUTH! */ $cfg['Servers'][$i]['host'] = '192.168.2.133'; ... [root@nginx phpMyAdmin-4.8.5-all-languages]# mkdir tmp [root@nginx phpMyAdmin-4.8.5-all-languages]# chmod 777 tmp ### 配置wordpress [root@nginx pkg]# cd wordpress [root@nginx wordpress]# cp wp-config-sample.php wp-config.php [root@nginx wordpress]# vim wp-config.php ... /** WordPress数据库的名称 */ define('DB_NAME', 'wordpress'); /** MySQL数据库用户名 */ define('DB_USER', 'wordpress'); /** MySQL数据库密码 */ define('DB_PASSWORD', '123'); /** MySQL主机 */ define('DB_HOST', '192.168.2.133'); ... ### 将修改好的项目文件分别发给ap,np的Document目录 [root@nginx pkg]# rsync -az phpMyAdmin-4.8.5-all-languages wordpress root@192.168.2.129:/usr/local/apache24/htdocs/ [root@nginx pkg]# rsync -az phpMyAdmin-4.8.5-all-languages wordpress root@192.168.2.130:/data/nginx/ [root@nginx pkg]# rsync -az phpMyAdmin-4.8.5-all-languages wordpress root@192.168.2.133:/data/nginx/
### 192.168.2.128 [root@nginx ~]# vim /etc/nginx/nginx.conf ... include conf.d/*.conf; ##在http标签内添加 upstream websrv_php { ##添加php动态解析服务器 #ip_hash; server 192.168.2.129:80 weight=1; server 192.168.2.130:80 weight=1; } upstream websrv_static { ##添加static静态文件解析服务器 server 192.168.2.133:80 weight=1; } ... [root@nginx ~]# mkdir /etc/nginx/conf.d/ ##建立虚拟主机目录 [root@nginx ~]# vim /etc/nginx/conf.d/upstream.conf server { listen 80; server_name www.ifan.com; root /data/nginx; index index.html; location ~* \.(jpeg|gif|jpg|png|html|css)$ { proxy_pass http://websrv_static; } location / { proxy_pass http://websrv_php; } } [root@nginx ~]# nginx -t ##没有报错,则下一步启动便可 [root@nginx ~]# nginx ## 配置192.168.2.129 ap主机咱们就使用默认主机提供服务,注意上面配置ap主机时已经完成了index.php文件的默认识别及目录显示功能一关闭,若未进行配置则返回配置,如下只更名 [root@ap htdocs]# mv phpMyAdmin-4.8.5-all-languages pam [root@ap htdocs]# mv wordpress blog ## 配置192.168.2.130 [root@nginx ~]# vim /etc/nginx/nginx.conf ... include conf.d/*.conf; ##在http标签内添加 ... [root@nginx ~]# mkdir /etc/nginx/conf.d/ ##建立虚拟主机目录 [root@nginx ~]# vim /etc/nginx/conf.d/project.conf server { listen 80; server_name www.ifan.com; root /data/nginx; index index.html index.php; location / { } location ~* \.php$ { fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #fastcgi_param SCRIPT_FILENAME /data/nginx$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; } } [root@np nginx]# mv phpMyAdmin-4.8.5-all-languages pam [root@np nginx]# mv wordpress blog [root@np nginx]# nginx -t && nginx ## 192.168.2.133 [root@nginx ~]# vim /etc/nginx/nginx.conf ... include conf.d/*.conf; ##在http标签内添加 ... [root@nginx ~]# mkdir /etc/nginx/conf.d/ ##建立虚拟主机目录 [root@nginx ~]# vim /etc/nginx/conf.d/project.conf server { listen 80; server_name www.ifan.com; root /data/nginx; index index.html index.php; location / { } } [root@np nginx]# mv phpMyAdmin-4.8.5-all-languages pam [root@np nginx]# mv wordpress blog [root@np nginx]# nginx -t && nginx
cat /etc/hosts ... 192.168.2.128 www.ifan.com ...
因为Firefox和Chrom老是加载缓存,测试出效果很不易,我访问的是php的测试文件,正常页面会显示phpinfo的基本信息及php的扩展信息,这里能够看出我nginx设置的是基本轮询,能够根据需求设置成基于源地址绑定访问html
访问blog系统:默认安装好不是这个界面,是须要自行配置的界面前端
访问pam系统:
这个并不是错误,缘由是使用负载均衡轮询的时候,第一次请求phpMyAdmin主页的时ap进行处理,页面返回的cookie存放在ap上.填写用户名密码提交以后,是np进行处理的,此时给页面的cookie不是ap上的cookie,因此会报错,解决方案很简单,在nginx代理时使用 ip_hash 便可(配置文件中我已添加,只须要打开便可注释便可)mysql
查看192.168.2.133Nginx日志,均是静态页面的访问:linux