admin

LNMP编译安装部署文档

admin WEB运维 2018-10-29 1586浏览 0

 

最近帮客户写一个文档,客户要求照着文档敲能够完成整个lnmp部署,特记录下来!


系统环境
CentOS Linux release 7.4.1708
 
软件环境
软件包位置(/opt)
nginx/1.12.2
 
PHP 7.1.1
 
Mysql5.6
Redis-4.0.9
 
phpMyAdmin-4.7.0
 
 
memcached-1.5.7

  

构建Nginx+MySQL+PHP平台

 

(1)安装lnmp所需依赖库
#yum -y install gcc automake autoconf libtool make gcc-c++ glibc libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5-devel libidn libidn-devel openssl openssl-devel pcre pcre-devel libmcrypt libmcrypt-devel cmake gmp-devel readline-devel

 

创建www所属组及用户
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

  

设置系统资源限制
ulimit -SHn 65535
(2)安装配置Nginx
Nginx 源码安装
#cd /opt/
wget http://nginx.org/download/nginx-1.12.2.tar.gz
#tar -xvf nginx-1.12.2.tar.gz
#cd nginx-1.12.2
#./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --user=www --group=www --with-http_ssl_module --with-http_gzip_static_module --with-http_stub_status_module
#make && make install


 

nginx //启动
nginx -s stop// 停止
nginx -s reload // 重新加载

 

为方便管理设置启动脚本,配置开机启动

 

 vi /etc/init.d/nginx

#! /bin/bash
    # chkconfig: 35 85 15 
    # description: Nginx is an HTTP(S) server, HTTP(S) reverse
    set -e
    PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
    DESC="nginx daemon"
    NAME=nginx
    DAEMON=/usr/sbin/$NAME
    SCRIPTNAME=/etc/init.d/$NAME
    test -x $DAEMON || exit 0
    d_start(){
        $DAEMON || echo -n " already running"
    }
    d_stop() {
        $DAEMON -s quit || echo -n " not running"
    }
    d_reload() {
        $DAEMON -s reload || echo -n " counld not reload"
    }
    case "$1" in
    start)
        echo -n "Starting $DESC:$NAME"
        d_start
        echo "."
    ;;
    stop)
        echo -n "Stopping $DESC:$NAME"
        d_stop
        echo "."
    ;;
    reload)
        echo -n "Reloading $DESC configuration..."
        d_reload
        echo "reloaded."
    ;;
    restart)
        echo -n "Restarting $DESC: $NAME"
        d_stop
        sleep 2
        d_start
        echo "."
    ;;
    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|reload}" >&2
        exit 3
    ;;
    esac
    exit 0

 

#chmod +x /etc/rc.d/init.d/nginx (设置可执行权限)
#chkconfig --add nginx (添加系统服务)
#service nginx start
#service nginx stop
#service nginx restart
#service nginx reload

 

浏览器访问:http://47.98.97.98/如能出现nginx页面则表示成功
常用查询
查看nginx进程
 ps -ef | grep nginx
 查看进程个数 去掉首位的
 ps -ef | grep nginx | wc -l
 查看80端口
 netstat -tlupn|grep nginx

    

(3)安装配置php

#cd /opt/
#wget http://am1.php.net/distributions/php-7.1.1.tar.gz
#tar zvxf php-7.1.1.tar.gz
#export LD_LIBRARY_PATH=/lib/:/usr/lib/:/usr/local/lib
#cd php-7.1.1
./configure \
--prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-fpm \
--with-fpm-user=nginx  \
--with-fpm-group=nginx \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared  \
--enable-soap \
--with-libxml-dir \
--with-xmlrpc \
--with-openssl \
--with-mcrypt \
--with-mhash \
--with-pcre-regex \
--with-sqlite3 \
--with-zlib \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--with-cdb \
--enable-dom \
--enable-exif \
--enable-fileinfo \
--enable-filter \
--with-pcre-dir \
--enable-ftp \
--with-gd \
--with-openssl-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir  \
--with-freetype-dir \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--with-gettext \
--with-gmp \
--with-mhash \
--enable-json \
--enable-mbstring \
--enable-mbregex \
--enable-mbregex-backtrack \
--with-libmbfl \
--with-onig \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-zlib-dir \
--with-pdo-sqlite \
--with-readline \
--enable-session \
--enable-shmop \
--enable-simplexml \
--enable-sockets  \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--with-libxml-dir \
--with-xsl \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-opcache

 

cp php.ini-development /usr/local/php/lib/php.ini
另外还需要设置环境变量 :
修改/etc/profile文件使其永久性生效,并对所有系统用户生效,在文件末尾加上如下两行代码
PATH=$PATH:/usr/local/php/bin
export PATH
然后执行 命令 source /etc/profile
php -v 就可以看到PHP版本信息了。
此时还需要配置PHP-fpm:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp /opt/php-7.1.1/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

 

启动php-fpm:
/etc/init.d/php-fpm start

  

如果出现错误:ERROR: [pool www] cannot get uid for user 'nginx'
则新建www-data 用户组:
useradd nginx
然后再启动php-fpm

 

php-fpm参数:
--start 启动
--stop 强制终止
--quit 平滑终止
--restart 重启
--reload 重新平滑加载php的php.ini
--logrotate 重新启用log文件

 

查看php版本
#php -v

  

(4)配置nginx支持php解析

 

在nginx的配置文件中添加

vi /usr/local/nginx/conf/nginx.conf

 

location ~ \.php$ {
                root           /usr/local/nginx/html/;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include /usr/local/nginx/conf/fastcgi_params;
    }

