LNMP分离式部署
依图:分别部署
-
部署Nginx服务
安装Nginx,并部署环境
[root@zhangmingjuan ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/
[root@zhangmingjuan ~]# cd /usr/src/nginx-1.10.2/
[root@zhangmingjuan nginx-1.10.2]# mount /dev/sr0 /media/cdrom
mount: block device /dev/sr0 is write-protected, mounting read-only
[root@zhangmingjuan nginx-1.10.2]# yum -y clean all
Loaded plugins: fastestmirror
Cleaning repos: c6-media
Cleaning up Everything
Cleaning up list of fastest mirrors
[root@zhangmingjuan nginx-1.10.2]# yum makecache
[root@zhangmingjuan nginx-1.10.2]# yum install -y pcre-devel openssl-devel
[root@zhangmingjuan nginx-1.10.2]# useradd -s /sbin/nologin -M www #创建程序用户www
[root@zhangmingjuan nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
[root@zhangmingjuan nginx-1.10.2]# make && make install
[root@zhangmingjuan nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ #做一个软链接
修改配置文件,并启动服务
[root@zhangmingjuan nginx-1.10.2]# cd /usr/local/nginx/conf/
[root@zhangmingjuan conf]# egrep -v "#|^$" nginx.conf.default > nginx.conf
[root@zhangmingjuan conf]# vim nginx.conf
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 www.yunjisuan.com;
root /www; #这个目录全局生效
location / {
index index.php; #这里写上映射文件,自动会去找文件
}
location ~.*\.(php|php5)?$ { #注意这里空格
fastcgi_pass 192.168.204.149:9000; #这个IP地址是响应PHP服务的IP地址:PHP服务监听的默认端口
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
[root@zhangmingjuan conf]# /usr/local/nginx/sbin/nginx #启动Nginx服务
[root@zhangmingjuan conf]# ss -antup | grep nginx
tcp LISTEN 0 128 *:80 *:* users:(("nginx",4853,6),("nginx",4854,6))
做网页目录做映射
[root@zhangmingjuan /]# cd www/ #进入动态网页目录下,创建动态网页
[root@zhangmingjuan www]# echo "666666" > index.php
[root@zhangmingjuan www]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts #虚拟机里将主机的ip地址和域名地址映射到/etc/hosts文件下
[root@zhangmingjuan www]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.204.150 www.yunjisuan.com
本地系统做映射
-
部署mysql服务
创建mysql用户账号
[root@zhangmingjuan ~]# groupadd mysql
[root@zhangmingjuan ~]# useradd -s /sbin/nologin -g mysql -M mysql
[root@zhangmingjuan ~]# tail -1 /etc/passwd
mysql:x:500:500::/home/mysql:/sbin/nologin
[root@zhangmingjuan ~]# id mysql
uid=500(mysql) gid=500(mysql) groups=500(mysql)
采用二进制方式安装MySQL
[root@zhangminagjuan ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
[root@zhangminagjuan ~]# cd /usr/local/
[root@zhangminagjuan local]# mv mysql-5.5.32-linux2.6-x86_64 mysql-5.5.32
[root@zhangminagjuan local]# ln -s mysql-5.5.32 mysql
[root@zhangminagjuan local]# ls
bin games lib libexec mysql-5.5.32 sbin src
etc include lib64 mysql nginx share
[root@zhangminagjuan local]# cd /usr/local/mysql
[root@zhangminagjuan mysql]# ls
bin data include lib mysql-test scripts sql-bench
COPYING docs INSTALL-BINARY man README share support-files
#提示:
二进制安装包,仅需要解压就可以了,不需要执行cmake/configure,make,make install等过程
初始化 MySQL配置文件my.cnf
[root@zhangmingjuan mysql]# cd /usr/local/mysql
[root@zhangmingjuan mysql]# ls -l support-files/*.cnf
-rw-r--r-- 1 7161 wheel 4691 Jun 19 2013 support-files/my-huge.cnf
-rw-r--r-- 1 7161 wheel 19759 Jun 19 2013 support-files/my-innodb-heavy-4G.cnf
-rw-r--r-- 1 7161 wheel 4665 Jun 19 2013 support-files/my-large.cnf
-rw-r--r-- 1 7161 wheel 4676 Jun 19 2013 support-files/my-medium.cnf
-rw-r--r-- 1 7161 wheel 2840 Jun 19 2013 support-files/my-small.cnf
[root@zhangmingjuan mysql]# /bin/cp support-files/my-small.cnf /etc/my.cnf
初始化MySQL数据库文件
[root@zhangminagjuan ~]# mkdir -p /usr/local/mysql/data //建立MySQL数据文件目录
[root@zhangminagjuan ~]# chown -R mysql.mysql /usr/local/mysql //授权mysql用户管理MySQL的安装目录
[root@zhangminagjuan ~]# yum -y install libaio //光盘源安装依赖包,否则下一步的编译会报错
[root@zhangminagjuan ~]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
#初始化MySQL数据库文件,会有很多信息提示,如果没有ERROR级别的错误,会有两个OK的字样,表示初始化成功,否则就要解决初始化的问题
以上命令的主要作用是在/usr/local/mysql/data/目录下生成MySQL正确运行所必需的基本数据库文件,其功能是对MySQL权限,状态等进行管理。
配置并启动MySQL数据库
[root@zhangmingjuan ~]# cd /usr/local/mysql
[root@zhangmingjuan mysql]# ls
bin data include lib mysql-test scripts sql-bench
COPYING docs INSTALL-BINARY man README share support-files
[root@zhangmingjuan mysql]# /bin/cp support-files/mysql.server /etc/init.d/mysqld #拷贝MySQL启动脚本到MySQL的命令路径
[root@zhangmingjuan mysql]# chmod +x /etc/init.d/mysqld
[root@zhangmingjuan mysql]# /etc/init.d/mysqld start #启动mysql数据库
Starting MySQL... SUCCESS!
[root@zhangmingjuan mysql]# netstat -antup | grep mysql #检查MySQL数据库是否启动
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1485/mysqld
[root@zhangmingjuan mysql]# chkconfig --add mysqld #设置MySQL开机自启动
[root@zhangmingjuan mysql]# chkconfig mysqld on
[root@zhangmingjuan mysql]# chkconfig --list mysqld
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@zhangmingjuan mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/ #配置mysql命令的全局使用路径
[root@zhangmingjuan mysql]# which mysqladmin
/usr/local/bin/mysqladmin
数据库方配置
[root@zhangmingjuan mysql]# mysqladmin -u root password '666666'
[root@zhangmingjuan mysql]# mysql -uroot -p666666
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.32 MySQL Community Server (GPL)
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> grant all on *.* to 'yunjisuan'@'%' identified by '123123';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
-
部署PHP服务
创建www程序用户
[root@zhangmingjuan ~]# groupadd www
[root@zhangmingjuan ~]# useradd -s /sbin/nologin -g www -M www
[root@zhangmingjuan ~]# tail -1 /etc/passwd
www:x:500:500::/home/www:/sbin/nologin
[root@zhangmingjuan ~]# id www
uid=500(www) gid=500(www) groups=500(www)
[root@zhangmingjuan ~]# /usr/local/php/sbin/php-fpm
执行下面命令安装相关的lib软件包
[root@zhangmingjuan ~]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
[root@zhangmingjuan ~]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
[root@zhangmingjuan ~]# yum -y install openssl-devel openssl
[root@zhangminagjuan ~]# rpm -qa zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
libjpeg-turbo-devel-1.2.1-1.el6.x86_64
zlib-devel-1.2.3-29.el6.x86_64
libxml2-devel-2.7.6-14.el6.x86_64
1. #这里仅缺少libiconv-devel包 因为默认的yum源没有此包,后面会编译安装。
[root@zhangminagjuan ~]# rpm -qa freetype-devel libpng-devel gd libcurl-devel libxslt-devel
libxslt-devel-1.1.26-2.el6_3.1.x86_64
gd-2.0.35-11.el6.x86_64
libpng-devel-1.2.49-1.el6_2.x86_64
libcurl-devel-7.19.7-37.el6_4.x86_64
freetype-devel-2.3.11-14.el6_3.1.x86_64
libiconv源码编译 (按着步骤来)
[root@zhangminagjuan ~]# tar xf libiconv-1.14.tar.gz -C /usr/src/
[root@zhangminagjuan libiconv-1.14]# cd /usr/src/libiconv-1.14/
[root@zhangminagjuan libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install
[root@zhangmingjuan ~]# rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm #下面四个包必须按照步骤来
warning: libmcrypt-2.5.8-9.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:libmcrypt ########################################### [100%]
[root@zhangmingjuan ~]# rpm -ivh mhash-0.9.9.9-3.el6.x86_64.rpm
warning: mhash-0.9.9.9-3.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:mhash ########################################### [100%]
[root@zhangmingjuan ~]# rpm -ivh libmcrypt-devel-2.5.8-9.el6.x86_64.rpm
warning: libmcrypt-devel-2.5.8-9.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:libmcrypt-devel ########################################### [100%]
[root@zhangmingjuan ~]# rpm -ivh mcrypt-2.6.8-10.el6.x86_64.rpm
warning: mcrypt-2.6.8-10.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
Preparing... ########################################### [100%]
1:mcrypt ########################################### [100%]
编译安装PHP(FastCGI方式)服务
[root@localhost ~]# tar xf php-5.3.28.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/php-5.3.28/
[root@localhost php-5.3.28]# ./configure \
> --prefix=/usr/local/php5.3.28 \ #表示指定PHP的安装路径为/usr/local/php5.3.28
> --with-mysql=mysqlnd \ #!!!--with-mysql=mysqlnd替代--with-mysql=/usr/local/mysql(一体式),因为PHP软件里已经自带了连接MySQL的客户端工具。
> --with-iconv-dir=/usr/local/libiconv \
> --with-freetype-dir \
> --with-jpeg-dir \
> --with-png-dir \
> --with-zlib \
> --with-libxml-dir=/usr \
> --enable-xml \
> --disable-rpath \
> --enable-safe-mode \
> --enable-bcmath \
> --enable-shmop \
> --enable-sysvsem \
> --enable-inline-optimization \
> --with-curl \
> --with-curlwrappers \
> --enable-mbregex \
> --enable-fpm \
> --enable-mbstring \
> --with-mcrypt \
> --with-gd \
> --enable-gd-native-ttf \
> --with-openssl \
> --with-mhash \
> --enable-pcntl \
> --enable-sockets \
> --with-xmlrpc \
> --enable-zip \
> --enable-soap \
> --enable-short-tags \
> --enable-zend-multibyte \
> --enable-static \
> --with-xsl \
> --with-fpm-user=www \ #nginx表示指定PHP-FPM进程管理的用户为www,此处最好和Nginx服务用户统一
> --with-fpm-group=www \ #表示指定PHP-FPM进程管理的组为www,此处最好与Nginx服务用户组统一。
> --enable-ftp #表示激活PHP-FPM方式服务,即以FastCGIF方式运行PHP服务。
1. #特别强调:上述每行结尾的换行符反斜线(\)之后不能再有任何字符包括空格
[root@zhangminagjuan php-5.3.28]# make
#make最后的正确提示
Build complete.
Don't forget to run 'make test'.
[root@zhangminagjuan php-5.3.28]# make install //安装PHP生成文件到系统
配置PHP引擎配置文件php.ini
[root@zhangmingjuan ~]# ln -s /usr/local/php5.3.28/ /usr/local/php
[root@zhangmingjuan ~]# ls -l /usr/local/php
lrwxrwxrwx 1 root root 21 Nov 29 22:52 /usr/local/php -> /usr/local/php5.3.28/
[root@zhangmingjuan ~]# cd /usr/src/php-5.3.28/
[root@zhangmingjuan php-5.3.28]# ls php.ini*
php.ini-development php.ini-production
[root@zhangmingjuan php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini
[root@zhangmingjuan php-5.3.28]# ls -l /usr/local/php/lib/php.ini
-rw-r--r-- 1 root root 69627 Nov 29 22:53 /usr/local/php/lib/php.ini
配置PHP(FastCGI方式)的配置文件php-fpm.conf
[root@zhangminagjuan php-5.3.28]# /bin/cp php.ini-production /usr/local/php/lib/php.ini
[root@zhangminagjuan php-5.3.28]# ls -l /usr/local/php/lib/php.ini
-rw-r--r-- 1 root root 69627 Nov 27 22:33 /usr/local/php/lib/php.ini
[root@zhangminagjuan php-5.3.28]# cd /usr/local/php/etc/
[root@zhangminagjuan etc]# ls
pear.conf php-fpm.conf.default
[root@zhangminagjuan etc]# /bin/cp php-fpm.conf.default php-fpm.conf
[root@zhangmingjuan etc]# vim php-fpm.conf
[root@zhangmingjuan ~]# /usr/local/php/sbin/php-fpm
[root@zhangmingjuan ~]# ss -antup | grep 9000
tcp LISTEN 0 128 192.168.204.149:9000 *:* users:(("php-fpm",126716,7),("php-fpm",126717,0),("php-fpm",126718,0))
在PHP服务端相关配置(动态网页、指向数据库的代码)
[root@zhangmingjuan ~]# cd www/
[root@zhangmingjuan www]# cat test_mysql.php
<?php
//$link_id=mysql_connect('主机名','用户','密码');
$link_id=mysql_connect('192.168.204.152','yunjisuan','666666'); #这里的主机名是指mysql服务器的IP;用户密码是mysql里面的账户密码
if($link_id){
echo "mysql successful by Juanne \n";
}else{
echo mysql_error();
}
?>
[root@zhangmingjuan www]# ls
test_mysql.php
[root@zhangmingjuan www]# echo "`hostname -I`welcome" > index.php
[root@zhangmingjuan www]# ll -d /www/
drwxr-xr-x 2 www www 4096 Nov 30 02:51 /www/
[root@zhangmingjuan www]# ls
index.php test_mysql.php
!!最终结果
在Nginx服务端输入域名,结果如下
在本地浏览器输入域名结果显示如下:
只是输入域名,Nginx指引去找PHP服务的动态网页页面
输入域名及指定找数据库,Nginx找PHP,PHP根据开发写的代码找到mysql数据库拿到相应的数据