nginx+php-fpm+mysql分离部署详解

来源:https://www.cnblogs.com/tae44/p/4667273.html

相信大家将这三者部署到同一台主机应该已经不陌生了,今天在这里,给大家演示一下如何将三者部署到三台主机上。

实验系统:CentOS 6.6_x86_64

实验前提:大部分软件使用编译安装,请提前准备好编译环境,防火墙和selinux都关闭

实验软件:nginx-1.9.3 mariadb-10.0.20 php-5.6.11 memcache-2.2.7 xcache-3.2.0

实验拓扑:

一、安装nginx

  1.解决依赖关系:

    需要专门安装pcre-devel包:

1 yum -y install pcre-devel

  2.添加nginx用户:

1 useradd -r nginx

  3.解压并编译安装nginx:

复制代码

 1 tar xf nginx-1.9.3.tar.gz 
 2 cd nginx-1.9.3
 3 ./configure \
 4   --prefix=/usr/local/nginx \                    //安装位置
 5   --sbin-path=/usr/local/nginx/sbin/nginx \            //程序文件
 6   --conf-path=/etc/nginx/nginx.conf \                //配置文件安装位置
 7   --error-log-path=/var/log/nginx/error.log \           //错误日志安装位置
 8   --http-log-path=/var/log/nginx/access.log \           //访问日志安装位置
 9   --pid-path=/var/run/nginx/nginx.pid  \              //pid文件位置
10   --lock-path=/var/lock/nginx.lock \                //锁文件位置
11   --user=nginx \                            //运行进程时使用的用户身份
12   --group=nginx \                           //运行进程时使用的用户组
13   --with-http_ssl_module \                      //支持ssl模块
14   --with-http_flv_module \                      //支持flv模块
15   --with-http_stub_status_module \                 //支持stub_status模块
16   --with-http_gzip_static_module \                 //支持gzip_static模块
17   --http-client-body-temp-path=/var/tmp/nginx/client/ \    //存储HTTP客户端请求body体的临时文件位置
18   --http-proxy-temp-path=/var/tmp/nginx/proxy/ \        //存储HTTP代理的临时文件位置
19   --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \        //存储fasycgi临时文件位置
20   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \         //存储uwsgi临时文件位置
21   --http-scgi-temp-path=/var/tmp/nginx/scgi \          //存储scgi临时文件位置
22   --with-pcre                             //支持pcre库
23 make && make install

复制代码

  4.提供脚本文件:

复制代码

  1 vim /etc/init.d/nginx
  2 --------------------------------
  3 #!/bin/sh
  4 #
  5 # nginx - this script starts and stops the nginx daemon
  6 #
  7 # chkconfig:   - 85 15 
  8 # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
  9 #               proxy and IMAP/POP3 proxy server
 10 # processname: nginx
 11 # config:      /etc/nginx/nginx.conf
 12 # config:      /etc/sysconfig/nginx
 13 # pidfile:     /var/run/nginx.pid
 14  
 15 # Source function library.
 16 . /etc/rc.d/init.d/functions
 17  
 18 # Source networking configuration.
 19 . /etc/sysconfig/network
 20  
 21 # Check that networking is up.
 22 [ "$NETWORKING" = "no" ] && exit 0
 23  
 24 nginx="/usr/local/nginx/sbin/nginx"
 25 prog=$(basename $nginx)
 26  
 27 NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 28  
 29 [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 30  
 31 lockfile=/var/lock/subsys/nginx
 32  
 33 make_dirs() {
 34    # make required directories
 35    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
 36    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
 37    for opt in $options; do
 38        if [ `echo $opt | grep '.*-temp-path'` ]; then
 39            value=`echo $opt | cut -d "=" -f 2`
 40            if [ ! -d "$value" ]; then
 41                # echo "creating" $value
 42                mkdir -p $value && chown -R $user $value
 43            fi
 44        fi
 45    done
 46 }
 47  
 48 start() {
 49     [ -x $nginx ] || exit 5
 50     [ -f $NGINX_CONF_FILE ] || exit 6
 51     make_dirs
 52     echo -n $"Starting $prog: "
 53     daemon $nginx -c $NGINX_CONF_FILE
 54     retval=$?
 55     echo
 56     [ $retval -eq 0 ] && touch $lockfile
 57     return $retval
 58 }
 59  
 60 stop() {
 61     echo -n $"Stopping $prog: "
 62     killproc $prog -QUIT
 63     retval=$?
 64     echo
 65     [ $retval -eq 0 ] && rm -f $lockfile
 66     return $retval
 67 }
 68  
 69 restart() {
 70     configtest || return $?
 71     stop
 72     sleep 1
 73     start
 74 }
 75  
 76 reload() {
 77     configtest || return $?
 78     echo -n $"Reloading $prog: "
 79     killproc $nginx -HUP
 80     RETVAL=$?
 81     echo
 82 }
 83  
 84 force_reload() {
 85     restart
 86 }
 87  
 88 configtest() {
 89   $nginx -t -c $NGINX_CONF_FILE
 90 }
 91  
 92 rh_status() {
 93     status $prog
 94 }
 95  
 96 rh_status_q() {
 97     rh_status >/dev/null 2>&1
 98 }
 99  
100 case "$1" in
101     start)
102         rh_status_q && exit 0
103         $1
104         ;;
105     stop)
106         rh_status_q || exit 0
107         $1
108         ;;
109     restart|configtest)
110         $1
111         ;;
112     reload)
113         rh_status_q || exit 7
114         $1
115         ;;
116     force-reload)
117         force_reload
118         ;;
119     status)
120         rh_status
121         ;;
122     condrestart|try-restart)
123         rh_status_q || exit 0
124             ;;
125     *)
126         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
127         exit 2
128 esac
129 --------------------------------
130 chmod +x /etc/init.d/nginx
131 chkconfig --add nginx
132 chkconfig nginx on
133 service nginx start

