当前位置 博文首页 > 青岛Linux运维:Linux基础学习-LNMP环境搭建

    青岛Linux运维:Linux基础学习-LNMP环境搭建

    作者:[db:作者] 时间:2021-09-15 22:22

    LNMP环境搭建

    LNMP动态网站部署架构是一套由Linux+Nginx+Mysql+PHP组件的动态网站系统解决方案,具有免费、高效、扩展性强且资源消耗低等特性.

    LNMP动态网站架构

    yum install -y apr* autoconf automake bison bzip2 bzip2* compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gd gettext gettext-devel glibc kernel kernel-headers keyutils keyutils-libs-devel krb5-devel libcom_err-devel libpng libpng-devel libjpeg* libsepol-devel libselinux-devel libstdc++-devel libtool* libgomp libxml2 libxml2-devel libXpm* libtiff libtiff* make mpfr ncurses* ntp openssl openssl-devel patch pcre-devel perl php-common php-gd policycoreutils telnet t1lib t1lib* nasm nasm* wget zlib-devel
    
     cd /usr/local/src
    
     wget https://www.linuxprobe.com/Software/cmake-2.8.11.2.tar.gz
     wget https://www.linuxprobe.com/Software/Discuz_X3.2_SC_GBK.zip
     wget https://www.linuxprobe.com/Software/freetype-2.5.3.tar.gz
     wget https://www.linuxprobe.com/Software/jpegsrc.v9a.tar.gz
     wget https://www.linuxprobe.com/Software/libgd-2.1.0.tar.gz
     wget https://www.linuxprobe.com/Software/libmcrypt-2.5.8.tar.gz
     wget https://www.linuxprobe.com/Software/libpng-1.6.12.tar.gz
     wget https://www.linuxprobe.com/Software/libvpx-v1.3.0.tar.bz2
     wget https://www.linuxprobe.com/Software/mysql-5.6.19.tar.gz
     wget https://www.linuxprobe.com/Software/nginx-1.6.0.tar.gz
     wget https://www.linuxprobe.com/Software/openssl-1.0.1h.tar.gz
     wget https://www.linuxprobe.com/Software/php-5.5.14.tar.gz
     wget https://www.linuxprobe.com/Software/pcre-8.35.tar.gz
     wget https://www.linuxprobe.com/Software/t1lib-5.1.2.tar.gz
     wget https://www.linuxprobe.com/Software/tiff-4.0.3.tar.gz
     wget https://www.linuxprobe.com/Software/yasm-1.2.0.tar.gz
     wget https://www.linuxprobe.com/Software/zlib-1.2.8.tar.gz
    
    [root@mail src]# ls
    cmake-2.8.11.2          libgd-2.1.0.tar.gz      nginx-1.6.0.tar.gz     tiff-4.0.3.tar.gz
    cmake-2.8.11.2.tar.gz   libmcrypt-2.5.8.tar.gz  openssl-1.0.1h.tar.gz  yasm-1.2.0.tar.gz
    Discuz_X3.2_SC_GBK.zip  libpng-1.6.12.tar.gz    pcre-8.35.tar.gz       zlib-1.2.8.tar.gz
    freetype-2.5.3.tar.gz   libvpx-v1.3.0.tar.bz2   php-5.5.14.tar.gz
    jpegsrc.v9a.tar.gz      mysql-5.6.19.tar.gz     t1lib-5.1.2.tar.gz
    
    [root@mail cmake-2.8.11.2]# tar -xzvf cmake-2.8.11.2.tar.gz 
    [root@mail cmake-2.8.11.2]# cd cmake-2.8.11.2/
    [root@mail cmake-2.8.11.2]# ./configure 
    [root@mail cmake-2.8.11.2]# make && make install
    [root@mail cmake-2.8.11.2]# echo $?
    0
    

    配置MySQL服务

    参数解释
    -DCMAKE_INSTALL_PREFIX参数用于定义数据库服务程序的保存目录
    -DMYSQL_DATADIR参数用于定义真实数据库文件的目录
    -DSYSCONFDIR参数定义MySQL数据库配置文件的保存目录
    –user参数指定MySQL服务的对应账号名称
    –basedir参数指定MySQL服务程序的保存目录
    –datadir参数指定MySQL真实数据库的文件保存目录
    [root@mail src]# tar -xzvf mysql-5.6.19.tar.gz 
    [root@mail src]# cd mysql-5.6.19/
    [root@mail mysql-5.6.19]# 
    [root@mail mysql-5.6.19]# useradd mysql -s /sbin/nologin -M 
    [root@mail mysql-5.6.19]# mkdir -p /usr/local/mysql/var
    [root@mail mysql-5.6.19]# chown -Rf mysql:mysql /usr/local/mysql
    
    [root@mail mysql-5.6.19]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc
    [root@mail mysql-5.6.19]# make && make install
    [root@mail mysql-5.6.19]# echo $?
    0
    
    [root@mail mysql-5.6.19]# rm -rf /etc/my.cnf
    
    [root@mail mysql-5.6.19]# cd /usr/local/mysql/
    [root@mail mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var
    
    [root@mail mysql]# ln -s my.cnf /etc/my.cnf
    [root@mail mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
    
    [root@mail mysql]# vim /etc/rc.d/init.d/mysqld
     46 basedir=/usr/local/mysql
     47 datadir=/usr/local/mysql/var
    
    [root@mail mysql]# service mysqld start
    Starting MySQL............ SUCCESS! 
    [root@mail mysql]# chkconfig mysqld on
    
    [root@mail mysql]# vim /etc/profile
     74 export PATH=$PATH:/usr/local/mysql/bin
    [root@mail mysql]# source /etc/profile
    
    [root@mail mysql]# mkdir /var/lib/mysql
    [root@mail mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
    [root@mail mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
    [root@mail mysql]# ln -s /usr/local/mysql/include/mysql
    /usr/include/mysql
    

    MySQL数据库初始化

    [root@mail mysql]# mysql_secure_installation 
    
    
    
    NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
          SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
    
    In order to log into MySQL to secure it, we'll need the current
    password for the root user.  If you've just installed MySQL, and
    you haven't set the root password yet, the password will be blank,
    so you should just press enter here.
    
    Enter current password for root (enter for none): //直接回车
    OK, successfully used password, moving on...
    
    Setting the root password ensures that nobody can log into the MySQL
    root user without the proper authorisation.
    
    Set root password? [Y/n] Y(要为root管理员设置数据库的密码)
    New password: 
    Re-enter new password: 
    Password updated successfully!
    Reloading privilege tables..
     ... Success!
    
    
    By default, a MySQL installation has an anonymous user, allowing anyone
    to log into MySQL without having to have a user account created for
    them.  This is intended only for testing, and to make the installation
    go a bit smoother.  You should remove them before moving into a
    production environment.
    
    Remove anonymous users? [Y/n] Y(删除匿名用户)
     ... Success!
    
    Normally, root should only be allowed to connect from 'localhost'.  This
    ensures that someone cannot guess at the root password from the network.
    
    Disallow root login remotely? [Y/n] Y(禁止root管理员从远程登录)
     ... Success!
    
    By default, MySQL comes with a database named 'test' that anyone can
    access.  This is also intended only for testing, and should be removed
    before moving into a production environment.
    
    Remove test database and access to it? [Y/n] Y(删除test数据库并取消对其的访问权限)
     - Dropping test database...
     ... Success!
     - Removing privileges on test database...
     ... Success!
    
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    
    Reload privilege tables now? [Y/n] Y(刷新授权表,让初始化后的设定立即生效)
     ... Success!
    
    
    
    
    All done!  If you've completed all of the above steps, your MySQL
    installation should now be secure.
    
    Thanks for using MySQL!
    
    
    Cleaning up...
    

    配置Nginx服务

    //用于提供Perl语言兼容的正则表达式库的软件包pcre
    
    [root@mail src]# tar -xzvf pcre-8.35.tar.gz 
    [root@mail src]# cd pcre-8.35/
    [root@mail pcre-8.35]# ./configure --prefix=/usr/local/pcre
    [root@mail pcre-8.35]# make && make install
    
    //openssl软件包用于提供网站加密证书服务的程序文件
    
    [root@mail src]# tar -xzvf openssl-1.0.1h.tar.gz 
    [root@mail nginx-1.6.0]# cd openssl-1.0.1h/
    [root@mail openssl-1.0.1h]# ./config --prefix=/usr/local/openssl
    [root@mail openssl-1.0.1h]# make && make install
    
    [root@mail openssl-1.0.1h]# vim /etc/profile
    export PATH=$PATH:/usr/local/mysql/bin:/usr/local/openssl/bin
    [root@mail openssl-1.0.1h]# source /etc/profile
    
    //zlib软件包用于提供压缩功能的函数库文件
    
    [root@mail src]# tar xzvf zlib-1.2.8.tar.gz 
    [root@mail src]# cd zlib-1.2.8/
    [root@mail zlib-1.2.8]# ./configure --prefix=/usr/local/zlib
    [root@mail zlib-1.2.8]# make && make install
    
    [root@mail src]# useradd www -s /sbin/nologin -M
    
    
    [root@mail src]# tar -xzvf nginx-1.6.0.tar.gz 
    [root@mail src]# cd nginx-1.6.0/
    [root@mail nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.1h --with-zlib=/usr/local/src/zlib-1.2.8 --with-pcre=/usr/local/src/pcre-8.35
    [root@mail nginx-1.6.0]# make && make install
    
    [root@mail nginx-1.6.0]# vim /etc/rc.d/init.d/nginx
    

    nginx启动脚本文件

    #!/bin/bash
    # nginx - this script starts and stops the nginx daemon
    # chkconfig: - 85 15
    # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
    # proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config: /etc/nginx/nginx.conf
    # config: /usr/local/nginx/conf/nginx.conf
    # pidfile: /usr/local/nginx/logs/nginx.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
    nginx="/usr/local/nginx/sbin/nginx"
    prog=$(basename $nginx)
    NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
    [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
    lockfile=/var/lock/subsys/nginx
    make_dirs() {
    # make required directories
    user=`$nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
            if [ -z "`grep $user /etc/passwd`" ]; then
                    useradd -M -s /bin/nologin $user
            fi
    options=`$nginx -V 2>&1 | grep 'configure arguments:'`
    for opt in $options; do
            if [ `echo $opt | grep '.*-temp-path'` ]; then
                    value=`echo $opt | cut -d "=" -f 2`
                    if [ ! -d "$value" ]; then
                            # echo "creating" $value
                            mkdir -p $value && chown -R $user $value
                    fi
            fi
    done
    }
    start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_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() {
    #configtest || return $?
    stop
    sleep 1
    start
    }
    reload() {
    #configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
    }
    force_reload() {
    restart
    }
    configtest() {
    $nginx -t -c $NGINX_CONF_FILE
    }
    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|configtest}"
    exit 2
    esac