目录
一、 LNMP架构介绍
二、 MySQL安装
三、 PHP安装
四、 Nginx介绍
一、 LNMP架构介绍
LNMP和LAMP唯一不同的是,LNMP中的N指的是Nginx(类似于Apache的一种Web服务软件)。在静态页面的处理上,Nginx较Apache更胜一筹;但在动态页面的处理上,Nginx并不比Apache有优势。在LNMP架构中,PHP会作为一个独立的服务存在,这个服务叫php-fpm,nginx会将用户请求的php交个php-fpm处理。Nginx可以支持较大的用户并发数。
二、 MySQL安装
将原先的Mysql删除再重新安装一遍(简单描述):
[root@minglinux-01 ~] ps aux|grep mysql
root 898 0.0 0.0 115640 1828 ? S 00:51 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/minglinux-01.pid
mysql 1282 0.0 24.5 1302740 457260 ? Sl 00:51 1:05 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=minglinux-01.err --pid-file=/data/mysql/minglinux-01.pid --socket=/tmp/mysql.sock --port=3306
root 10063 0.0 0.0 112724 984 pts/0 S+ 22:42 0:00 grep --color=auto mysql
[root@minglinux-01 ~] systemctl stop mysql //停止mysqld服务再删除
[root@minglinux-01 ~] ps aux|grep mysql
root 10093 0.0 0.0 112720 984 pts/0 S+ 22:43 0:00 grep --color=auto mysql
[root@minglinux-01 ~] rm -rf /usr/local/mysql
[root@minglinux-01 ~] rm -rf /etc/init.d/mysqld
[root@minglinux-01 ~] cd /usr/local/src/
[root@minglinux-01 /usr/local/src] wget http://mirrors.163.com/mysql/Downloads/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
[root@minglinux-01 /usr/local/src] tar zxvf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
[root@minglinux-01 /usr/local/src] mv mysql-5.6.39-linux-glibc2.12-x86_64 /usr/local/mysql
[root@minglinux-01 /usr/local/src] ls /usr/local/mysql/
bin docs man scripts support-files
COPYING include mysql-test share
data lib README sql-bench
[root@minglinux-01 /usr/local/src] cd /usr/local/mysql
[root@minglinux-01 /usr/local/mysql] rm -rf /data/mysql/*
[root@minglinux-01 /usr/local/mysql] id mysql
uid=1011(mysql) gid=1011(mysql) 组=1011(mysql)
[root@minglinux-01 /usr/local/mysql] ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql //初始化
[root@minglinux-01 /usr/local/mysql] echo $?
0 //判断初始化是否成功
[root@minglinux-01 /usr/local/mysql] vim /etc/my.cnf //etc目录已经存在my.cnf,不必再复制
[root@minglinux-01 /usr/local/mysql] cp support-files/mysql.server /etc/init.d/mysqld //复制启动脚本
[root@minglinux-01 /usr/local/mysql] vim !$
vim /etc/init.d/mysqld //编辑启动脚本修改以下内容
···
basedir=/usr/local/mysql
datadir=/data/mysql
···
[root@minglinux-01 /usr/local/mysql] /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/minglinux-01.err'.
. SUCCESS!
[root@minglinux-01 /usr/local/mysql] ps aux|grep mysqld
root 10276 0.1 0.0 113304 1620 pts/0 S 23:09 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/minglinux-01.pid
mysql 10513 3.0 24.1 1302728 450044 pts/0 Sl 23:09 0:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=minglinux-01.err --pid-file=/data/mysql/minglinux-01.pid --socket=/tmp/mysql.sock --port=3306
root 10544 0.0 0.0 112720 984 pts/0 S+ 23:10 0:00 grep --color=auto mysqld
[root@minglinux-01 /usr/local/mysql] chkconfig --add mysqld //将mysqld加入服务列表
[root@minglinux-01 /usr/local/mysql] chkconfig mysqld on //设置开机启动
[root@minglinux-01 /usr/local/mysql] service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@minglinux-01 /usr/local/mysql] service mysqld start
Starting MySQL. SUCCESS!
三、 PHP安装
Nginx的PHP安装和上一章的PHP安装是有区别的。因为Nginx中的PHP
是以fastcgi的方式结合Nginx的,可以理解为Nginx代理了PHP的fastcgi,而httpd是把PHP作为自己的模块来调用的,在lnmp里面php作为一个独立的服务存在,与nginx无直接关系。
- 删除安装LAMP时编译的文件
[root@minglinux-01 /usr/local/src/php-5.6.30] pwd
/usr/local/src/php-5.6.30
[root@minglinux-01 /usr/local/src/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-5.6.30目录就是刚解压时状态。
- 生成可编译文件
[root@minglinux-01 /usr/local/src/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
报错:
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
easy.h should be in <curl-dir>/include/curl/
解决:yum install -y libcurl-devel
[root@minglinux-01 /usr/local/src/php-5.6.30] yum install -y libcurl-devel
[root@minglinux-01 /usr/local/src/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
//再次执行
- 编译安装
[root@minglinux-01 /usr/local/src/php-5.6.30] make
[root@minglinux-01 /usr/local/src/php-5.6.30] make install
- 查看文件目录
[root@minglinux-01 /usr/local/src/php-5.6.30] ls /usr/local/php-fpm
bin etc include lib php sbin var
[root@minglinux-01 /usr/local/src/php-5.6.30] ls /usr/local/php
bin etc include lib php
[root@minglinux-01 /usr/local/src/php-5.6.30] ls /usr/local/php-fpm/sbin
php-fpm
[root@minglinux-01 /usr/local/src/php-5.6.30] ls /usr/local/php-fpm/var/
log run
php-fpm中比php多的两个目录sbin和var
sbin目录下的php-fpm文件用于启动php-fpm服务
var目录下log目录用于存放日志,run目录用于存放pid,日志和pid的存放路径都可以单独指定。
- 查看php-fpm包含的模块
[root@minglinux-01 /usr/local/src/php-5.6.30] /usr/local/php-fpm/sbin/php-fpm -m
[PHP Modules]
cgi-fcgi
Core
ctype
curl
date
dom
ereg
exif
fileinfo
filter
ftp
gd
hash
iconv
json
libxml
mbstring
mcrypt
mysql
mysqli
openssl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
SimpleXML
soap
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib
[Zend Modules]
- 查看php-fpm配置文件语法
[root@minglinux-01 /usr/local/src/php-5.6.30] /usr/local/php-fpm/sbin/php-fpm -t
[24-Nov-2018 00:00:35] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[24-Nov-2018 00:00:35] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[24-Nov-2018 00:00:35] ERROR: FPM initialization failed
/usr/local/php-fpm/etc/php-fpm.conf配置文件不存在
- 配置php-fpm服务
[root@minglinux-01 /usr/local/src/php-5.6.30] cp php.ini-production /usr/local/php-fpm/etc/php.ini //拷贝php的模板配置文件到php-fpm配置文件路径下
模板配置文件:
php.ini-development:开发环境下使用
php.ini-production:生产环境下使用,在错误日志处与php.ini-development不同
[root@minglinux-01 /usr/local/php-fpm/etc] vim php-fpm.conf //新建php-fpm的配置文件并写入以下内容
[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 监听的socket地址
#listen = 127.0.0.1:9000 //php-fpm默认监听的端口为9000
listen.mode = 666 //当监听的是socket地址时生效,用于定义socket文件的权限
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@minglinux-01 /usr/local/php-fpm/etc] cd /usr/local/src/php-5.6.30
[root@minglinux-01 /usr/local/src/php-5.6.30] cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm //拷贝php-fpm启动脚本
[root@minglinux-01 /usr/local/src/php-5.6.30] chmod 755 /etc/init.d/php-fpm //修改启动脚本权限
[root@minglinux-01 /usr/local/src/php-5.6.30] chkconfig --add php-fpm //将php-fpm服务加入服务列表
[root@minglinux-01 /usr/local/src/php-5.6.30] chkconfig php-fpm on //将php-fpm设为开机启动
[root@minglinux-01 /usr/local/src/php-5.6.30] /usr/local/php-fpm/sbin/php-fpm -t
[24-Nov-2018 00:21:59] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful
//检测配置是否正确
[root@minglinux-01 /usr/local/src/php-5.6.30] service php-fpm start
Starting php-fpm [24-Nov-2018 00:23:32] ERROR: [pool www] cannot get uid for user 'php-fpm' //报错php-fpm用户不存在
[24-Nov-2018 00:23:32] ERROR: FPM initialization failed
failed
[root@minglinux-01 /usr/local/src/php-5.6.30] useradd php-fpm //创建用户
[root@minglinux-01 /usr/local/src/php-5.6.30] service php-fpm startStarting php-fpm done
[root@minglinux-01 /usr/local/src/php-5.6.30] ps aux|grep php-fpm
root 126762 0.0 0.2 123692 4944 ? Ss 00:23 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 126763 0.0 0.2 123692 4700 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126764 0.0 0.2 123692 4700 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126765 0.0 0.2 123692 4700 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126766 0.0 0.2 123692 4700 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126767 0.0 0.2 123692 4704 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126768 0.0 0.2 123692 4704 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126769 0.0 0.2 123692 4704 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126770 0.0 0.2 123692 4704 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126771 0.0 0.2 123692 4704 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126772 0.0 0.2 123692 4704 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126773 0.0 0.2 123692 4708 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126774 0.0 0.2 123692 4708 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126775 0.0 0.2 123692 4708 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126776 0.0 0.2 123692 4708 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126777 0.0 0.2 123692 4708 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126778 0.0 0.2 123692 4708 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126779 0.0 0.2 123692 4708 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126780 0.0 0.2 123692 4708 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126781 0.0 0.2 123692 4708 ? S 00:23 0:00 php-fpm: pool www
php-fpm 126782 0.0 0.2 123692 4708 ? S 00:23 0:00 php-fpm: pool www
root 126784 0.0 0.0 112720 984 pts/0 S+ 00:26 0:00 grep --color=auto php-fpm
[root@minglinux-01 /usr/local/src/php-5.6.30] ls -l /tmp/php-fcgi.sock
srw-rw-rw- 1 root root 0 11月 24 00:23 /tmp/php-fcgi.sock
四、 Nginx介绍
- Nginx官网 nginx.org,最新版1.13,最新稳定版1.12
- Nginx的静态文件处理能力比apache强很多,Nginx支持扩展第三方模块(如支持https等)
- Nginx应用场景:web服务、反向代理、负载均衡
- Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并
- Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928
扩展
Nginx为什么比Apache Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html
apache和nginx工作原理比较 http://www.server110.com/nginx/201402/6543.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
网友评论