1. 程式人生 > >CentOS7編譯安裝LNMP

CentOS7編譯安裝LNMP

zend text ast www. listen with sting 完整 url

  • 安裝編譯工具和依賴包
    
    [root@localhost src]# yum install -y apr* autoconf automake bison bzip2 bzip2* cloog-ppl compat* cpp curl curl-devel fontconfig fontconfig-devel freetype freetype* freetype-devel gcc gcc-c++ gtk+-devel 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* libxml* libXaw-devel libXmu-devel 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
  • 
    * 安裝MySQL
    1、安裝cmake
    

    [root@localhost src]# tar zxvf cmake-3.7.2.tar.gz
    [root@localhost src]# cd cmake-3.7.2
    [root@localhost cmake-3.7.2]# ./configure
    [root@localhost cmake-3.7.2]# make && make install

    
    2、安裝MySQL

    [root@localhost cmake-3.7.2]# cd /usr/local/src/
    [root@localhost src]# mkdir -p /usr/local/boost

    [root@localhost src]# cp boost_1_59_0.tar.gz /usr/local/boost/
    [root@localhost src]# groupadd mysql
    [root@localhost src]# useradd -g mysql mysql -s /bin/false
    [root@localhost src]# mkdir -p /data/mysql
    [root@localhost src]# chown -R mysql:mysql /data/mysql/
    [root@localhost src]# mkdir -p /usr/local/mysql
    [root@localhost src]# tar zxvf mysql-5.7.16.tar.gz
    [root@localhost src]# cd mysql-5.7.16
    [root@localhost mysql-5.7.16]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DWITH_INNOBASE_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_EMBEDDED_SERVER=OFF -DWITH_BOOST=/usr/local/boost
    -- cd /usr/local/boost; tar xfz /usr/local/boost/boost_1_59_0.tar.gz
    CMake Error: Problem with archive_read_next_header(): Truncated input file (needed 33792 bytes, only 0 available)
    CMake Error: Problem extracting tar: /usr/local/boost/boost_1_59_0.tar.gz
    -- WITH_BOOST /usr/local/boost.
    -- Failed to extract files.
    Please try downloading and extracting yourself.
    The url is: http://sourceforge.net/projects/boost/files/boost/1.59.0/boost_1_59_0.tar.gz
    CMake Error at cmake/boost.cmake:217 (MESSAGE):
    Giving up.
    Call Stack (most recent call first):
    CMakeLists.txt:455 (INCLUDE)

    -- Configuring incomplete, errors occurred!
    See also "/usr/local/src/mysql-5.7.16/CMakeFiles/CMakeOutput.log".
    See also "/usr/local/src/mysql-5.7.16/CMakeFiles/CMakeError.log".
    [root@localhost mysql-5.7.16]#

    我這裏是boost_1_59_0.tar.gz包有問題,重新下載完整的包即可,或者可以使用-DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost參數在線安裝boost軟件包,需要服務器聯網,容易下載失敗。
    cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/tmp/mysql.sock -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DENABLED_LOCAL_INFILE=ON -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_FEDERATED_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITHOUT_EXAMPLE_STORAGE_ENGINE=1 -DWITH_EMBEDDED_SERVER=OFF -DDOWNLOAD_BOOST=1 -DWITH_BOOST=/usr/local/boost
    編譯出錯, 重新編譯前要刪除編譯失敗的文件,重新編譯時,需要清除舊的對象文件和緩存信息。
    make clean
    rm -f CMakeCache.txt

    [root@localhost mysql-5.7.16]# make
    [root@localhost mysql-5.7.16]# make install
    [root@localhost mysql-5.7.16]# rm -rf /etc/my.cnf
    [root@localhost mysql-5.7.16]# cd /usr/local/mysql/
    [root@localhost mysql]# ./bin/mysqld --user=mysql --initialize --basedir=/usr/local/mysql --datadir=/data/mysql
    2018-01-29T19:37:06.836533Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
    2018-01-29T19:37:09.794126Z 0 [Warning] InnoDB: New log files created, LSN=45790
    2018-01-29T19:37:10.074405Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
    2018-01-29T19:37:10.100352Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: cc1d463a-052b-11e8-94a0-000c29bd8d97.
    2018-01-29T19:37:10.101845Z 0 [Warning] Gtid table is not ready to be used. Table ‘mysql.gtid_executed‘ cannot be opened.
    2018-01-29T19:37:10.102898Z 1 [Note] A temporary password is generated for root@localhost: D!Pd0Mokriji
    [root@localhost mysql]#

    --initialize表示默認生成密碼, --initialize-insecure 表示不生成密碼, 密碼為空。最後一行的D!Pd0Mokriji為默認生成的密碼

    [root@localhost mysql]# cp /usr/local/mysql/support-files/my-default.cnf /usr/local/mysql/my.cnf
    [root@localhost mysql]# ln -s /usr/local/mysql/my.cnf /etc/my.cnf
    [root@localhost mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
    [root@localhost mysql]# chmod 755 /etc/init.d/mysqld
    [root@localhost mysql]# chkconfig mysqld on
    [root@localhost mysql]# vi /etc/rc.d/init.d/mysqld
    basedir=/usr/local/mysql
    datadir=/data/mysql
    [root@localhost mysql]# systemctl daemon-reload
    [root@localhost mysql]# systemctl start mysqld
    [root@localhost mysql]# vi /etc/profile
    export PATH=$PATH:/usr/local/mysql/bin
    [root@localhost mysql]# source /etc/profile
    [root@localhost mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
    [root@localhost mysql]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
    [root@localhost mysql]# mkdir /var/lib/mysql
    [root@localhost mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
    [root@localhost mysql]# mysql_secure_installation
    [root@localhost mysql]# mysql_secure_installation

    Securing the MySQL server deployment.

    Enter password for user root: #輸入上面生成的密碼

    The existing password for the user account root has expired. Please set a new password.

    New password:

    Re-enter new password:

    VALIDATE PASSWORD PLUGIN can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD plugin?

    Press y|Y for Yes, any other key for No: y #是否安裝密碼安全插件?選擇y

    There are three levels of password validation policy: #有以下幾種密碼強度選擇
    Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0 #選擇0,只要8位數字即可,選1要有大寫,小寫,特殊字符等

    
    * 安裝Nginx
    1、安裝pcre

    [root@localhost src]# tar zxvf pcre-8.40.tar.gz
    [root@localhost src]# cd pcre-8.40
    [root@localhost pcre-8.40]# ./configure --prefix=/usr/local/pcre
    [root@localhost pcre-8.40]# make && make install

    2、安裝openssl

    [root@localhost src]# tar zxvf openssl-1.1.0e.tar.gz
    [root@localhost src]# mkdir /usr/local/openssl
    [root@localhost src]# cd openssl-1.1.0e
    [root@localhost openssl-1.1.0e]# ./config --prefix=/usr/local/openssl
    [root@localhost openssl-1.1.0e]# make && make install
    [root@localhost openssl-1.1.0e]# vi /etc/profile
    export PATH=$PATH:/usr/local/openssl/bin
    [root@localhost openssl-1.1.0e]# source /etc/profile

    3、安裝zlib

    [root@localhost openssl-1.1.0e]# cd /usr/local/src/
    [root@localhost src]# tar zxvf zlib-1.2.11.tar.gz
    [root@localhost src]# cd zlib-1.2.11
    [root@localhost zlib-1.2.11]# ./configure --prefix=/usr/local/zlib
    [root@localhost zlib-1.2.11]# make && make install

    4、安裝Nginx

    [root@localhost zlib-1.2.11]# cd /usr/local/src/
    [root@localhost src]# groupadd www
    [root@localhost src]# useradd -g www www -s /bin/false
    [root@localhost src]# tar nginx-1.10.3.tar.gz
    [root@localhost src]# cd nginx-1.10.3
    [root@localhost nginx-1.10.3]# ./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.1.0e --with-zlib=/usr/local/src/zlib-1.2.11 --with-pcre=/usr/local/src/pcre-8.40
    [root@localhost nginx-1.10.3]# make && make install

    --with-openssl=/usr/local/src/openssl-1.1.0e --with-zlib=/usr/local/src/zlib-1.2.11 --with-pcre=/usr/local/src/pcre-8.40指向的是源碼包解壓的路徑,而不是安裝的路徑,否則會報錯

    [root@localhost ~]# vim /etc/rc.d/init.d/nginx #將ngin配置成服務
    #!/bin/sh
    #

    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_PATH="/usr/local/nginx"
    nginx="$NGINX_PATH/sbin/nginx"
    prog=$(basename $nginx)
    NGINX_CONF_FILE="$NGINX_PATH/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
    [root@localhost init.d]# chmod 755 /etc/rc.d/init.d/nginx
    [root@localhost init.d]# chkconfig nginx on
    [root@localhost init.d]# systemctl daemon-reload
    [root@localhost init.d]# systemctl restart nginx

    *安裝php
    1、安裝yasm

    [root@localhost init.d]# cd /usr/local/src/
    [root@localhost src]# tar zxvf yasm-1.3.0.tar.gz
    [root@localhost src]# cd yasm-1.3.0
    [root@localhost yasm-1.3.0]# ./configure && make && make install

    2、安裝libmcrypt

    [root@localhost yasm-1.3.0]# cd /usr/local/src/
    [root@localhost src]# tar zxvf libmcrypt-2.5.8.tar.gz
    [root@localhost src]# cd libmcrypt-2.5.8
    [root@localhost libmcrypt-2.5.8]# ./configure && make && make install

    3、安裝libvpx

    [root@localhost libmcrypt-2.5.8]# cd /usr/local/src/
    [root@localhost src]# tar zxvf libvpx-1.3.0.tar.gz
    [root@localhost src]# cd libvpx-1.3.0
    [root@localhost libvpx-1.3.0]# ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9 && make && make install

    4、安裝tiff

    [root@localhost libvpx-1.3.0]# cd /usr/local/src/
    [root@localhost src]# tar zxvf tiff-4.0.7.tar.gz
    [root@localhost src]# cd tiff-4.0.7
    [root@localhost tiff-4.0.7]# ./configure --prefix=/usr/local/tiff --enable-shared && make && make install

    5、安裝libpng

    [root@localhost tiff-4.0.7]# cd /usr/local/src
    [root@localhost src]# tar zxvf libpng-1.6.32.tar.gz
    [root@localhost src]# cd libpng-1.6.32
    [root@localhost libpng-1.6.32]# ./configure --prefix=/usr/local/libpng --enable-shared
    [root@localhost libpng-1.6.32]# make && make install

    6、安裝freetype

    [root@localhost libpng-1.6.32]# cd /usr/local/src
    [root@localhost src]# tar zxvf freetype-2.7.1.tar.gz
    [root@localhost src]# cd freetype-2.7.1
    [root@localhost freetype-2.7.1]# ./configure --prefix=/usr/local/freetype --enable-shared && make && make install

    7、安裝jpeg

    [root@localhost freetype-2.7.1]# cd /usr/local/src
    [root@localhost src]# tar zxvf jpegsrc.v9b.tar.gz
    [root@localhost src]# cd jpeg-9b/
    [root@localhost jpeg-9b]# ./configure --prefix=/usr/local/jpeg --enable-shared && make && make install

    8、安裝libgd

    [root@localhost jpeg-9b]# cd /usr/local/src
    [root@localhost src]# tar zxvf libgd-2.1.1.tar.gz
    [root@localhost src]# cd libgd-2.1.1
    [root@localhost libgd-2.1.1]# ./configure --prefix=/usr/local/libgd --enable-shared --with-jpeg=/usr/local/jpeg --with-png=/usr/local/libpng --with-freetype=/usr/local/freetype --with-fontconfig=/usr/local/freetype --with-xpm=/usr/lib64 --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx
    [root@localhost libgd-2.1.1]# make && make install

    說明:如果libgd編譯失敗,可以先跳過,直接使用系統默認的2.1.0版本,在編譯php的時候把參數--with-gd=/usr/local/libgd修改為--with-gd即可。
    
    9、安裝t1lib

    [root@localhost libgd-2.1.1]# cd /usr/local/src
    [root@localhost src]# tar zxvf t1lib-5.1.2.tar.gz
    [root@localhost src]# cd t1lib-5.1.2
    [root@localhost t1lib-5.1.2]# ./configure --prefix=/usr/local/t1lib --enable-shared && make without_doc && make install

    10、安裝php
    如果系統是64位,請執行以下兩條命令,否則安裝php會出錯

    [root@localhost t1lib-5.1.2]# cd /usr/local/src/
    [root@localhost src]# cp -frp /usr/lib64/libltdl.so /usr/lib/
    [root@localhost src]# cp -frp /usr/lib64/libXpm.so
    /usr/lib/
    [root@localhost src]# tar zxvf php-7.1.2.tar.gz
    [root@localhost src]# cd php-7.1.2
    [root@localhost php-7.1.2]# export LD_LIBRARY_PATH=/usr/local/libgd/lib
    [root@localhost php-7.1.2]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd=/usr/local/libgd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/lib64 --with-zlib-dir=/usr/local/zlib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype --enable-mysqlnd
    checking for XpmFreeXpmImage in -lXpm... (cached) yes
    checking for gdSetErrorMethod in -lgd... no
    configure: error: Unable to find libgd.(a|so) >= 2.1.0 anywhere under /usr/local/libgd
    [root@localhost php-7.1.2]#

    如果提示libgd版本錯誤,把php編譯參數--with-gd=/usr/local/libgd修改為--with-gd即可

    [root@localhost php-7.1.2]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/lib64 --with-zlib-dir=/usr/local/zlib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype --enable-mysqlnd
    [root@localhost php-7.1.2]# make && make install
    [root@localhost php-7.1.2]# cp php.ini-production /usr/local/php/etc/php.ini
    [root@localhost php-7.1.2]# rm -rf /etc/php.ini
    [root@localhost php-7.1.2]# ln -s /usr/local/php/etc/php.ini /etc/php.ini
    [root@localhost php-7.1.2]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
    [root@localhost php-7.1.2]# ln -s /usr/local/php/etc/php-fpm.conf /etc/php-fpm.conf
    [root@localhost php-7.1.2]# vim /usr/local/php/etc/php-fpm.conf
    pid = run/php-fpm.pid #取消註釋
    [root@localhost php-7.1.2]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
    [root@localhost php-7.1.2]# vi /usr/local/php/etc/php-fpm.d/www.conf
    user = www
    group = www
    [root@localhost php-7.1.2]# cp /usr/local/src/php-7.1.2/sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
    [root@localhost php-7.1.2]# chmod +x !$
    chmod +x /etc/rc.d/init.d/php-fpm
    [root@localhost php-7.1.2]# chkconfig php-fpm on
    [root@localhost php-7.1.2]# vi /usr/local/php/etc/php.ini
    disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname #列出PHP可以禁用的函數,如果某些程序需要用到這個函數,可以刪除,取消禁用。
    date.timezone = PRC #設置時區
    expose_php = Off #禁止顯示php版本的信息
    short_open_tag = ON #支持php短標簽
    opcache.enable=1 #php支持opcode緩存
    opcache.enable_cli=0 #php支持opcode緩存
    zend_extension=opcache.so #開啟opcode緩存功能
    [root@localhost php-7.1.2]# vi /usr/local/nginx/conf/nginx.conf #配置nginx支持php
    user www; #必須與/usr/local/php/etc/php-fpm.conf中的user,group配置相同,否則php運行出錯
    location / {
    root html;
    index index.html index.htm index.php; #添加index.php
    }

    pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

    取消FastCGI server以下location的註釋

        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;     #fastcgi_param行的參數,改為$document_root$fastcgi_script_name,或者使用絕對路徑
            include        fastcgi_params;
        }

    [root@localhost php-7.1.2]# systemctl restart nginx
    [root@localhost php-7.1.2]# systemctl restart php-fpm

    驗證測試

    [root@localhost php-7.1.2]# cd /usr/local/nginx/html/
    [root@localhost html]# vim index.php

    <?php
    phpinfo();
    ?>
    [root@localhost html]# chown -R www:www /usr/local/nginx/html/
    [root@localhost html]# chmod -R 700 /usr/local/nginx/html/

    瀏覽器驗證

    技術分享圖片

    CentOS7編譯安裝LNMP