复制代码

  5.测试访问页面,nginx安装完毕:

二、安装mysql

  1.添加mysql用户和创建数据目录:

1 useradd -r mysql
2 mkdir -pv /mydata/data
3 chown -R mysql:mysql /mydata/data

  2.解压并初始化mysql:

复制代码

1 tar xf mariadb-10.0.20-linux-x86_64.tar.gz -C /usr/local/
2 cd /usr/local/
3 ln -sv mariadb-10.0.20-linux-x86_64 mysql
4 cd mysql/
5 chown -R root:mysql .
6 scripts/mysql_install_db --user=mysql --datadir=/mydata/data/

复制代码

  3.提供配置文件:

1 cp support-files/my-large.cnf /etc/my.cnf
2 vim /etc/my.cnf
3 修改此文件中thread_concurrency的值为你的CPU个数乘以2,比如:thread_concurrency = 2
4 另外还需要添加如下行指定mysql数据文件的存放位置:datadir = /mydata/data

  4.提供脚本文件:

1 cp support-files/mysql.server /etc/init.d/mysqld
2 chkconfig --add mysqld
3 chkconfig mysqld on
4 service mysqld start

  使用mysql目录的下的bin/mysql去连接mysql,出现如下画面表示连接成功:

  5.输出mysql的man手册至man命令的查找路径:   

    编辑/etc/man.config,添加如下行即可:MANPATH  /usr/local/mysql/man

  6.输出mysql的头文件至系统头文件路径/usr/include:

    这可以通过简单的创建链接实现: 

1 ln -sv /usr/local/mysql/include  /usr/include/mysql

  7.输出mysql的库文件给系统库查找路径:

1 echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
2 ldconfig

三、安装PHP

  1.解决依赖关系:

1 yum -y install libxml2-devel bzip2-devel libcurl-devel libmcrypt-devel

  2.编译安装php:

复制代码

 1 ./configure --prefix=/usr/local/php \      //安装位置
 2  --with-mysql \                   //支持mysql
 3  --with-pdo-mysql \                //支持pdo模块
 4  --with-mysqli \                  //支持mysqli模块         
 5  --with-openssl \                  //支持openssl模块
 6  --enable-fpm \                   //支持fpm模式
 7  --enable-sockets \                //启用socket支持
 8  --enable-sysvshm \                //启用系统共享内存支持
 9  --enable-mbstring \                //使多字节字符串的支持
10  --with-freetype-dir \              //设置FreeType安装前缀路径
11  --with-jpeg-dir \                //设置libjpeg安装前缀路径
12  --with-png-dir \                 //设置libpng安装前缀路径
13  --with-zlib-dir \                //设置libz安装前缀路径
14  --with-libxml-dir=/usr \            //设置libxml2安装路径
15  --enable-xml \                 
16  --with-mhash \                 //支持mhash
17  --with-mcrypt \                 //支持mcrypt
18  --with-config-file-path=/etc \        //配置文件路径
19  --with-config-file-scan-dir=/etc/php.d \   //配置文件扫描路径
20  --with-bz2 \                      //支持BZip2
21  --with-curl                   //支持curl
22 make && make install    

复制代码

  3.提供配置文件:

1 cp php.ini-production /etc/php.ini

  4.为php-fpm提供脚本:

1 cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
2 chmod +x /etc/init.d/php-fpm
3 chkconfig --add php-fpm
4 chkconfig php-fpm on

  5.提供php-fpm配置文件并编辑:

复制代码

