LNMP簡介
lamp(linux apache mysql php)—比較早的web服務。
LNMP(Linux nginx mysql php)—比較潮流,新的webn服務,使用率上升趨勢(國內非常流行)。
?
立即學習“PHP免費學習筆記(深入)”;
??Nginx(Engine X),是俄羅斯人開發的,開源的www服務軟件,軟件一共780K(非常小),是一款靜態(html,js,css,jpg等)www軟件,靜態小文件高并發,同時占用資源很少。
?
立即學習“PHP免費學習筆記(深入)”;
??Nginx使用平臺(linux unix windows都可以)和apache一樣。
?
立即學習“PHP免費學習筆記(深入)”;
Nginx的功能:
a.www web服務—http 80
b.負載均衡(反向代理proxy)
c.web cache
?
立即學習“PHP免費學習筆記(深入)”;
nginx的優點:
1.配置簡單,靈活。
2.高并發(靜態小文件),靜態1-2W。
3.占用資源少,2W并發開10個線程服務,內存消耗幾百兆。
4.功能種類比較多(web cache proxy代理),每個功能都不是很強。
5.支持epoll模型,使得nginx支持高并發。
6.nginx配合動態服務和apache有區別。
7.利用nginx可以對IP限速。可以限制連接數。
?
立即學習“PHP免費學習筆記(深入)”;
實現原理:nginx(apache)—》php–》mysql
?
立即學習“PHP免費學習筆記(深入)”;
nginx的應用場合:
1.靜態服務(圖片,視頻),另一個lighttpd(社區不活躍)。并發:1-3W,html、js、css、.flv
2.動態服務:nginx+fastcgi方式運行php,jsp。并發:500-1500,類似于apache+php,lighttpd+fcgi php
3.反向代理,負載均衡。日PV2000W以下,都可以用nginx做代理。haproxy,F5,a10
4.緩存服務,SQUID,VARNI.
?
立即學習“PHP免費學習筆記(深入)”;
nginx和其它web服務器對比:
1)apache
a.穩定強大
b.Prefork模式取消了進程創建開銷,性能很高。
c.處理動態業務數據時,因關聯到后端的引擎和數據庫,瓶頸不在apache本身
d.高并發時消耗系統資源相對來說多一些
e.基于傳統的select模型
2)nginx
a.基于異步IO模型(epoll,kqueue),性能強,能夠支持上萬并發
b.對小文件支持很好,性能很高(限靜態小文件)
c.代碼優美,擴展庫必須編譯進行主程序
d.消耗資源相對低
3)Lighttpd(百度貼吧,豆瓣)
a.基于異步IO模型,性能和nginx相近
b.擴展是SO模式,比nginx要靈活
c.全球使用率較低,安全性沒有apache以及nginx好
d.通過插件(mod secdownload)可實現文件URL地址加密
?
立即學習“PHP免費學習筆記(深入)”;
企業生產場景如何選擇web服務器(推薦):
靜態業務:高并發,用nginx或lighttpd
動態業務:采用nginx和apache均可
既有動態又有靜態業務:nginx和apache都可以,單選
對外業務nginx,對內業務apache
?
立即學習“PHP免費學習筆記(深入)”;
安裝,配置nginx
1.安裝準備:[root@localhost ~]# yum install pcre* -y
??????????[root@localhost ~]# yum install openssl* -y
2.編譯:
./configure –prefix=/application/nginx1.12.0
–user=nginx
–group=nginx
–with-http_stub_status_module
–with-http_ssl_module
3.啟動檢查:
[root@localhost application]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx1.12.0/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx1.12.0/conf/nginx.conf test is successful
[root@localhost application]# /application/nginx/sbin/nginx
[root@localhost application]# netstat -lntup|grep nginx(另外80端口也可以查)
?
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
出現這個界面表示成功
[root@localhost application]# vim /application/nginx/html/index.html ??修改主頁內容
?
立即學習“PHP免費學習筆記(深入)”;
4.關于nginx模塊
1)Nginx core modules(必須的),包括Main Events
2)Standard HTTP modules(雖然不是必須的,但是缺省都會安裝,不建議改動),包括Core ?Acess ?FastCGI ?Gzip(壓縮模塊,性能優化) ?LOG(日志模塊) ?Proxy Rewrite(URL重寫模塊) ?Upstream(負載均衡模塊)等等
提示:缺省條件下,這兩組模塊都會安裝。修改配置文件,主要是根據這2組模塊的功能來修改模塊的參數。官網能夠查看模塊的參數。
?
立即學習“PHP免費學習筆記(深入)”;
5.nginx的配置
[root@localhost conf]# egrep -v “#|^$” nginx.conf.default >nginx.conf(去掉注釋#行)
[root@localhost conf]# vim nginx.conf(cairui.org是別名)
?
?
立即學習“PHP免費學習筆記(深入)”;
[root@localhost conf]# mkdir /application/nginx/html/www
[root@localhost conf]# touch /application/nginx/html/www/index.html
[root@localhost conf]# vim /application/nginx/html/www/index.html
[root@localhost conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx1.12.0/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx1.12.0/conf/nginx.conf test is successful
[root@localhost conf]# /application/nginx/sbin/nginx -s reload
修改windows本地hosts文件,保證在win命令提示符內能ping通來自虛擬主機,再上網頁查看
?
立即學習“PHP免費學習筆記(深入)”;
添加狀態的server標簽:
[root@localhost conf]# vim /application/nginx/conf/nginx.conf
?
?
立即學習“PHP免費學習筆記(深入)”;
##status
server {
????????listen ??????80;
????????server_name ?status.cairui.org;
???????????location / {
?????????????????stub_status on;
?????????????????access_log ?off;
}
}
在win本地hosts文件中也要添加,
?
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
解決惡意域名綁定:
[root@localhost conf]# vim /application/nginx/conf/nginx.conf(必須放在第一個server)
server {
????????listen ??????80;
???????????location / {
???????????deny all;
?
立即學習“PHP免費學習筆記(深入)”;
}
????????????
}
?
立即學習“PHP免費學習筆記(深入)”;
開啟日志功能:
[root@localhost conf]# cat /application/nginx/conf/nginx.conf.default (找到日志格式)
?
立即學習“PHP免費學習筆記(深入)”;
????#log_format ?main ?‘$remote_addr – $remote_user [$time_local] “$request” ‘
????# ?????????????????‘$status $body_bytes_sent “$http_referer” ‘
????# ?????????????????‘”$http_user_agent” “$http_x_forwarded_for”‘;
?
立即學習“PHP免費學習筆記(深入)”;
????#access_log ?logs/access.log ?main;
拷貝到nginx.conf下,去掉注釋
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
錯誤日志格式放在最頂端
分析nginx日志的軟件:syslog,rsyslog,Awstats,scribe,kafka
?
立即學習“PHP免費學習筆記(深入)”;
Mysql
mysql安裝方法:
5.1—編譯安裝configure,make,make install
5.5—編譯安裝cmake,make,make install(可以直接用二進制包安裝,解壓即可)
?
立即學習“PHP免費學習筆記(深入)”;
1.安裝mysql
[root@localhost conf]# useradd mysql -s /sbin/nologin -M
[root@localhost tools]# tar xf mysql-5.5.32-linux2.6-x86_64.tar.gz
[root@localhost tools]# mv mysql-5.5.32-linux2.6-x86_64 /application/mysql5.5.32
[root@localhost tools]# ln -s /application/mysql5.5.32/ /application/mysql
[root@localhost tools]# chown -R mysql.mysql /application/mysql/data/
[root@localhost mysql]# ./scripts/mysql_install_db –user=mysql –basedir=/application/mysql/ –datadir=/application/mysql/data/(編譯之前需要安裝libaio)
[root@localhost mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@localhost mysql]# vim /etc/init.d/mysqld
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
[root@localhost mysql]# cp support-files/my-small.cnf /etc/my.cnf (每個版本的配置文件不同)
cp:是否覆蓋“/etc/my.cnf”? y
[root@localhost mysql]# vim /etc/profile
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
[root@localhost mysql]# service mysqld start
Starting MySQL.. SUCCESS!
[root@localhost mysql]# mysql
數據庫優化:
mysql> show databases
mysql> drop Database test;
mysql> select user,host from mysql.user;
mysql> drop user”@localhost;
[root@localhost mysql]# mysql -uroot -pnew-password
修改密碼(進入mysql修改)mysql> set password for root@localhost = password(‘123’);
?
立即學習“PHP免費學習筆記(深入)”;
PHP
LNMP下的php:
apache—-》libphp5.so
nginx—》fcgi php-fpm port9000(百度)
在安裝php之前確保mysql,nginx運行正常[root@localhost mysql]# netstat -lntup|egrep “nginx|mysql”
1.需要安裝的軟件
安裝libiconv(./configure –prefix=/usr/local/libiconv)
?
立即學習“PHP免費學習筆記(深入)”;
安裝libmcrypt(不是必須,建議安裝)是一個加密的庫
./configure
make
make install
sleep2
/sbin/ldconfig
cd libltdl/
./configure –enable-ltdl-install
make
make install
cd ../../cd
?
立即學習“PHP免費學習筆記(深入)”;
安裝mhash
./configure
make
make install
sleep 2
cd ../
?
立即學習“PHP免費學習筆記(深入)”;
rm -fr /usr/lib64/libmcrypt.*
rm -fr /usr/lib64/libmhash*
ln -s /usr/local/lib64/libmcrypt.la /usr/lib64/libmcrypt.la
ln -s /usr/local/lib64/libmcrypt.so /usr/lib64/libmcrypt.so
ln -s /usr/local/lib64/libmcrypt.so.4 /usr/lib64/libmcrypt.so.4
ln -s /usr/local/lib64/libmcrypt.so.4.4.8 /usr/lib64/libmcrypt.so.4.4.8
ln -s /usr/local/lib64/libmhash.a /usr/lib64/libmhash.a
ln -s /usr/local/lib64/libmhash.la ?/usr/lib64/libmhash.la
ln -s /usr/local/lib64/libmhash.so ?/usr/lib64/libmhash.so
ln -s /usr/local/lib64/libmhash.so.2 /usr/lib64/libmhash.so.2
ln -s /usr/local/lib64/libmhash.so.2.0.1 /usr/lib64/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config
?
立即學習“PHP免費學習筆記(深入)”;
安裝mcrypt
/sbin/ldconfig
./configure LD_LIBRARY_PATH=/usr/local/lib
make
make install
cd ../
sleep 2
?
立即學習“PHP免費學習筆記(深入)”;
2.安裝php
[root@localhost tools]# yum install libxslt* -y
./configure ?
–prefix=/application/php5.6.30
–with-mysql=/application/mysql
–with-iconv-dir=/usr/local/libiconv
–with-freetype-dir
–with-jpeg-dir
–with-png-dir
–with-zlib
–with-libxml-dir=/usr
–enable-xml
–disable-rpath
–enable-safe-mode
–enable-bcmath
–enable-shmop
–enable-sysvsem
–enable-inline-optimization
–with-curl
–with-curlwrappers
–enable-mbregx
–enable-fpm
–enable-mbstring
–with-mcrypt
–with-gd
–enable-gd-native-ttf
–with-openssl
–with-mhash
–enable-pcntl
–enable-sockets
–with-xmlrpc
–enable-zip
–enable-soap
–enable-short-tags
–enable-zend-multibyte
–enable-static
–with-xsl
–with-fpm-user=nginx
–with-fpm-group=nginx
–enable-ftp
–enable-opcache=no
ln -s /application/mysql/lib/libmysqlclient.so.18 /usr/lib64
報錯處理:,,
?
立即學習“PHP免費學習筆記(深入)”;
3.php配置
[root@localhost php-5.3.27]# cp php.ini-production /application/php/lib/php.ini
?
立即學習“PHP免費學習筆記(深入)”;
直接上傳
[root@localhost etc]# /application/php/sbin/php-fpm -t
[root@localhost etc]# /application/php/sbin/php-fpm
開機啟動
cat >>/etc/rc.local
#nginx+php-fpm by cairui at 2017
/etc/init.d/mysqld start
/application/php/sbin/php-fpm
/application/nginx/sbin/nginx
EOF
?
立即學習“PHP免費學習筆記(深入)”;
[root@134 extra]# cat /application/nginx/conf/extra/bbs.conf
server {
????????listen ??????80;
????????server_name ?bbs.caimengzhi.org;
????????root ????????html/bbs;
????????index ????????index.html index.htmi index.php;
???????location ~ .*.(php|php5)?$ ????#此時不{}內不指定目錄的話就直接讀取上面html/bbs下的php文件
????????{
????????????fastcgi_pass ??127.0.0.1:9000;
????????????fastcgi_index ?index.php;
????????????include ???????fastcgi.conf;
????????}
????}
?
立即學習“PHP免費學習筆記(深入)”;
[root@localhost conf]# cd /application/nginx/html/www/
[root@localhost www]# vim index.php
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
[root@localhost www]# /application/nginx/sbin/nginx -t
[root@localhost www]# /application/nginx/sbin/nginx -s reload
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
?
立即學習“PHP免費學習筆記(深入)”;
wiki軟件
[root@localhost www]# mysql
mysql> create database wiki;
mysql> grant all on wiki.* to wiki@’localhost’ identified by ‘wiki’;
mysql> flush privileges ;
mysql> quit
(wiki下載HDwiki)
[root@localhost www]# wget (注意www)
[root@localhost www]# unzip download.php?n=HDWiki-v6.0GBK-20170209.zip
[root@localhost www]# rm -f download.php?n=HDWiki-v6.0GBK-20170209.zip
[root@localhost www]# mv hdwiki/* .
[root@localhost www]# cd ../
[root@localhost html]# chown -R nginx.nginx www/