注释:/usr/local/nginx/html/    nginx的web目录

      /usr/local/nginx/conf/fastcgi_params   fastcgi处理文件

   

重启nginx
service nginx restart
启动php解析管理器fastcgi
service php-fpm start

  

编写测试页面
  <?php
        phpinfo();
   ?>

 

测试:
http://47.98.97.98/index.php

  

(5)nginx配置虚拟主机(参考)

 

web.test.com
server {
 listen 80;
 server_name web.test.com;
  location ~ \.php$ {
 root  /usr/local/nginx/html/web.test.com;
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
 include /usr/local/nginx/conf/fastcgi_params
 }
 location / {
 root  /usr/local/nginx/html/web.test.com;
 index  index.php;
 }
}

 

(6)MySQL5.6安装配置

 

#cd /opt
#yum -y install libaio libaio-devel
#wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
#groupadd mysql
#useradd -r -g mysql -s /bin/false mysql
#tar -xvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
# cd mysql-5.6.35-linux-glibc2.5-x86_64
#mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
#chown -R mysql:mysql /usr/local/mysql
#rm -rf /etc/my.cnf
#cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
修改文件为
[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
pid-file=/usr/local/mysql/mysql.pid
log-error=/usr/local/mysql/data/mysql.err
max_connections=200
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=20M
 初始化数据库
#cd /usr/local/mysql/scripts
# ./mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
 启动数据库
# service mysqld start    
 添加环境变量
#echo 'export PATH=/usr/local/mysql/bin:$PATH' >> /etc/profile
#source /etc/profile
  #原数据库面位为空 修改数据库密码为admin123@
mysqladmin -uroot password admin123@


 

(7)Redis安装配置

#cd /opt/
#wget 
http://download.redis.io/releases/redis-4.0.9.tar.gz#tar -xvf redis-4.0.9.tar.gz
#cd redis-4.0.9
#make
#make install
#cp redis.conf /etc/
配置文件修改
vi /etc/redis.conf
Daemonize yes
pidfile /var/run/redis.pid
编写启动脚本
vi /etc/init.d/redis
#!/bin/sh
#
# redis - this script starts and stops the redis-server daemon
#
# chkconfig:   - 85 15
# description:  Redis is a persistent key-value database
# processname: redis-server
# config:      /etc/redis.conf
# config:      /etc/sysconfig/redis
# pidfile:     /var/run/redis.pid
 # Source function library.
. /etc/rc.d/init.d/functions
 # Source networking configuration.
. /etc/sysconfig/network
 # Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
 redis="/usr/local/bin/redis-server"
prog=$(basename $redis)
 REDIS_CONF_FILE="/etc/redis.conf"
 [ -f /etc/sysconfig/redis ] && . /etc/sysconfig/redis
 lockfile=/var/lock/subsys/redis
 start() {
    [ -x $redis ] || exit 5
    [ -f $REDIS_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $redis $REDIS_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 restart() {
    stop
    start
}
 reload() {
    echo -n $"Reloading $prog: "
    killproc $redis -HUP
    RETVAL=$?
    echo
}
 force_reload() {
    restart
}
 rh_status() {
    status $prog
}
 rh_status_q() {
    rh_status >/dev/null 2>&1
}
 case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac

 

配置开机启动脚本
#chmod +x /etc/init.d/redis
#chkconfig --add redis
#chkconfig --level 35 redis on
#启动服务
service redis restart
测试

 

(8)Memcached安装配置

 

 #yum install gcc libevent libevent-devel
#wget #tar -xvf memcached-1.5.7.tar.gz
#cd memcached-1.5.7
#/configure --prefix=/usr/local/memcached && make && sudo make install 
启动服务
/usr/local/memcached/bin/memcached -d -u root -l 172.16.177.69 -p 11211 -m 10 -c 1000 -P /tmp/memcached.pid

 

启动参数详解
#选项说明,这里只列出比较重要的选项,具体选项说明使用memcached -h来查阅
-p   TCP端口,默认为11211,可以不设置
-U   UDP端口,默认为11211,0为关闭
-l   监听的ip地址
-d   守护进程(daemon)
-u   指定用户,如果当前为 root ,需要使用此参数指定用户
-m   最大内存,单位MB。默认64MB,32位操作系统,每个进程最多只能使用2GB,64位无限制
-M   禁止LRU策略,内存耗尽时返回错误,而不是删除数据
-c   最大连接数,默认是1024
-vv  查看日志
-P   memcache的pid文件,结束memcache进程:kill `cat /tmp/memcached_32054.pid`
-f   增长因子,默认1.25
-n   初始chunk=key+suffix+value+32结构体,默认48字节
-L   启用大内存页,可以降低内存浪费,改进性能
-t   线程数,默认4。由于memcached采用NIO,所以更多线程没有太多作用
-R   每个event连接最大并发数,默认20
-C   禁用CAS命令(可以禁止版本计数,减少开销)
-I   每次申请内存的页的大小(page),默认1M,最小1k,最大128M
-F   禁用flush_all

 

(9)phpadmin安装配置

 

#wget https://files.phpmyadmin.net/phpMyAdmin/4.7.0/phpMyAdmin-4.7.0-all-languages.tar.gz 
#tar -xvf phpMyAdmin-4.7.0-all-languages.tar.gz
#mv phpMyAdmin-4.7.0-all-languages /usr/local/nginx/html/phpMyAdmin
测试界面
http://47.98.97.98/phpMyAdmin/

 

 

版权声明

本站《作品展示》类文章均为原创,转载必须注明出处,技术分享类文章部分来源于网络,版权归原作者所有,若侵权请留言。

发表评论