• LNMP分离式部署


    ---恢复内容开始---

    提示:需要开三台虚拟机(Nginx,PHP,MySQL)

    LNMP:即(Linux,Nginx,MySQL,PHP)

    LNMP搭建步骤:

    1,搭建MySQL
    2,搭建Nginx
    3,搭建PHP
    4,分发以上的配置文件
    5,启动以上的配置文件

    一:安装MySQL

    (1)创建MySQL用户的账号

    [root@MySQL ~]# groupadd mysql
    [root@MySQL ~]# useradd -s /sbin/nologin -g mysql -M mysql
    [root@MySQL ~]# tail -1 /etc/passwd
    mysql:x:500:501::/home/mysql:/sbin/nologin
    [root@MySQL ~]# id mysql
    uid=500(mysql) gid=501(mysql) groups=501(mysql)

    (2)获取MySQL二进制软件包

    百度网盘:http://pan.baidu.com/s/1hrBCzsC

    提取码:4yjf

    (3)采用二进制安装MySQL(仅需解压即可,省事,方便)

    [root@MySQL ~]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz -C /usr/local/
    [root@MySQL ~]# cd /usr/local/
    [root@MySQL  local]# mv mysql-5.5.32-linux2.6-x86_64 mysql-5.5.32
    [root@MySQL  local]# ln -s mysql-5.5.32 mysql
    [root@MySQL  local]# ls
    bin etc games include lib lib64 libexec mysql mysql-5.5.32 sbin share src
    [root@MySQL  local]# cd /usr/local/mysql
    [root@MySQL  mysql]# ls
    bin data include lib mysql-test scripts sql-bench
    COPYING docs INSTALL-BINARY man README share support-files
    

    (4)初始化MySQL配置文件my.cnf

    [root@MySQL 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@MySQL mysql]# /bin/cp support-files/my-small.cnf /etc/my.cnf
    

    提示:

    - support-files下有my.cnf的各种配置样例。 
    - 使用cp全路径/bin/cp,可实现拷贝而不出现替换提示,即如果有重名文件会直接覆盖 
    - 本例为测试安装环境,因此选择参数配置小的my-small.cnf配置模版,如果是生产环境可以根据硬件选择更高级的配置文件,上述配置文件模版对硬件的要求从低到高依次为:
    my-medium.cnf (最低)
    my-small.cnf
    my-large.cnf
    my-huge.cnf
    my-innodb-heavy-4G.cnf(最高)
    

    (5)初始化MySQL文件

    [root@MySQL mysql]# mkdir -p /usr/local/mysql/data 
    [root@MySQL mysql]# chown -R mysql.mysql /usr/local/mysql
    [root@MySQL mysql]# yum -y install libaio 
    [root@MySQL]# /usr/local/mysql/scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
    #初始化MySQL数据库文件,会有很多信息提示,如果没有ERROR级别的错误,会有两个OK的字样,表示初始化成功,否则就要解决初始化的问题
    

    (6)配置并启动MySQL

     [root@mysql mysql]# cp support-files/mysql.server /etc/init.d/mysqld
    #拷贝MySQL启动脚本到MySQL的命令路径
    [root@mysql mysql]# chmod +x /etc/init.d/mysqld
    #使脚本可执行
    
    [root@mysql mysql]# /etc/init.d/mysqld start
    Starting MySQL... SUCCESS!     #启动MySQL       
    [root@mysql mysql]# netstat -antup | grep mysql     #过滤其端口
    tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1347/mysqld 
    如果发现3306端口没起来,请tail -100 /usr/local/mysql/data/主机名.err查看日志信息,看是否有报错信息,然后根据相关错误提示进行调试。经常查看服务运行日志是个很好的习惯,也是高手的习惯。
    
    [root@mysql mysql]# chkconfig mysqld on      #设置MySQL开机自启动
    [root@mysql mysql]# chkconfig --list mysqld
    mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
    
    [root@mysql mysql]# ln -s /usr/local/mysql/bin/* /usr/local/bin/     #配置mysql的全局使用路径
    [root@mysql mysql]# which mysqladmin
    /usr/local/bin/mysqladmin
    

    二:安装Nginx 

    (1)nginx的编译安装部署

    [root@Nginx ~]# mount /dev/sr0 /media/cdrom/
    mount: block device /dev/sr0 is write-protected, mounting read-only
    [root@Nginx ~]# yum install -y pcre-devel openssl-devel
    #wget -q http://nginx.org/download/nginx-1.10.2.tar.gz
    [root@Nginx ~]# useradd -s /sbin/nologin -M www #创建程序用户
    [root@Nginx ~]# tar xf nginx-1.10.2.tar.gz -C /usr/src/ #解压缩
    [root@Nginx ~]# cd /usr/src/nginx-1.10.2
    [root@Nginx nginx-1.10.2]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #预配置
    [root@Nginx nginx-1.10.2]# make && make install #编译和安装
    [root@Nginx nginx-1.10.2]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ #给命令做软连接,以便PATH能找到
    [root@Nginx nginx-1.10.2]# /usr/local/nginx/sbin/nginx #启动Nginx
    

    (2)修改Nginx配置文件 

    [root@Nginx conf]# cat nginx.conf.default | egrep -v "#|^$" > nginx.conf
    [root@Nginx conf]# vim nginx.conf
    [root@Nginx conf]# cat nginx.conf
    woeker_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.html  index.htm;
    	}
        }
    }
    
    # 检查配置文件
    [root@Nginx conf]# /usr/local/nginx/sbin/nginx -t
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    
    #加入映射文件
    [root@Nginx conf]# echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts
    [root@Nginx conf]# cat /etc/hosts
    127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
    192.168.100.129 www.yunjisuan.com

    (3)添加网页及测试

    [root@Nginx conf]# mkdir /www
    [root@Nginx conf]# echo "`hostname -I` www.yunjisuan.com" >> /www/index.html
    [root@Nginx conf]# chown -R www.www /www/ #PHP的程序用户也要弄成www
    [root@Nginx conf]# curl www.yunjisuan.com
    192.168.100.129 www.yunjisuan.com
    

    三:安装PHP

    (1)安装PHP所需的lib库

    [root@PHP ~]# yum -y install zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel
    [root@PHP ~]# yum -y install freetype-devel libpng-devel gd libcurl-devel libxslt-devel
    

    (2)安装yum无法安装的libiconv库

    [root@PHP ~]# yum -y install wget
    [root@PHP ~]# wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
    [root@PHP ~]# ls
    anaconda-ks.cfg install.log install.log.syslog libiconv-1.14.tar.gz
    [root@PHP ~]# tar xf libiconv-1.14.tar.gz -C /usr/src/
    [root@PHP ~]# cd /usr/src/libiconv-1.14/
    [root@PHP libiconv-1.14]# ./configure --prefix=/usr/local/libiconv && make && make install
    

    (3)云yum安装其它几个库

    [root@PHP libiconv-1.14]# yum -y install libmcrypt-devel
    [root@PHP ~]# yum -y install mhash
    [root@PHP ~]# yum -y install mcrypt
      wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo #云yum
    

    (4)获取PHP软件包

    [root@PHP ~]# wget http://cn2.php.net/get/php-5.3.28.tar.gz/from/this/mirror
    

    (5)解压配置及编译安装

    先解压配置:

    [root@PHP ~]# useradd -s /sbin/nologin -M www
    [root@PHP ~]# id www
    uid=500(www) gid=500(www) groups=500(www)
    
    [root@PHP ~]# tar xf php-5.3.28.tar.gz -C /usr/src/
    [root@PHP ~]# cd /usr/src/php-5.3.28/
    [root@PHP 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
    

    yum -y install xxx -devel是安装前面有关xxx的所有包

    配置后,一定要echo $?进行确认,以免影响进程配置成功后会冒出下图:

     然后进行编译安装:

    [root@PHP php-5.3.28]# make && make install  #分离式直接进行编译安装
    

    (6)配置PHP的两个文件

    引擎配置文件php.ini

    [root@PHP php-5.3.28]# ln -s /usr/local/php5.3.28/ /usr/local/php  #设置软连接,以便访问
    [root@PHP php-5.3.28]# ls php.ini*  #查看PHP配置的默认模板文件,绝对路径:/usr/src/php-5.3.28/
    php.ini-development php.ini-production
    [root@PHP php-5.3.28]# cp php.ini-production /usr/local/php/lib/php.ini  #拷贝并改名
    [root@PHP php-5.3.28]# ls -l /usr/local/php/lib/php.ini
    -rw-r--r--. 1 root root 69627 Nov 25 23:32 /usr/local/php/lib/php.ini
     
    

    php-fpm.conf

    [root@PHP php-5.3.28]# cd /usr/local/php/etc/
    [root@PHP etc]# ls
    pear.conf php-fpm.conf.default
    [root@PHP etc]# cp php-fpm.conf.default php-fpm.conf
    #然后vim进去,修改第151行:监听本地IP(listen = 192.168.100.129:9000)
    

    (7)启动PHP服务并检查

    [root@PHP etc]# /usr/local/php/sbin/php-fpm  #启动PHP服务
    [root@PHP etc]# ps -ef | grep php-fpm   #检查PHP的进程
    root 30699 1 0 23:42 ? 00:00:00 php-fpm: master process (/usr/local/php5.3.28/etc/php-fpm.conf)
    www 30700 30699 0 23:42 ? 00:00:00 php-fpm: pool www 
    www 30701 30699 0 23:42 ? 00:00:00 php-fpm: pool www 
    root 30716 1046 0 23:43 pts/0 00:00:00 grep php-fpm
    [root@PHP etc]# lsof -i:9000   #检查其端口
    COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    php-fpm 30699 root 7u IPv4 146919 0t0 TCP 192.168.200.30:cslistener (LISTEN)
    php-fpm 30700 www 0u IPv4 146919 0t0 TCP 192.168.200.30:cslistener (LISTEN)
    php-fpm 30701 www 0u IPv4 146919 0t0 TCP 192.168.200.30:cslistener (LISTEN)
     
    

    四:配置Nginx支持PHP程序的请求访问

    location ~ .*.(php|php5)?$ {
     fastcgi_pass IP:9000;
     fastcgi_index index.php;
     include fastcgi.conf;
    Nginx的配置文件中加一个location 如上。
    
    [root@Nginx conf]# /usr/local/sbin/nginx -s reload  #重启Nginx服务
    

    测试静态:

    在windows做映射 192.168.100.129 www.yunjisuan.com
    然后在IE浏览器中测试一下静态网页:www.yunjisuan.com

    测试动态:

    在PHP下创建mkdir /www 然后echo "xxx" >> index.php

    Nginx中必须跟PHP中(index.php)一样,虽然PHP可以推过来,可以测试成功,但Nginx中不能没有,本地没有会被权限拒绝掉的(空的也行)

    五:加入数据库(MySQL)

    [root@MySQL mysql]# mysql -uroot -p123123
    Welcome to the MySQL monitor. Commands end with ; or g.
    Your MySQL connection id is 5
    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)
    

    六:创建网页(PHP,Nginx)

    [root@PHP www]# cat test_mysql.php 
    <?php
    //$link_id=mysql_connect('主机名','用户','密码');
    $link_id=mysql_connect('192.168.100.129','yunjisuan','123123');
    if($link_id){
    echo "mysql successful";
    }else{
    echo mysql_error();
    }
    ?>
     
    

    最后可以在IE浏览器中测试:

    www.yunjisuan.com/test_mysql.php
    

      

    ---恢复内容结束---

  • 相关阅读:
    ANSI C 与 C99的不同
    字符串中含有空格的注意事项
    巧用printf函数
    求数列的和
    数值统计
    平方和与立方和
    求奇数的乘积
    第几天?
    细节之重
    用%*c滤掉回车,ASCII码排序
  • 原文地址:https://www.cnblogs.com/hj0307/p/10026917.html
Copyright © 2020-2023  润新知