## N26 - Sixteenth Blog Assignment

Keywords: PHP Nginx MySQL MariaDB

Sixteenth Blog Assignment

1. Source code compilation and installation of LNMP architecture environment;

  • Test environment:
    • nginx host IP: 192.168.23.200
    • php-fpm host IP: 192.168.23.201
    • mysql host IP: 192.168.23.202
  • Test procedure

    • (1) Install nginx on the nginx host by source code compilation (install pcre-devel and openssl-devel first)
      • 1: Compilation parameters are:
        • ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_gzip_static_module --with-debug --with-http_stub_status_module
      • 2: Add the compiled and installed binary program of nginx to the environment variable
        • /etc/profile.d/nginx.sh
        • Add: export PATH=/usr/local/nginx/sbin/:$PATH
        • . /etc/profile.d/nginx.sh
      • 3: Edit the master configuration file / etc/nginx/nginx.conf
        • Virtual machines can be added to the main configuration file
        • location ~ \.php$ {
          fastcgi_pass 192.168.23.201:9000;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME /apps/php/$fastcgi_script_name;
          include fastcgi_params;
          }
    • (2) Compile and install php-fpm on the host of php-fpm (libxml2-devel libmcrypt-devel bzip2-devel curl-devel openssl-devel pcre-devel needs to be installed)

      • 1: Compilation parameters are:
        • ./configure --prefix=/usr/local/php5.4 --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --with-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-bz2 --with-curl --with-fpm-user=nginx --with-fpm-group=nginx
      • 2: Copy php.ini engine to PHP installation package

        • ln -vs /usr/local/php5.4 /usr/local/php
        • Vi/etc/profile.d/php.sh Add export PATH=/usr/local/php5.4/sbin:$PATH
        • . /etc/profile.d/php.sh
        • cp php.ini-production /usr/local/php/etc/php.ini
        • cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
      • 3: Edit php-fpm.conf

        • vi /usr/local/php/etc/php-fpm.conf
        • Modify listen = 192.168.23.201:9000 to set php-fpm process monitor on 192.168.23.201
        • Note; listen.allowed_clients = 127.0.0.1, allowing any host to connect php via fastcgi
        • Cancel the comment run/php-fpm.pid so that php-fpm has a fixed PID
      • 4: Create a new php site directory on the local file system
        • mkdir -pv /apps/php
        • vi index.php
        • Add <? PHP phpinfo ();?>
      • 5: For restarting and stopping php-fpm services
        • Signal mode
          • Stop: kill-INT cat/usr/local/php/var/run/php-fpm.pid
          • Restart: kill-USR2 cat/usr/local/php/var/run/php-fpm.pid
        • Service scripting pattern (pid number must be ensured in / usr/local/php/var/run/php-fpm.pid first)
          • CP. /sapi/fpm/init.d.php-fpm/etc/init.d/php-fpm is executed in the directory of the source package and is granted executable permissions: chmod+x/etc/init.d/php-fpm
          • /etc/init.d/php-fpm {start | stop | restart}
    • (3) Compile and install on mysql host
      • 1: Prepare data catalogue
        • Take the / mydata/data directory as an example
        • Chown-R mysql.mysql/mydata/data for MySQL to be able to write data to directories
      • 2: Install and configure mariadb
        • groupadd mysql
        • useradd -g mysql mysql
        • tar xf mariaDB-version -C /usr/local
        • cd /usr/local
        • ln -sv mariaDB-version mysql
        • cd /usr/local/mysql
        • chown -R root:mysql ./*
        • cp support-files/my-large.cnf /etc/my.cnf
        • vi /etc/mysql/my.cnf
        • Add three options dataDir =/mydata/data innodb_file_per_table = ON skip_name_resolve = ON
        • scripts/mysql_install_db --user=mysql --datadir=/mydata/data
        • cp support-files/mysql.server /etc/init.d/mysqld
        • vi /etc/ld.so.conf.d/mysql.conf
        • Add / usr/local/msyql/lib
        • ldconfig
        • ln -vs /usr/local/mysql/include /usr/include/
      • 3: Note: For compiling and installing mariadb, you need to base edir=/usr/local/mysql datadir=/my data/data in / etc/my.cnf and / etc/init.d/mysql D
      • 4: Run / usr/local/mysql/bin/mysql_secure_installation to set up root to log in remotely
      • 5: Log in to the database, authorize root to log in remotely
        • grant all on *.* to root@'192.168.%.%' identified by '~~~~~'
    • Summary: Through testing, installing nginx, php-fpm and mariadb on different hosts can be realized, and PHP programs can be uploaded and connected to databases in the / apps/php directory of php-fpm. But you just can't load CSS stylesheets.

2. Write a script to accomplish the following functions:
(A) One key to build LNMP source code compilation environment;
(2) You can customize the installation directory and other options by following the script with some parameters.

  • (1) Test environment: nginx, php-fpm, mariadb are on the same host, host IP is 192.168.23.204
#!/bin/bash
###################################    One button installation LNMP Environmental Science     #################################### 
################    install "Development Tools" "Server Platform Development"    #################      
################       Since these two package groups were selected when installing the system, they are no longer installed here.       ###################

echo -e "\033[37m Development Tools and Server Platform Development groups finished!!! \033[0m"

################                install Nginx , php-fpm Dependent packages                ################## 
installNginxPHPMariadbDependencyPackages (){

    yum install -y pcre-devel openssl-devel libxml2-devel libmcrypt-devel bzip2-devel curl-devel openssl-devel pcre-devel &> /dev/null 
    status=$?
    commandCheck $status " php-fpm Mariadb Dependency Packages" 

}

# Check whether the previous command was successfully executed
commandCheck (){

    if [ $1 -eq 0 ];then
        echo -e "\033[37m Nginx $2 success! \033[0m"
    else
        echo -e "\033[37m Nginx $2 failed~ \033[0m"
        exit 1
    fi

}

editMainConfigurationFile (){
    cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf_backup
    mkdir /apps/php -vp &> /dev/null 
    sed -i  '/#/d' /etc/nginx/nginx.conf
    [ ! -f "/root/php.txt" ] &&  echo -e "\tlocation ~ \.php$ {\n\tfastcgi_pass   127.0.0.1:9000;\n\tfastcgi_index  index.php;\n\tfastcgi_param  SCRIPT_FILENAME  /apps/php/\$fastcgi_script_name;\n\tinclude        fastcgi_params;}" > /root/php.txt
    sed -i '33r /root/php.txt' /etc/nginx/nginx.conf
    echo -e "\t  root   /apps/php/;" > /root/document.txt
    sed -i '30d' /etc/nginx/nginx.conf
    sed -i '29r /root/document.txt' /etc/nginx/nginx.conf
    sed -i '31d' /etc/nginx/nginx.conf
    echo -e "\t  index  index.php index.html index.htm;" > /root/php1.txt
    sed -i '30r /root/php1.txt' /etc/nginx/nginx.conf


    if nginx -t > /dev/null;then
        echo -e "\033[37m nginx configuration success!  \033[0m"
        nginx && echo -e "\033[37m Nginx start success" || echo "Nginx start failed~ \033[0m"
    else
        echo -e "\033[37m nginx configuration failed~ \033[0m"

    fi
}

configurationSet (){

    ./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --user=nginx --group=nginx --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --with-http_ssl_module --with-http_gzip_static_module --with-debug --with-http_stub_status_module &> /dev/null

}

nginxCompilingInstall (){

    # Install the pcre-devel and openssl-devel packages and check for success
    #yum install -y pcre-devel openssl-devel &> /dev/null 
    #status=$?
    #commandCheck $status "Dependency Package" 

    #Under the / root directory, download the ngxin-1.8.1 source package and decompress it
    cd && wget http://nginx.org/download/nginx-1.8.1.tar.gz && tar xf nginx-1.8.1.tar.gz &> /dev/null  
    cd nginx-1.8.1 

    #In the source directory of Nginx, Nginx is compiled and installed, and the compiled parameters are passed through parameters.
    ########### Note that no parameters are given here. $1 ###########
    useradd -r -s /sbin/nologin nginx
    # Because you cannot quote when specifying compilation parameters, use the form of function calls
    configurationSet
    status=$?
    commandCheck $status "configuration"

    make -j 4 &> /dev/null 
    status=$?
    commandCheck $status "create makefile"

    make install &> /dev/null 
    status=$?
    commandCheck $status "install"

    # Add binary program paths for compilation and installation of nginx to environment variables
    echo  "export PATH=/usr/local/nginx/sbin/:$PATH" >> /etc/profile.d/nginx.sh && . /etc/profile.d/nginx.sh


}

createHtmlCheckPage (){

    echo "this is the dynamical install nginx page!!!" > /apps/php/index.html
    echo -e "\033[37m Nginx install success!! \033[0m"

}

# Install and configure Nginx services
installNginx (){

    # Compile and install Nginx
    nginxCompilingInstall

    # Edit the master configuration file / etc/nginx/nginx.conf
    editMainConfigurationFile 

    # Create html test page
    createHtmlCheckPage

}

phpConfigurationSet (){

    ./configure --prefix=/usr/local/php5.4 --with-mysql=mysqlnd --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --enable-mbstring --with-png-dir --with-jpeg-dir --with-freetype-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-bz2 --with-curl --with-fpm-user=nginx --with-fpm-group=nginx &> /dev/null

}

phpCompilingInstall (){
    cd && wget http://cn.php.net/distributions/php-5.5.38.tar.bz2 && tar xf php-5.5.38.tar.bz2
    cd php-5.5.38
    phpConfigurationSet
    status=$?
    commandCheck $status " PHP configuration "
    make -j 4 &> /dev/null 
    status=$?
    commandCheck $status " PHP create makefile "
    make install &> /dev/null 
    status=$?
    commandCheck $status " PHP install "
}

copyPhpEngine (){

    ln -vs /usr/local/php5.4 /usr/local/php &> /dev/null
    echo "export PATH=/usr/local/php5.4/sbin:$PATH" >> /etc/profile.d/php.sh && . /etc/profile.d/php.sh
    cp php.ini-production /usr/local/php/etc/php.ini
    cp /usr/local/php/etc/php-fpm.conf.default  /usr/local/php/etc/php-fpm.conf

}

editPhpMainConfigurationFile (){

    sed -i '25c pid = run/php-fpm.pid' /usr/local/php/etc/php-fpm.conf

}

createPHPCheckPage (){

    echo "<?php phpinfo(); ?>" > /apps/php/index.php
}

phpService (){

    cp ./sapi/fpm/init.d.php-fpm  /etc/init.d/php-fpm
    chmod +x /etc/init.d/php-fpm
    /etc/init.d/php-fpm start
    status=$?
    commandCheck $status " PHP start "
}

# Install and configure php-fpm services
installPHP (){

    # Compile and install PHP-fpm
    phpCompilingInstall

    # Copy php.ini engine to PHP installation package
    copyPhpEngine

    # Edit the master configuration file / usr/local/php/etc/php-fpm.conf
    editPhpMainConfigurationFile

    # Create PHP test page
    createPHPCheckPage

    # Set up to restart and stop php services
    phpService

}



createDataDirectory (){

    mkdir -pv /mydata/data && chown -R mysql.mysql /mydata/data &> /dev/null
    status=$?
    commandCheck $status " Mariadb Directory "


}

binaryCreateMariadb (){

    cd && wget https://mirrors.tuna.tsinghua.edu.cn/mariadb//mariadb-5.5.54/bintar-linux-x86_64/mariadb-5.5.54-linux-x86_64.tar.gz
    tar xf mariadb-5.5.54-linux-x86_64.tar.gz -C /usr/local
    cd /usr/local
    ln -sv mariadb-5.5.54-linux-x86_64 mysql
    cd /usr/local/mysql
    chown -R root:mysql ./*
    cp support-files/my-large.cnf /etc/my.cnf 
    echo -e "datadir = /mydata/data\ninnodb_file_per_table = ON\nskip_name_resolve = ON" > /root/mysql.txt
    sed -i '41r /root/mysql.txt' /etc/my.cnf
    scripts/mysql_install_db --user=mysql --datadir=/mydata/data
    cp support-files/mysql.server /etc/init.d/mysqld
    echo "/usr/local/msyql/lib" > /etc/ld.so.conf.d/mysql.conf
    ldconfig
    ln -vs /usr/local/mysql/include/* /usr/include/
    status=$?
    commandCheck $status " Mariadb install "
}

createMariadbCheckPage (){

    echo -e "<?php \n\$conn=mysql_connect('127.0.0.1','root','yhy3426356');\nif (\$conn)\necho "ok";\nelse\necho "failure";\nphpinfo(); ?>" > /apps/php/index.php
}

# Install and configure mariadb service
installMariadb (){

    # Create a data catalog
    createDataDirectory

    # Install and configure mariadb
    binaryCreateMariadb

    # Start mariadb
    service mysqld start && mysql -uroot -h127.0.0.1 -e "grant all on *.* to root@127.0.0.1 identified by 'yhy3426356';"


    # Create Mariadb Connection Test Page
    createMariadbCheckPage
    status=$?
    commandCheck $status " PHP Mariadb Install "
}

# (1) Installing dependency packages for LNMP environments
installNginxPHPMariadbDependencyPackages
sleep 5

# (2) Installation of Nginx
installNginx 
sleep 5

# (3) Installation of php-fpm
installPHP
sleep 5

# (4) Installation of Mariadb
installMariadb
sleep 5

# (5) Compile and install LNMP
echo -e "\033[37m  You Did A Great Job ! \033[0m"

Posted by twinedev on Wed, 10 Apr 2019 21:39:31 -0700