LNMP分离式部署搭建
首先准备3台虚拟机:
Nginx:192.168.200.70
PHP:192.168.200.71
MySQL:192.168.200.72
一、Nginx搭建
首先安装支持包
yum -y install pcre-devel openssl-devel gcc gcc-c++ make automake
创建程序用户
useradd -s /sbin/nologin -M www
解压缩和预配置,编译和安装
软件包在上文有
tar xf nginx-1.10.2.tar.gz -C /usr/src/
cd /usr/src/nginx-1.10.2.tar.gz
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
做Nginx软链接
ln -s /usr/local/nginx/sbin/* /usr/local/sbin
修饰一下Nginx配置文件
cd /usr/local/nginx/conf
egrep -v "#|^$" nginx.conf.default > nginx.conf
二、MySQL二进制搭建方法
useradd -s /sbin/nologin -M mysql
二进制方式安装MySQL
软件包上文有
tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local
cd /usr/local
mv mysql-5.5.32-linux2.6-x86_64 mysql-5.5.32
ln -s mysql-5.5.32 mysql
初始化MySQL配置文件到my.cnf
cd /usr/local/mysql
/bin/cp support-files/my-small.cnf /etc/my.cnf
工作中如果主机名,在你本地没有映射,有可能会有错误出现
echo "192.168.200.70 LNMP" >> /etc/hosts
初始化MySQL数据库文件
chown -R mysql.mysql /usr/local/mysql
光盘源安装依赖包,否则编译会报错
yum -y install libaio
/usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
设置MySQL启动脚本
cp support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
/etc/init.d/mysqld start
ss -antup | grep 3306
chkconfig --add mysqld
chkconfig mysqld on
chkconfig --list mysqld
制作软连接,让linux有mysql命令
ln -s /usr/local/mysql/bin/* /usr/local/bin
which mysql
创建MySQL密码
mysqladmin -uroot password '666666'
三、PHP服务搭建
安装PHP所需的lib库
yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
安装yum无法安装的libiconv库
软件包上文有
tar xf libiconv-1.14.tar.gz -C /usr/src/
cd /usr/src/libiconv-1.14/
./configure --prefix=/usr/local/libiconv && make && make install
安装libmcrypt库、安装mhash加密扩展库、安装mcrvpt加密扩展库
rpm -ivh libmcrypt-2.5.8-9.el6.x86_64.rpm
rpm -xxxxxx.rpm
rpm -xxxxxx.rpm
rpm -xxxxxx.rpm
编译过程中需要用到www程序用户,所以需要创建一个
useradd -s /sbin/nologin -M www
解压并编译安装PHP
软件包上文有
tar xf php-5.3.28.tar.gz -C /usr/src/
cd /usr/src/php-5.3.28/
./configure --prefix=/usr/local/php5.3.28 --with-mysql=mysqlnd --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 --with-fpm-group=www --enable-ftp
最后的正确输出提示为:Thank you for using PHP.
编译的详解看上文
编译PHP并安装
make && make install
make install最后的正确提示
Build complete.
Don't forget to run 'make test'.
设置软链接
ln -s /usr/local/php5.3.28/ /usr/local/php
PHP配置默认模版文件
生产环境:php.ini-development 开发环境:php.ini-production
开发环境更多的是开启日志,调试信息,而生产环境都是关闭状态
cd /usr/src/php-5.3.28
cp php.ini-production /usr/local/php/lib/php.ini
cd /usr/local/php/etc/
cp php-fpm.conf.default php-fpm.conf
关于php-fpm.conf,暂时可用默认的配置,先把服务搭好,以后再进行优化。
分离式部署特别提示
vim php-fpm.conf
listen = 192.168.200.71:9000 --->要监听本地IP端口,让远程访问(151行)
启动PHP服务,查询端口并确认
/usr/local/php/sbin/php-fpm
ss -antup | grep 9000
四、测试阶段
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 sl.yunjisuan.com;
root /www;
location / {
index index.htm index.html;
}
location ~ .*.(php|php5)?$ {
fastcgi_pass 192.168.200.71:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
Nginx网页目录默认填写在/www下所以需要创建一个www的目录并修改属主和属组
mkdir /www
chown -R www.www /www
在Nginx网页目录创建静态内容
cd /www
echo "html" > index.html
启动Nginx服务并在Windows网页上测试(需要做windows本地映射这里就不演示了)
/usr/local/nginx/sbin/nginx
在PHP网页目录创建动态内容(需要创建网页目录并让Nginx程序用户有权限访问)
mkdir /www
chown www.www /www
echo "PHP" > index.php
用动态网页当默认首页测试结果
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 sl.yunjisuan.com;
root /www;
location / {
index index.php;
}
location ~ .*.(php|php5)?$ {
fastcgi_pass 192.168.200.71:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
}
Nginx和PHP网页目录
cd /www
echo "html PHP" > index.php
修改完配置文件重启Nginx并Windows网页测试
/usr/local/nginx/sbin/nginx -s reload
从上图发现,Nginx默认还是去PHP动态去找网页内容,而不是默认跳转Nginx网页目录的内容
五、针对MySQL测试
先在MySQL数据库授权一个远程登陆账号
mysql -uroot -p666666
grant all on *.* to ‘yunjisuan’@’%’ identified by ‘666666’;
flush privileges;
六、到PHP里
创建test_mysql.php
在PHP服务网页目录创建MySQL登陆页面
cd /www
vim test_mysql.php
<?php
$link_id=mysql_connect('192.168.200.72','sunlin','666666');
if($link_id){
echo "mysql successful by Mr.sl ! ";
}else{
echo mysql_error();
}
?>
Windows输入域名并测试
ka.yunjisuan.com/test_mysql.php