LNMP基礎架構
阿新 • • 發佈:2018-03-14
LNMP LNMP架構 [toc]
LNMP基礎架構
12.1 LNMP架構介紹
架構原理:
LAMP=linux+apache+mysql+php
LNMP=linux+nginx+mysql+php
1.初識LNMP
當我們講LAMP的時候PHP是作為一個模塊在Apache中,但是在LNMP中,PHP是一個服務,當用戶請求的時候nginx會把它交給php 然後對mysql進行交互!像這種靜態的,例如圖片或者html,nginx會直接處理,從而加快訪問速度! 談到速度,其實如果一個普通的站點你是看不到什麽效果的,但是如果要是訪問一個純靜態站點,nginx就能體現出它的優勢!nginx的另外一個優勢得益於它的高並發支持!
和LAMP不同的是,提供web服務的是Nginx
並且php是作為一個獨立服務存在的,這個服務叫做php-fpm
Nginx直接處理靜態請求,動態請求會轉發給php-fpm
2.MYSQL 安裝過程
2.1 獲取安裝包,並解壓
[root@xavi ~] ps aux |grep mysql //安裝前先確定mysql是否開啟 root 1082 0.0 0.0 115392 1672 ? S 21:09 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/xavi.pid mysql 1375 3.8 24.2 1302876 452908 ? Sl 21:09 0:03 /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/xavi.err --pid-file=/data/mysql/xavi.pid root 2645 0.0 0.0 112680 976 pts/0 R+ 21:10 0:00 grep --color=auto mysql [root@xavilinux01 ~]# cd /usr/local/src [root@xavi src]# rm -rf /usr/local/mysql //把之前的刪除 [root@xavi src]# rm -rf /etc/init.d/mysqld [root@xavilinux01 src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz [root@xavilinux01 src]# tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz [root@xavilinux01 src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql [root@xavi src]# ls /usr/local/mysql/ bin data include man README share support-files COPYING docs lib mysql-test scripts sql-bench
2.2 進入mysql目錄下,刪除之前/data/mysql/下的文件
[root@xavi ~]# cd /usr/local/mysql
[root@xavi mysql]# ls /data/
mariadb mysql wwwroot
[root@xavi mysql]# id mysql
uid=1004(mysql) gid=1005(mysql) 組=1005(mysql)
[root@xavi mysql]# rm -rf /data/mysql/*
2.3 配置文件./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
出現兩個OK
2.4 拷貝編輯cp support-files/my-default.cnf /etc/my.cnf
2.5 拷貝啟動腳本並編輯 cp support-files/mysql.server /etc/init.d/mysqld
- 編輯 vim /etc/init.d/mysqld
vim編輯下面兩行basedir和datadir配置
basedir=/usr/local/mysql
datadir=/data/mysql
2.6 啟動mysqld,/etc/init.d/mysqld start
報錯:
root@xavi mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to ‘/data/mysql/xavi.err‘.
.. ERROR! The server quit without updating PID file (/data/mysql/xavi.pid).
sql啟動報錯參考:http://blog.51cto.com/732233048/1636409
2.7 kill -9 PID 殺死占用mysql的進程,然後開啟
查找出之前報錯的原因,mysql進程沒有關閉
[root@xavi mysql]# ps -ef|grep mysqld
root 1082 1 0 21:09 ? 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/xavi.pid
mysql 1375 1082 0 21:09 ? 00:00:13 /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/xavi.err --pid-file=/data/mysql/xavi.pid
root 4433 2598 0 22:23 pts/0 00:00:00 grep --color=auto mysqld
[root@xavi mysql]# kill -9 1082
[root@xavi mysql]# ps -ef|grep mysqld
mysql 1375 1 0 21:09 ? 00:00:13 /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/xavi.err --pid-file=/data/mysql/xavi.pid
root 4444 2598 0 22:23 pts/0 00:00:00 grep --color=auto mysqld
[root@xavi mysql]# kill -9 1375
[root@xavi mysql]# ps -ef|grep mysqld
root 4447 2598 0 22:24 pts/0 00:00:00 grep --color=auto mysqld
[root@xavi mysql]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
將mysql添加到開機啟動項
[root@xavi mysql]# chkconfig --add mysqld
[root@xavi mysql]# chkconfig mysqld on
[root@xavi mysql]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@xavi mysql]# service mysqld start
Starting MySQL. SUCCESS!
12.3 PHP安裝
和LAMP安裝PHP方法有差別,需要開啟php-fpm服務
1. 配置並編譯:
進入src目錄下,查看之前已經下載了哪些安裝包
[root@xavi src]cd /usr/local/src/
[root@xavi src]# ls
apr-1.6.3
apr-1.6.3.tar.gz
apr-util-1.6.1
apr-util-1.6.1.tar.gz
httpd-2.4.29
httpd-2.4.29.tar.gz
mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz
mysql-5.5.55
mysql-5.5.55.tar.gz
mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
mysql-5.6.36-linux-glibc2.5-x86_64.tar.gz
php-5.6.30
php-5.6.30.tar.bz2
php-5.6.30.tar.gz
php-5.6.30.tar.gz.1
php-7.1.6
php-7.1.6.tar.bz2
phpredis-develop
phpredis-develop.zip
打開php-5.6.50源碼包(之前LAMP架構中已經下載),make clean
[root@xavi src]# cd php-5.6.30/
[root@xavi php-5.6.30]# ls
[root@xavi php-5.6.30]# make clean
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp5.la sapi/cli/php sapi/cgi/php-cgi libphp5.la modules/* libs/*
將這裏用的php命名為php-fpm,添加配置文件
[root@xavi php-5.6.30]# ls /usr/local/
apache2.4 apr-util etc include lib64 mariadb php sbin src
apr bin games lib libexec mysql php7 share
[root@xavi php-5.6.30]#useradd -s /sbin/nologin php-fpm
[root@xavi 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
--prefix=指定安裝位置
--with-config-file-path=指定配置文件目錄
--enable-fpm=啟動fpm服務
--with-fpm-user=指定用戶
--with-fpm-group=指定用戶組
--with-mysql=指定mysql的路徑
報錯處理,安裝devel安裝包,安裝完重新加載配置文件,然後make&make install
<curl-dir>
configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/
- 安裝 :yum install libcurl-devel
3 PHP信息:
[root@xavi php-5.6.30]# ls /usr/local/php-fpm
bin etc include lib php sbin var
[root@xavi php-5.6.30]# ls /usr/local/php
bin etc include lib php
安裝下來之後我們發現其實比之前安裝PHP要多了兩個目錄 sbin 和 var
sbin:實際上就是啟動php-fpm服務的目錄
var:實際上就是存放PHP日誌的,當然這個我們可以指定的
查看PHP信息:/usr/local/php-fpm/sbin/php-fpm -i
查看PHP模塊:/usr/local/php-fpm/sbin/php-fpm -m
測試PHP配置語法:/usr/local/php-fpm/sbin/php-fpm -t
4. 配置PHP:
cp php.ini-production /usr/local/php-fpm/etc/php.ini
//牢記此命令,後期會經常使用到
4.1 編輯:vi /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 =127.0.0.1:9000
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]=定義全局參數
[www]=自定義模塊
listen = 監聽的地址
或者可以使用這樣的方式↓
listen = 127.0.0.1:9000 (port默認為9000,也可以更改)
listen.mode = 666 用來定義listen = /tmp/php-fcgi.sock的權限,只有這個sock打開的情況下才生效!
4.2拷貝啟動腳本,先到源碼包目錄,腳本為sapi/fpm/init.d.php-fpm
[root@xavi php-5.6.30]# cd /usr/local/src/php-5.6.30/
[root@xavi php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
賦予權限,修改開機啟動項
[root@xavi php-5.6.30]# chmod 755 /etc/init.d/php-fpm
[root@xavi php-5.6.30]# chkconfig --add php-fpm
[root@xavi php-5.6.30]# chkconfig php-fpm on
[root@xavi php-5.6.30]# service php-fpm start
Starting php-fpm done
LNMP基礎架構