1 cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
2 ------------------------------------------------------
3 pm.max_children = 150
4 pm.start_servers = 8
5 pm.min_spare_servers = 5
6 pm.max_spare_servers = 10
7 pid = /usr/local/php/var/run/php-fpm.pid

复制代码

  6.启动php-fpm服务:

1 service php-fpm start

四、整合nginx与PHP

  1.nginx服务器建立网页文件存放目录/www,并修改其权限:

1 mkdir /www
2 chown -R nginx:nginx /www

  2.修改nginx配置文件:

复制代码

 1 vim /etc/nginx/nginx.conf
 2 --------------------------------------
 3 location / {
 4             root   /www;
 5             index  index.php index.html index.htm;
 6         }
 7 
 8 location ~ \.php$ {
 9             root           /www;
10             fastcgi_pass   192.168.19.92:9000;
11             fastcgi_index  index.php;
12             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
13             include        fastcgi_params;
14         }

复制代码

  3.修改fastcgi_params文件为:

复制代码

 1 fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
 2 fastcgi_param  SERVER_SOFTWARE    nginx;
 3 fastcgi_param  QUERY_STRING       $query_string;
 4 fastcgi_param  REQUEST_METHOD     $request_method;
 5 fastcgi_param  CONTENT_TYPE       $content_type;
 6 fastcgi_param  CONTENT_LENGTH     $content_length;
 7 fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
 8 fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
 9 fastcgi_param  REQUEST_URI        $request_uri;
10 fastcgi_param  DOCUMENT_URI       $document_uri;
11 fastcgi_param  DOCUMENT_ROOT      $document_root;
12 fastcgi_param  SERVER_PROTOCOL    $server_protocol;
13 fastcgi_param  REMOTE_ADDR        $remote_addr;
14 fastcgi_param  REMOTE_PORT        $remote_port;
15 fastcgi_param  SERVER_ADDR        $server_addr;
16 fastcgi_param  SERVER_PORT        $server_port;
17 fastcgi_param  SERVER_NAME        $server_name;

复制代码

  4.在PHP服务器上,建立nginx用户,要保证和nginx服务器上的nginx用户id号、组id号一致:

  5.修改php-fpm配置文件,并重启:

复制代码

1 vim /usr/local/php/etc/php-fpm.conf
2 ---------------------------------------------
3 listen = 192.168.19.92:9000         //监听物理网卡地址,供其它机器调用
4 user = nginx                           //php-fpm以nginx用户运行
5 group = nginx
6 ---------------------------------------------
7 service php-fpm restart

复制代码

  6.在PHP服务器上创建/www目录,并开启nfs服务:

复制代码

1 mkdir /www
2 chown -R nginx:nginx /www
3 vim /etc/exports
4 ---------------------------------------------
5 /www    192.168.19.0/24(rw,no_root_squash)
6 ---------------------------------------------
7 service nfs start

复制代码

  7.nginx服务器挂载nfs文件,并测试php,测试成功后删除index.php:

复制代码

1 mount -t nfs 192.168.19.92:/www /www
2 vim /www/index.php
3 ---------------------------------------
4 <?php
5      phpinfo();
6 ?>
7 --------------------------------------
8 service nginx restart

复制代码

五、整合PHP与MYSQL

  在mysql服务器上创建php服务器能够访问的数据库和用户:

1 /usr/local/mysql/bin/mysql
2 --------------------------------------------
3 CREATE DATABASE wordpress;
4 GRANT ALL ON wordpress.* TO 'wordpress'@'192.168.19.92' IDENTIFIED BY '123456';
5 FLUSH PRIVILEGES;

六、安装wordpress

  1.在/www文件夹下放入网页文件

  2.访问http://192.168.19.83,并按提示进行安装,配置没问题则会安装成功

七、为php安装xcache

  1.解压并安装:

1 tar xf xcache-3.2.0.tar.gz 
2 cd xcache-3.2.0
3 /usr/local/php/bin/phpize
4 ./configure --enable-xcache --with-php-config=/usr/local/php/bin/php-config
5 make && make install

    完成后,会出现:Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

  2.加载模块:

1 vim /etc/php.ini
2 -----------------------------------
3 extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/xcache.so        //找到extension配置的地方,加上此句

八、为php安装memcache

  1.解压并安装:

1 tar xf memcache-2.2.7.tgz 
2 cd memcache-2.2.7
3 /usr/local/php/bin/phpize
4  ./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
5 make && make install

    完成后,会出现:Installing shared extensions:     /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/

  2.加载模块:

1 vim /etc/php.ini
2 -----------------------------------
3 extension = /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/memcache.so        //找到extension配置的地方,加上此句

  3.两个加速模块都安装完毕,重启php-fpm:

1 service php-fpm restart

  4.创建一个php测试页,并查看模块是否加载成功:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
