LNMP基礎架構介紹與安裝

1、LNMP架構介紹

LNMP==Linux+Nginx+Mysql+PHP
nginx:省資源,省cpu,所以在高併發時能夠處理更多的請求,高端能達到3萬到5萬的併發量。
nginx和LAMP不同的是,提供web服務的是Nginx
並且php是作爲一個獨立服務存在的,這個服務叫做php-fpm
Nginx直接處理靜態請求,動態請求會轉發給php-fpm
mark
在當前互聯網環境下,一般高端的服務前端都採用nginx作爲web前端,而更多的都是採用lnmp架構,真正的後端服務器纔會採用apache.
爲什麼這麼做,要取決於nginx和apache兩者之間的優缺性.:
nginx與apache相比有以下優勢:在性能上,nginx佔用很少的系統資源,能支持更多的併發鏈接,達到更高的訪問率;在功能上,Nginx是優秀的代理服務器和負載均衡器;在安裝配置上,簡單靈活。
nginx模塊基本都是靜態編譯,同時對Fast-CGI支持比較好.在處理鏈接上,nginx支持epoll,而且體積小一般只有幾百K。
Nginx的優點有以下幾點:
1.作爲Web服務器,nginx處理靜態文件、索引文件以及自動索引效率非常高。
2.作爲代理服務器,Nginx可以實現無緩存的反向代理加速,提高網站運行速度。
3.作爲負載均衡服務器,Nginx既可以在內部直接支持Rails和PHP,也可以支持HTTP代理服務器,對外進行服務。同時支持簡單的容錯和利用算法進行負載均衡。
4.在性能方面,Nginx是專門爲性能優化而開發的,在實現上非常注重效率。它採用內核Poll模型,可以支持更多的併發連接,最大可以支持對50 000個併發連接數的響應,而且佔用很低的內存資源。
5.在穩定性方面,Nginx採取了分階段資源分配技術,使得對CPU與內存的佔用率非常低。Nginx官方表示Nginx保持10 000個沒有活動的連接,這些連接只佔2.5M內存,因此,類似DOS這樣的***對Nginx來說基本上是沒有任何作用的。
6.在高可用性方面,Nginx支持熱部署,啓動速度特別迅速,因此可以在不間斷服務的情況下,對軟件版本或者配置進行升級,即使運行數月也無需重新啓動,幾乎可以做到7×24小時的不間斷運行。

2、mysql安裝

安裝mysql的配置,其實在做LAMP的時候已經做過了,一模一樣的編譯配置。http://blog.51cto.com/chy940405/2047288 (有需要的可以看看這篇)

3、php安裝總結

和LAMP安裝PHP方法有差別,需要開啓php-fpm服務

