LNMP架構介紹,MySQL安裝,PHP安裝,Nginx介紹

LNMP架構介紹

  • 和lamp不一樣的是,提供web服務的是nginx

  • 而且php是做爲獨立服務存在的,這個服務叫作php-fpm

  • Nginx直接處理靜態請求,動態請求會轉發給php-fpm

Ngeinx 設計初衷是提供一種快速、高效、多併發的web服務軟件。在靜態頁面處理上,Nginx較Apache更勝一籌。可是在動態頁面處理上,Nginx並不能比Apache有優點。可是目前仍是有不少愛好者對Nginx比較熱衷。隨着Nginx技術的逐漸成熟,它在web服務軟件領域的地位也會愈來愈高。javascript


MySQl安裝

  • 先到/usr/local/src下 而後去r.aminglinux.com找下載連接,而後wget下載
[root@aminglinux-01 src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
  • 解壓 tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  • 移動位置 mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
[root@aminglinux-01 src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
[root@aminglinux-01 src]# ^C
[root@aminglinux-01 src]# cd ../mysql/
[root@aminglinux-01 mysql]# ls
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
[root@aminglinux-01 mysql]#
  • 建立mysql用戶 建立/data/目錄
useradd -s /sbin/nologin mysql
mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
  • 初始化
[root@aminglinux-01 mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql

報錯php

FATAL ERROR: please install the following Perl modules before executing scripts/mysql_install_db:
Data::Dumper

安裝yum install -y perl-Module-Install.noarchcss

報錯Installing MySQL system tables.../usr/local/mysql/bin/mysqld: error while loading sharedhtml

緣由:缺乏libaio庫文件 

解決方法:yum install libaio* -y
  • copy配置文件

cp support-files/my-default.cnf /etc/my.cnfjava

而後vim /etc/my.cnfnode

定義以下:mysql

datadir = /data/mysql
# port = .....
# server_id = .....
socket = /tmp/mysql.sock
  • copy啓動腳本
[root@aminglinux-01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld

編輯一下,有兩個地方須要改linux

basedir=/usr/local/mysql
datadir=/data/mysql
  • 啓動服務 /etc/init.d/mysqld start
[root@aminglinux-01 mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/aminglinux-01.err'.
. SUCCESS! 
[root@aminglinux-01 mysql]#
[root@aminglinux-01 mysql]# ps aux |grep mysql
root       4080  0.0  0.1 113256  1612 pts/0    S    22:08   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/aminglinux-01.pid
mysql      4215  2.0 44.9 973048 449532 pts/0   Sl   22:08   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/aminglinux-01.err --pid-file=/data/mysql/aminglinux-01.pid --socket=/tmp/mysql.sock
root       4241  0.0  0.0 112664   976 pts/0    R+   22:09   0:00 grep --color=auto mysql
[root@aminglinux-01 mysql]#

把mysql加入到服務行列 chkconfig --add mysqldnginx

設置開機啓動 chkconfig mysqld onweb

這時候就能夠執行 service mysqld stop 或者 start 的命令了。

PHP安裝

  • 在lnmp裏面php的安裝和lamp安裝是有差異的。在這裏php須要開啓php-fpm服務,是一個獨立的存在。
  • 安裝

cd /usr/local/src/

wget http://cn2.php.net/distributions/php-5.6.30.tar.gz

解壓 tar zxvf php-5.6.30.tar.gz

建立用戶 useradd -s /sbin/nologin php-fpm

cd php-5.6.30/

  • 初始化

yum install -y epel-release yum -y install gcc libxml2-devel openssl openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype-devel libmcrypt-devel

./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

報錯checking for cc... no checking for gcc... no

yum install gcc -y

報錯configure: error: xml2-config not found. Please check your libxml2 installation.

yum install libxml2-devel -y

報錯configure: error: Cannot find OpenSSL's <evp.h>

yum install -y openssl openssl-devel

報錯configure: error: Please reinstall the libcurl distribution - easy.h should be in <curl-dir>/include/curl/

yum install libcurl-devel -y

報錯:configure: error: jpeglib.h not found.

yum install -y libjpeg-devel

報錯:configure: error: png.h not found.

yum install -y libpng-devel

報錯: configure: error: freetype-config not found.

yum install -y freetype-devel

報錯: configure: error: mcrypt.h not found. Please reinstall libmcrypt.

yum install -y epel-release  先安裝擴展源
 yum install -y libmcrypt-devel
  • make && make install

報錯:libtool: link: `ext/opcache/ZendAccelerator.lo' is not a valid libtool object

解決辦法
# make distclean
從新初始化,安裝
  • 安裝成功後,能夠看一下這個目錄,是啓動php-fpm服務用的
[root@aminglinux-01 php-fpm]# cd /usr/local/php-fpm/sbin/
[root@aminglinux-01 sbin]# ls
php-fpm
  • /usr/local/php-fpm/sbin/php-fpm -t 是測試它本身配置文件的語法有沒有問題的
[root@aminglinux-01 sbin]# /usr/local/php-fpm/sbin/php-fpm -t
[19-Oct-2017 09:58:56] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[19-Oct-2017 09:58:56] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[19-Oct-2017 09:58:56] ERROR: FPM initialization failed
[root@aminglinux-01 sbin]#

發現配置文件不存在,配置它,讓服務起來。

  • cp /usr/local/src/php-5.6.30/php.ini-production /usr/local/php-fpm/etc/php.ini
  • 建立/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
  • cp啓動腳本
[root@aminglinux-01 php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
  • 而後加到服務列表裏去,首先要改權限,添加列表,開機啓動
[root@aminglinux-01 php-5.6.30]# chmod 755 /etc/init.d/php-fpm
[root@aminglinux-01 php-5.6.30]# chkconfig --add php-fpm
[root@aminglinux-01 php-5.6.30]# chkconfig php-fpm on
  • 啓動和查看
[root@aminglinux-01 php-5.6.30]# service php-fpm start
Starting php-fpm  done
[root@aminglinux-01 php-5.6.30]# ps aux |grep php-fpm
root      73918  0.0  0.4 123548  4960 ?        Ss   10:31   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   73919  0.0  0.4 123548  4712 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73920  0.0  0.4 123548  4712 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73921  0.0  0.4 123548  4712 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73922  0.0  0.4 123548  4712 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73923  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73924  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73925  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73926  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73927  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73928  0.0  0.4 123548  4716 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73929  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73930  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73931  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73932  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73933  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73934  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73935  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73936  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73937  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
php-fpm   73938  0.0  0.4 123548  4720 ?        S    10:31   0:00 php-fpm: pool www
root      73940  0.0  0.0 112668   976 pts/0    R+   10:31   0:00 grep --color=auto php-fpm
[root@aminglinux-01 php-5.6.30]#

Nginx安裝

解壓:tar zxvf nginx-1.8.0.tar.gz

cd nginx-1.8.0

  • 初始化 ./configure --prefix=/usr/local/nginx
  • make && make install
安裝Nginx時報錯


./configure: error: the HTTP rewrite module requires the PCRE library.


安裝pcre-devel解決問題

yum -y install pcre-devel

 

錯誤提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library.   You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.

解決辦法:

yum -y install openssl openssl-devel

 

總結:

yum -y install pcre-devel openssl openssl-devel

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

make

make install

一切搞定
  • 下面要給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

而後改權限,添加服務,設置開機自啓

[root@aminglinux-01 nginx-1.8.0]# chmod 755 /etc/init.d/nginx 
[root@aminglinux-01 nginx-1.8.0]# chkconfig --add nginx
[root@aminglinux-01 nginx-1.8.0]# chkconfig nginx on
  • cd /usr/local/nginx/conf/ 到這個目錄裏,配置一下nginx.conf,這裏面已經有一個了,咱們不用他,本身配置
[root@aminglinux-01 nginx-1.8.0]# cd /usr/local/nginx/conf/
[root@aminglinux-01 conf]# mv nginx.conf nginx.conf.bak

編輯配置文件以下 vim 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;
        }    
    }
}
  • 啓動

檢查配置是否正確 :/usr/local/nginx/sbin/nginx -t

啓動 : /etc/init.d/nginx start

[root@aminglinux-01 conf]# /etc/init.d/nginx start
Starting nginx (via systemctl):                            [  肯定  ]
[root@aminglinux-01 conf]# ps aux |grep nginx
root      77488  0.0  0.0  24828   792 ?        Ss   22:08   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    77489  0.0  0.3  27272  3376 ?        S    22:08   0:00 nginx: worker process
nobody    77490  0.0  0.3  27272  3376 ?        S    22:08   0:00 nginx: worker process
root      77492  0.0  0.0 112668   976 pts/0    S+   22:09   0:00 grep --color=auto nginx
[root@aminglinux-01 conf]#
[root@aminglinux-01 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>
[root@aminglinux-01 conf]#
  • 測試php解析
  • vi /usr/local/nginx/html/1.php //加入以下內容

<?php
    echo "test php scripts.";
?>
  • curl localhost/1.php

Nginx介紹

  • Nginx官網 nginx.org,最新版1.13,最新穩定版1.12
  • Nginx應用場景:web服務、反向代理、負載均衡
  • Nginx著名分支,淘寶基於Nginx開發的Tengine,使用上和Nginx一致,服務名,配置文件名都同樣,和Nginx的最大區別在於Tenging增長了一些定製化模塊,在安全限速方面表現突出,另外它支持對js,css合併
  • Nginx核心+lua相關的組件和模塊組成了一個支持lua的高性能web容器openresty,參考http://jinnianshilongnian.iteye.com/blog/2280928

擴展

Nginx爲何比Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html mod_php 和 mod_fastcgi以及php-fpm的比較 http://dwz.cn/1lwMSd 概念瞭解:CGI,FastCGI,PHP-CGI與PHP-FPM http://www.nowamagic.net/librarys/veda/detail/1319/ https://www.awaimai.com/371.html

相關文章
相關標籤/搜索