项目案例:部署社交网站(svn服务器)全面过程
案例需求:社交网站采用PHP语言开发,为了管理开发代码,搭建SVN服务器进行版本控制,社交网站的第一个版本部署在LNMP平台上,前端为nginx服务器,通过fastcgi协议访问后端的PHP服务器。这里实现动静分离,动态页面交给PHP服务器来处理,静态页面交给nginx服务器来处理。
案例环境:
主机 |
操作系统 |
IP地址 |
主要软件 |
功能 |
SVN服务器 |
Centos7.4 |
192.168.80.181 www.aa.com |
subversion |
为PHP服务器创建repo目录的访问账户,通知PHP程序员可以导入代码 |
Nginx服务器 |
Centos7.4 |
192.168.80.182 www.ab.com |
nginx-1.13.7.tar.gz |
显示网站的静态页面 |
PHP服务器 |
Centos7.4 |
192.168.80.183 www.ac.com |
php-7.1.10.tar.bz |
显示网站的动态页面 |
实验步骤:
-----1.部署SVN服务器在181主机上,要公网环境-------
[[email protected] ~]# service firewalld stop
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum install -y subversion
[[email protected] ~]# svnserve --version //查看版本
[[email protected] ~]# mkdir -p /opt/svn/repo
[[email protected] ~]# svnadmin create /opt/svn/repo/ //为php程序员创建仓库目录repo(用于统一存储开发数据)
[[email protected] ~]# ls /opt/svn/repo/
如上图,当创建新仓库后,之前创建的/opt/svn/repo目录里就会多了一些目录及文件。
注:conf ——用户认证和权限配置文件
dd ——版本控制信息产生的数据
format ——当前代码库账号数据
----------调整SVN参数-------------
[[email protected] ~]# cd /opt/svn/repo/
[[email protected] repo]# vi conf/svnserve.conf //修改以下几行
19 anon-access = none //匿名用户没有任何权限
20 auth-access = write //认证用户有写权限
27 password-db = /opt/svn/repo/conf/passwd //用户的密码文件
34 authz-db = /opt/svn/repo/conf/authz //用户的信息文件
[[email protected] repo]# svnserve -d -r /opt/svn/repo/ //启动SVN服务(停止 svnserve用killall -9 svnserve )
[[email protected] repo]# netstat -anptu | grep svnserve //3690端口监听
[[email protected] repo]# vi conf/passwd //创建账户密码文件
alpha = alphapass //格式:用户名=用户密码,这里为php程序员alpha建立用户
sysadmin = sysadminpass
[[email protected] repo]# vi conf/authz //创建权限文件,新增以下内容
[/]
sysadmin = r
alpha = r
[/webphp]
sysadmin = r //运维工程师账户具有读权限,用于部署
alpha = rw //alpha用户对此目录有读写权限
[[email protected] repo]# mkdir webphp //建立用户存储开发数据的子目录webphp
[[email protected] repo]# svn import webphp file:///opt/svn/repo/webphp/ -m "init web" //初始化svn目录
可以在宿主机上安装SVN的Windows客户端,然后导入代码
svn co svn://192.168.80.181/webphp
---------以下内容为扩展-------------------
在win7上安装svn客户端软件:
输入svn服务器地址:
在win7上用记事本写一个网页文件test.html
然后把test.html和fruit.jpg文件拖到webphp文件夹中
在nginx服务器上进行同步:
[[email protected] ~]# cd /usr/local/nginx/html/
[[email protected] html]# yum install -y subversion-1.7.14-10.el7.x86_64
[[email protected] html]# svn co svn://192.168.80.181/webphp
在php服务器上进行同步:
[[email protected] ~]# cd /var/www/html/
[[email protected] html]# yum install -y subversion-1.7.14-10.el7.x86_64
[[email protected] html]# svn co svn://192.168.80.181/webphp
浏览器访问:http://192.168.80.182/test.html
注意:网站开发人员修改网页一般在Windows上操作,修改完成后通过SVN服务器上传nginx和PHP。
---------扩展内容结束-------------
-----2.部署nginx服务器在182主机上,要公网环境-------
[[email protected] ~]# service firewalld stop
[[email protected] ~]# systemctl disable firewalld
[[email protected] ~]# setenforce 0
[[email protected] ~]# yum -y install \
pcre-devel \
zlib-devel
[[email protected] ~]# useradd -M -s /sbin/nologin nginx
[[email protected] ~]# tar xzvf nginx-1.13.7.tar.gz -C /usr/src/
[[email protected] ~]# cd /usr/src/nginx-1.13.7
[[email protected] ~]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module
[[email protected] ~]# make && make install
[[email protected] ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[[email protected] ~]# nginx -t //配置文件语法检查
[[email protected] ~]# nginx //启动服务
[[email protected] ~]# killall -1 nginx //安全重启
[[email protected] ~]# killall -3 nginx //停止服务
[[email protected] ~]# vi /etc/init.d/nginx
#!/bin/bash
# chkconfig: 35 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[[email protected] ~]# chmod +x /etc/init.d/nginx
[[email protected] ~]# chkconfig --add nginx
[[email protected] ~]# vi /usr/local/nginx/conf/nginx.conf //配置后端php程序的fastcgi访问接口
user nginx nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html/webphp;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ { //动态请求给PHP服务器183
root /var/www/html/webphp;
fastcgi_pass 192.168.80.183:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/webphp$fastcgi_script_name;
include fastcgi_params;
}
}
}
--------编写一个静态页面用于测试---------
[[email protected] ~]# mkdir -p /usr/local/nginx/html/webphp
[[email protected] ~]# vi /usr/local/nginx/html/webphp/index.html
hello! this is nginx provides static pages!
浏览器访问静态页面http://192.168.80.182/index.html
-----3.部署PHP服务器在183主机上,要公网环境-------
[[email protected] ~]# yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel
[[email protected] ~]# tar xjvf php-7.1.10.tar.bz2
[[email protected] ~]# cd php-7.1.10
[[email protected] ~]# ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--enable-fpm \
--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
[[email protected] ~]# make && make install
[[email protected] ~]# cp php.ini-development /usr/local/php/lib/php.ini
[[email protected] ~]# vi /usr/local/php/lib/php.ini
mysqli.default_socket = /usr/local/mysql/mysql.sock
date.timezone = Asia/Shanghai
[[email protected] ~]# /usr/local/php/bin/php -m //验证安装的模块
-----------配置及优化FPM模块--------
[[email protected] ~]# cd /usr/local/php/etc/
[[email protected] ~]# cp php-fpm.conf.default php-fpm.conf
[[email protected] ~]# cd /usr/local/php/etc/php-fpm.d/
[[email protected] ~]# cp www.conf.default www.conf
[[email protected] ~]# vi www.conf
192.168.80.183:9000
[[email protected] ~]# cd /usr/local/php/etc/
[[email protected] ~]# vi php-fpm.conf
pid = run/php-fpm.pid
[[email protected] ~]# /usr/local/php/sbin/php-fpm -c /usr/local/php/etc/php.ini //启动php-fpm模块
[[email protected] ~]# netstat -anpt | grep 9000
[[email protected] ~]# ln -s /usr/local/php/bin/* /usr/local/bin/
[[email protected] ~]# ps aux | grep -c "php-fpm" //结果
-------编写一个动态页面用于测试---------
[[email protected] ~]# mkdir -p /var/www/html/webphp
[[email protected] ~]# vi /var/www/html/webphp/index.php
<?php
phpinfo();
?>
在浏览器里输入nginx服务器的IP 可以访问动态PHP页面
http://192.168.80.182/index.php
说明静态网页有nginx服务器处理,动态网页由php服务器处理,实现了动静分离。
4.部署MYSQL数据库
按PHP程序员需求在MySQL主服务器上新建一个数据库,并使创建PHP程序的用户对数据库有读写权限