[root@chy php-5.6.30]#  wget http://cn2.php.net/distributions/php-5.6.30.tar.gz 
[root@chy php-5.6.30]# tar  -zxvf php-5.6.30.tar.gz 
[root@chy01 php-5.6.30]# useradd -s /sbin/nologin php-fpm (增加php-fpm用戶)
[root@chy01 php-5.6.30]# ./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-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-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 --with-pear --with-curl  --with-openssl
(編譯參數如上,如下是詳解參數 ./configure --prefix=/usr/local/php-fpm(php路徑) --with-config-file-path=/usr/local/php-fpm/etc(php配置文件路徑) --enable-fpm(啓動服務必須的參數) --with-fpm-user=php-fpm(指定php的用戶) --with-fpm-group=php-fpm(指定phpd 的組) --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-pdo-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 --with-pear --with-curl  --with-openssl 

報錯信息1:configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/
解決方法:[root@chy01 php-5.6.30]# yum install -y libcurl-devel 
然後再次編譯時:編譯成功
[root@chy01 php-5.6.30]# echo $?
0
[root@chy01 php-5.6.30]# make && make install 
(然後make 與make install)
[root@chy01 php-5.6.30]# echo $?
0
(make 成功)

php-fpm簡單介紹

[root@chy01 php-5.6.30]# ls /usr/local/php-fpm/sbin/
php-fpm
其中(php-fpm是php-fpm的啓動文件)
[root@chy01 php-5.6.30]# ls /usr/local/php-fpm/var/log/
(查看日誌的啓動文件)
[root@chy01 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -i 
[root@chy01 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -m
(查看php的模塊)
[root@chy01 php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t
[10-Aug-2017 01:41:12] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[10-Aug-2017 01:41:12] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[10-Aug-2017 01:41:12] ERROR: FPM initialization failed
(-t測試php-fpm的配置的語法是否正確)
[root@chy01 php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
(cp php的線上的配置文件到/usr/local/php-fpm/etc/php.ini下去)
[root@chy01 php-5.6.30]# cd /usr/local/php-fpm/etc/
[root@chy01 etc]# ls
pear.conf  php-fpm.conf.default  php.ini
[root@chy01 etc]# vim php-fpm.conf
(創建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
(配置文件含義如下:[global](定義全局參數)
pid = /usr/local/php-fpm/var/run/php-fpm.pid (定義pid)
error_log = /usr/local/php-fpm/var/log/php-fpm.log
[www] (服務的名稱)
listen = /tmp/php-fcgi.sock(監聽的sock)
#listen =127.0.0.1:9000 (這個是監聽的ip地址)
listen.mode = 666(這個是如果監聽的是sock此項纔會生效,則不會生效)
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
[root@chy01 etc]# cd /usr/local/src/php-5.6.30 (進入到php的源碼包裏)
[root@chy01 php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
(cp配置文件到/etc/init.d/php-fpm 下)
[root@chy01 php-5.6.30]# chmod 755 /etc/init.d/php-fpm 
(更改配置權限爲755)
[root@chy01 php-5.6.30]# chkconfig --add php-fpm
[root@chy01 php-5.6.30]# chkconfig --list

注意:該輸出結果只顯示 SysV 服務,並不包含原生 systemd 服務。SysV 配置數據可能被原生 systemd 配置覆蓋。 
      如果您想列出 systemd 服務,請執行 'systemctl list-unit-files'。
      欲查看對特定 target 啓用的服務請執行
      'systemctl list-dependencies [target]'。

apache2         0:關 1:關 2:關 3:開 4:關 5:開 6:關
mysqld          0:關 1:關 2:開 3:開 4:開 5:開 6:關
netconsole      0:關 1:關 2:關 3:關 4:關 5:關 6:關
network         0:關 1:關 2:開 3:開 4:開 5:開 6:關
php-fpm         0:關 1:關 2:開 3:開 4:開 5:開 6:關
(增加到開機啓動)
[root@chy01 php-5.6.30]# /etc/init.d/php-fpm start
Starting php-fpm  done
(啓動服務)
[root@chy01 php-5.6.30]# ps aux |grep php-fpm
root     125159  0.0  0.3 226640  4948 ?        Ss   02:13   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm  125160  0.0  0.3 226640  4712 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125161  0.0  0.3 226640  4712 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125162  0.0  0.3 226640  4712 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125163  0.0  0.3 226640  4712 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125164  0.0  0.3 226640  4716 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125165  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125166  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125167  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125168  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125169  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125170  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125171  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125172  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125173  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125174  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125175  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125176  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125177  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125178  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
php-fpm  125179  0.0  0.3 226640  4720 ?        S    02:13   0:00 php-fpm: pool www
root     125220  0.0  0.0 112664   976 pts/0    R+   02:15   0:00 grep --color=auto php-fpm
[root@chy01 php-5.6.30]# ls -l /tmp/php-fcgi.sock 
srw-rw-rw- 1 root root 0 8月  10 02:13 /tmp/php-fcgi.sock

4nginx介紹與安裝

nginx官網http://nginx.org
Nginx應用場景:web服務、反向代理、負載均衡
Nginx著名分支,淘寶基於Nginx開發的Tengine,使用上和Nginx一致,服務名,配置文件名都一樣,和Nginx的最大區別在於Tenging增加了一些定製化模塊,在安全限速方面表現突出,另外它支持對js,css合併

[root@chy01 src]# wget http://nginx.org/download/nginx-1.12.1.tar.gz
(下載nginx安裝包)
[root@chy01 src]# tar zxvf nginx-1.12.1.tar.gz (解壓)
[root@chy01 src]./configure --prefix=/usr/local/nginx
[root@chy01 src]# cd nginx-1.12.1
(進入到此目錄下開始編譯)
[root@chy01 nginx-1.12.1]# echo $?
0
[root@chy01 nginx-1.12.1]# make && make install
[root@chy01 nginx-1.12.1]# echo $?
0
(編譯成功)
[root@chy01 nginx-1.12.1]# /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 successful
(-t檢查配置文件語法是否錯誤)
[root@chy01 nginx-1.12.1]# 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
[root@chy01 nginx-1.12.1]# chmod 755 /etc/init.d/nginx  (增加nginx權限)
[root@chy01 nginx-1.12.1]# chkconfig --add nginx
(將nginx設置爲開機自啓)
[root@chy01 nginx-1.12.1]# cd /usr/local/nginx/conf/ (配置nginx的配置文件,需要先下一個模板)
[root@chy01 conf]# mv nginx.conf nginx.conf.1
(因nginx已經有一個配置文件,需要將此配置文件cp並且重新更改一個自己的配置文件)
[root@chy01 conf]# vim nginx.conf
(配置nginx的配置文件)
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;
        }    
    }
}
如下是配置文件的簡單介紹:
user nobody nobody; (定義的nginx的用戶)
worker_processes 2;(定義子進程有幾個)
error_log /usr/local/nginx/logs/nginx_error.log crit; (定義錯誤日誌)
pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;(定義nginx最多能打開多少個文件)
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$ (配置解析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;
        }    
    }
}
[root@chy01 conf]# /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 successful
(查看nginx配置文件是否有錯)
[root@chy01 conf]# /etc/init.d/nginx start(這裏報了一個錯誤)
Starting nginx (via systemctl):  Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
                                                           [失敗]
(根據提示查錯誤,[root@chy01 conf]# systemctl status nginx.service
● nginx.service - SYSV: http service.
   Loaded: loaded (/etc/rc.d/init.d/nginx; bad; vendor preset: disabled)
   Active: failed (Result: exit-code) since 五 2017-08-11 00:23:23 CST; 36s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 5613 ExecStart=/etc/rc.d/init.d/nginx start (code=exited, status=1/FAILURE)

8月 11 00:23:21 chy01 nginx[5613]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
8月 11 00:23:21 chy01 nginx[5613]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
8月 11 00:23:22 chy01 nginx[5613]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
8月 11 00:23:22 chy01 nginx[5613]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
8月 11 00:23:23 chy01 nginx[5613]: nginx: [emerg] still could not bind()
8月 11 00:23:23 chy01 nginx[5613]: [失敗]
8月 11 00:23:23 chy01 systemd[1]: nginx.service: control process exited, code=exited status=1
8月 11 00:23:23 chy01 systemd[1]: Failed to start SYSV: http service..
8月 11 00:23:23 chy01 systemd[1]: Unit nginx.service entered failed state.
8月 11 00:23:23 chy01 systemd[1]: nginx.service failed.
(可以看到提示錯誤nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use) 提示的意思爲80端口被使用)
[root@chy01 conf]# /usr/local/apache2.4/bin/apachectl stop
(將http關閉)
[root@chy01 conf]# /etc/init.d/nginx start 
Starting nginx (via systemctl):                            [  確定  ]
(在進行啓動,啓動成功)
[root@chy01 conf]# ps aux |grep nginx
root       5655  0.0  0.0  20484   628 ?        Ss   00:24   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody     5656  0.0  0.2  22928  3216 ?        S    00:24   0:00 nginx: worker process
nobody     5657  0.0  0.2  22928  3216 ?        S    00:24   0:00 nginx: worker process
root       5672  0.0  0.0 112664   976 pts/0    S+   00:26   0:00 grep --color=auto nginx
(可以看到子進程爲root,主進程爲nobody)
[root@chy01 conf]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
(nginx 進行測試)
[root@chy01 conf]# vim /usr/local/nginx/html/1.php
(創建一個php的測試文件
<?php
echo "this is nginx test page";
[root@chy01 conf]# curl localhost/1.php
this is nginx test page[root@chy01 conf]# 
(如上解析成功)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章