LNMP架構(一)

12.1 LNMP架構介紹

 


12.2 MySQL安裝

由於以前已經搭建了LAMP環境,已經安裝了MySQL,爲了不衝突和出錯,咱們把以前安裝的MySQL卸載。javascript

#rm -rf /usr/local/mysqlphp

#rm -rf /etc/init.d/mysqldcss

而後再安裝。html

解壓:#tar xvzf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gzjava

#mv mysql-5.6.39-linux-glibc2.12-x86_64 /usr/local/mysql/node

#cd /usr/local/mysqlmysql

建立mysql用戶(由於以前已經建立,故這一步省略)#id mysql
uid=10014(mysql) gid=10015(mysql) 組=10015(mysql)linux

刪除以前安裝的mysql目錄下的文件#rm -rf /data/mysql/*nginx

#./scripts/mysql_install_db --user=mysql --datadir=/data/mysqlweb

#cp support-files/mysql.server /etc/init.d/mysql

而後編輯文件/etc/init.d/mysql

#mv /etc/init.d/mysql /etc/init.d/mysqld

啓動mysql服務

#/etc/init.d/mysqld start

設定開機啓動

#chkconfig --add mysqld

#chkconfig mysqld on


12.3/12.4 PHP安裝

進入/usr/local/src目錄,再進入php-5.6.32目錄,執行make clean,把以前編譯過的文件刪掉,恢復解壓時的狀態。

而後配置編譯選項:

./configure \
--prefix=/usr/local/php-fpm \
--with-config-file-path=/usr/local/php-fpm/etc \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-mysql=/usr/local/mysql \
--with-mysql-sock=/tmp/mysql.sock \
--with-libxml-dir \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-iconv-dir \
--with-zlib-dir \
--with-mcrypt \
--enable-soap \
--enable-gd-native-ttf \
--enable-ftp \
--enable-mbstring \
--enable-exif \
--disable-ipv6 \
--with-pear \
--with-curl \
--with-openssl

此時出現錯誤信息,解決的方法以下:

#yum install -y libcurl-devel

再進行配置編譯選項,而後再編譯PHP:

#make

#make install

而後查看fpm是否可用:

#/usr/local/php-fpm/sbin/php-fpm -m

#/usr/local/php-fpm/sbin/php-fpm -i

而後修改配置文件:

#cp php.ini-production /usr/local/php-fpm/php.ini

#vim /usr/local/php-fpm/etc/php-fpm.conf

寫入以下內容:

[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www]
listen = /tmp/php-fcgi.sock
listen.mode = 666
user = php-fpm
group = php-fpm
pm = dynamic
pm.max_children = 50
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

保存配置文件後,檢驗配置是否正確:

啓動php-fpm:

設置php-fpm開機啓動:

#chkconfig php-fpm on

檢測php-fpm是否啓動:


12.5 Nginx介紹

Nginx官網:nginx.org,目前最新版本爲1.13,最新穩定版本爲1.12。

Nginx應用場景:web服務、反向代理、負載均衡。

Nginx著名分支,淘寶基於Nginx開發的Tengine,使用上和Nginx一致,服務名,配置文件名都同樣,和Nginx的最大區別在於Tenging增長了一些定製化模塊,在安全限速方面表現突出,另外它支持對js、css合併。

Nginx核心+lua相關的組件和模塊組成了一個支持lua的高性能web容器openresty。


12.6 Nginx安裝

切換到目錄#/usr/local/src

而後下載nginx安裝包#wget http://nginx.org/download/nginx-1.12.1.tar.gz

解壓安裝包#tar xvzf nginx-1.12.1.tar.gz

#cd nginx-1.12.1/

#./configure --prefix=/usr/local/nginx

#make

#make insall

安裝完成後,編寫Nginx啓動腳本,並加入系統服務:

#vim /etc/init.d/nginx

寫入如下內容:

#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings

NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"

start() 
{
    echo -n $"Starting $prog: "
    mkdir -p /dev/shm/nginx_temp
    daemon $NGINX_SBIN -c $NGINX_CONF
    RETVAL=$?
    echo
    return $RETVAL
}

stop() 
{
    echo -n $"Stopping $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -TERM
    rm -rf /dev/shm/nginx_temp
    RETVAL=$?
    echo
    return $RETVAL
}

reload()
{
    echo -n $"Reloading $prog: "
    killproc -p $NGINX_PID $NGINX_SBIN -HUP
    RETVAL=$?
    echo
    return $RETVAL
}

restart()
{
    stop
    start
}

configtest()
{
    $NGINX_SBIN -c $NGINX_CONF -t
    return 0
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  reload)
        reload
        ;;
  restart)
        restart
        ;;
  configtest)
        configtest
        ;;
  *)
        echo $"Usage: $0 {start|stop|reload|restart|configtest}"
        RETVAL=1
esac

exit $RETVAL

保存該腳本後更改權限:

#chmod 755 /etc/init.d/nginx

#chkconfig --add nginx

設置開機啓動:

#chkconfig nginx on

而後更改Nginx的配置文件:

清空原來的配置文件:]#> /usr/local/nginx/conf/nginx.conf

#vim /usr/local/nginx/conf/nginx.conf

加入如下內容:

user nobody nobody;
worker_processes 2;
error_log /usr/local/nginx/logs/nginx_error.log crit;
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;

events
{
    use epoll;
    worker_connections 6000;
}

http
{
    include mime.types;
    default_type application/octet-stream;
    server_names_hash_bucket_size 3526;
    server_names_hash_max_size 4096;
    log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';
    sendfile on;
    tcp_nopush on;
    keepalive_timeout 30;
    client_header_timeout 3m;
    client_body_timeout 3m;
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 1k;
    large_client_header_buffers 8 4k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
    client_body_temp_path /usr/local/nginx/client_body_temp;
    proxy_temp_path /usr/local/nginx/proxy_temp;
    fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
    fastcgi_intercept_errors on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 8k;
    gzip_comp_level 5;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm 
    application/xml;

    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;

        location ~ \.php$ 
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}

保存配置文件後,檢驗是否有誤:

如顯示上圖,則說明配置正確。

啓動Nginx:

#service nginx start
查看是否啓動成功:

再測試是否正確解析PHP:

首先建立測試文件:

#vim /usr/local/nginx/html/1.php
寫入內容:

<?php
    echo "test php scripts.";
?>
而後執行以下命令測試文件:

#curl localhost/1.php

相關文章
相關標籤/搜索