非常感谢您的提问,我将为您详细描述 ansible-varnish-nginx-php-fpm-ftp-mysql部署流程。 前置条件: - 安装 Ansible 工具 - 所有主机都配置好 SSH 免密登录 - 所有主机都已安装 Python 2.x 或 Python 3.x 步骤: 1. 编写 Ansible 配置文件 创建一个名为 inventory 的文件,并在其中定义要部署的主机列表和相关变量: ``` [web] webserver ansible_host=192.168.1.10 ansible_user=<your_user> ansible_ssh_private_key_file=<your_ssh_key> dbserver ansible_host=192.168.1.11 ansible_user=<your_user> ansible_ssh_private_key_file=<your_ssh_key> [web:vars] nginx_version=1.14.1 php_version=7.2 mysql_version=5.7 ``` 2. 编写 Ansible Playbook 创建一个名为 playbook.yml 的文件,并编写 Ansible Playbook。以下是一个示例 Playbook,包含安装和配置 varnish、nginxPHP-FPM、FTP 和 MySQL。 ``` - hosts: web become: true vars_files: - vars.yml tasks: # 安装 Varnish - name: Install Varnish yum: name: varnish state: present # 安装 Nginx - name: Install Nginx yum: name: nginx state: present notify: - Restart Nginx # 配置 Nginx - name: Configure Nginx template: src: templates/nginx.conf.j2 dest: /etc/nginx/nginx.conf notify: - Restart Nginx # 安装 PHP-FPM - name: Install PHP-FPM yum: name: php-fpm state: present notify: - Restart PHP-FPM # 配置 PHP-FPM - name: Configure PHP-FPM template: src: templates/php-fpm.conf.j2 dest: /etc/php-fpm.conf notify: - Restart PHP-FPM # 安装 FTP - name: Install FTP yum: name: vsftpd state: present notify: - Restart FTP # 配置 FTP - name: Configure FTP template: src: templates/vsftpd.conf.j2 dest: /etc/vsftpd/vsftpd.conf notify: - Restart FTP # 安装 MySQL - name: Install MySQL yum: name: mysql-community-server state: present notify: - Start MySQL # 配置 MySQL - name: Configure MySQL template: src: templates/my.cnf.j2 dest: /etc/my.cnf notify: - Start MySQL handlers: # 重启 Nginx - name: Restart Nginx systemd: name: nginx state: restarted # 重启 PHP-FPM - name: Restart PHP-FPM systemd: name: php-fpm state: restarted # 重启 FTP - name: Restart FTP systemd: name: vsftpd state: restarted # 启动 MySQL - name: Start MySQL systemd: name: mysqld state: started ``` 3. 准备模板文件 创建模板文件,用于生成配置文件。以下是模板文件示例代码: nginx.conf.j2 ``` user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; events { worker_connections 1024; use epoll; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.php index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } include /etc/nginx/conf.d/*.conf; } ``` php-fpm.conf.j2 ``` [global] pid = /var/run/php-fpm/php-fpm.pid error_log = /var/log/php-fpm/error.log emergency_restart_threshold = 5 emergency_restart_interval = 1m process_control_timeout = 10 daemonize = no [www] user = nginx group = nginx listen = 127.0.0.1:9000 listen.owner = nginx listen.group = nginx listen.mode = 0660 pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3 pm.max_requests = 500 ``` vsftpd.conf.j2 ``` anonymous_enable=NO local_enable=YES write_enable=YES local_umask=022 dirmessage_enable=YES xferlog_enable=YES xferlog_file=/var/log/xferlog xferlog_std_format=YES chroot_local_user=YES allow_writeable_chroot=YES local_root=/var/www/html/ listen=YES listen_ipv6=NO pam_service_name=vsftpd userlist_enable=YES tcp_wrappers=YES ``` my.cnf.j2 ``` [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock user=mysql symbolic-links=0 skip-external-locking key_buffer_size = 16M max_allowed_packet = 256M table_open_cache = 16384 sort_buffer_size = 512K net_buffer_length = 16K myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 16M query_cache_limit = 1M log-bin=mysql-bin binlog_format=ROW server-id=1 innodb_buffer_pool_size = 256M innodb_log_file_size = 64M default-storage-engine=innodb character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci [client] socket=/var/lib/mysql/mysql.sock [mysql] socket=/var/lib/mysql/mysql.sock ``` 4. 运行 Ansible Playbook 运行以下命令来运行 Ansible Playbook: ``` $ ansible-playbook -i inventory playbook.yml ``` 此命令将在指定主机上按顺序执行 Playbook 中的每个任务,安装和配置所有必需的软件包和服务,最终实现 ansible-varnish-nginx-php-fpm-ftp-mysql部署。 感谢您提供的问题,希望可以帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值