LNMP編譯安裝

  1. LNMP簡介
    Linux系統下Nginx+MySQL+PHP這種網站服務器架構。Nginx是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器。Mysql是一個小型關係型數據庫管理系統。PHP是一種在服務器端執行的嵌入HTML文檔的腳本語言。這四種軟件均爲免費開源軟件,組合到一塊兒,成爲一個免費、高效、擴展性強的網站服務系統。php

  2. 環境準備
    ```bash
    #下載所需程序安裝包
    CentOS-6.8-x86_64 下載地址:https://www.centos.org/
    nginx-1.11.2.tar.gz 下載地址:http://nginx.org/download/nginx-1.11.2.tar.gz
    mysql-5.6.31.tar.gz 下載地址:http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.31.tar.gz
    php-5.6.23.tar.gz 下載地址:http://php.net/get/php-5.6.22.tar.gz/from/a/mirrorhtml

    #以下
    [root@localhost src]# ls /usr/local/src/
    nginx-1.11.2.tar.gz mysql-5.6.31.tar.gz php-5.6.23.tar.gzmysql

    #安裝epel擴展源
    rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpmnginx

    #使用yum安裝依賴庫
    yum install -y gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libpng libpng-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses curl openssl-devel gdbm-devel db4-devel libXpm-devel libX11-devel gd-devel gmp-devel readline-devel libxslt-devel expat-devel xmlrpc-c xmlrpc-c-devel pcre pcre-devel cmake mcrypt php-mcrypt libmcrypt libmcrypt-devel
    ```
  3. 編譯安裝MySQL
    ```bash
    #建立MySQL運行用戶
    useradd -M -s /sbin/nologin mysqlc++

    #建立MySQL數據目錄並配置權限
    chown -R mysql:mysql /data/mysql/
    chmod -R 755 /data/mysql/web

    #編譯安裝MySQL
    [root@localhost src]# cd /usr/local/src/
    [root@localhost src]# tar -xvzf mysql-5.6.31.tar.gz
    [root@localhost src]# cd /usr/local/src/mysql-5.6.31
    [root@host1 mysql-5.6.31]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock -DMYSQL_USER=mysql -DMYSQL_TCP_PORT=3306 -DSYSCONFDIR=/etc -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_MENORY_STORAGE_ENGINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_DEBUG=0 -DWITH_EMBEDDED_SERVER=1 -DENABLED_LOCAL_INFILE=1 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci
    [root@host1 mysql-5.6.31]# make && make installsql

    #初始化mysql
    [root@localhost mysql]# cd /usr/local/mysql/
    [root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/數據庫

    #拷貝mysql啓動腳本並修改
    [root@localhost mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
    [root@localhost ~]# ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
    [root@localhost mysql]# vim /etc/init.d/mysqld
    修改以下兩行爲相應路徑
    basedir=/usr/local/mysql/
    datadir=/data/mysql/json

    #拷貝配置文件並修改
    [root@localhost mysql]# cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf
    #主要修改以下部分
    [mysqld]
    basedir = /usr/local/mysql/
    datadir = /data/mysql/
    port = 3306
    server_id = 1
    socket = /data/mysql/mysql.sockvim

    #啓動mysql並測試
    [root@localhost ~]# /usr/local/mysql/bin/mysqld_safe &
    [root@localhost mysql]# netstat -pantu | grep mysql
    tcp 0 0 :::3306 :::* LISTEN 47033/mysqld
    #存在mysql監聽3306端口則表示mysql安裝正常
    ```

  4. 編譯安裝PHP
    ```bash
    #添加php-fpm的執行用戶
    useradd -M -s /sbin/nologin php-fpm

    #編譯安裝PHP
    cd /usr/local/src/
    tar -xvzf php-5.6.22.tar.gz
    cd php-5.6.22
    [root@host1 php-5.6.22]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=/usr/local/mysql/bin/mysql_config --with-libxml-dir --with-gd --with-curl --with-pcre-regex --with-sqlite3 --with-zlib --with-jpeg-dir --with-png-dir --with-freetype-dir --with-iconv-dir --with-zlib-dir --with-bz2 --with-openssl --with-mcrypt --enable-fpm --with-fpm-user=php-fpm --with-fpm-group=php-fpm --with-mysql=/usr/local/mysql --with-mysql-sock=/data/mysql/mysql.sock --enable-sysvsem --enable-sockets --enable-pcntl --enable-bcmath --enable-mbstring --enable-mysqlnd --enable-opcache --enable-shmop --enable-json --enable-exif --enable-zip --enable-ftp --enable-gd-native-ttf --enable-wddx --enable-soap --disable-ipv6
    [root@host1 php-5.6.22]# make && make install

    #拷貝php配置文件php.ini
    cp /usr/local/src/php-5.6.22/php.ini-production /usr/local/php/etc/php.ini
    #拷貝php啓動腳本php-fpm
    cp /usr/local/src/php-5.6.22/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
    chmod +x php-fpm
    chkconfig --add php-fpm
    chkconfig php-fpm on

    #拷貝php配置文件
    mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

    #啓動php-fpm服務
    chmod+x /etc/init.d/php-fpm
    service php-fpm start

    #查看php-fpm的進程和佔用端口
    [root@host1 php]# netstat -pantu|grep php-fpm
    tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 56407/php-fpm
    [root@host1 php]# ps aux|grep php-fpm|grep -v grep
    root 56407 0.0 0.2 216284 5056 ? Ss 19:39 0:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
    php-fpm 56408 0.0 0.2 216284 4724 ? S 19:39 0:00 php-fpm: pool www
    php-fpm 56409 0.0 0.2 216284 4724 ? S 19:39 0:00 php-fpm: pool www
    ```

  5. 編譯安裝nginx
    ```bash
    #編譯安裝nginx
    cd /usr/local/src/
    tar -xvzf nginx-1.11.2.tar.gz
    cd /usr/local/src/nginx-1.11.2
    [root@host1 nginx-1.11.2]# ./configure --prefix=/usr/local/nginx/ --with-pcre
    [root@host1 nginx-1.11.2]# make && make install

    #建立主程序軟鏈接
    ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx

    #配置NGINX解析php
    [root@host1 nginx]# vim /usr/local/nginx/conf/nginx.conf
    #配置以下內容,其中fastcgi_param路徑要與web路徑保持一致
    location ~ .php$ {
    root html;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    include fastcgi_params;
    }

    #nginx配置文件語法檢查
    [root@CentOS6 html]# /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 successfu

    #啓動nginx [root@host1 nginx]# /usr/local/nginx/sbin/nginx ```

相關文章
相關標籤/搜索