1 LNMP架构介绍
2 MySQL安装
3 PHP安装
4 Nginx介绍
5 Nginx安装
扩展
Nginx为什么比Apache Httpd高效:原理篇 http://www.toxingwang.com/linux-unix/linux-basic/1712.html
https://coding.net/u/aminglinux/p/nginx/git/blob/master/4z/IO.md?public=true
https://www.zhihu.com/question/64727674
apache和nginx工作原理比较 http://www.server110.com/nginx/201402/6543.html
概念了解:CGI,FastCGI,PHP-CGI与PHP-FPM https://www.awaimai.com/371.html
1 LNMP架构介绍
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。Nginx中的PHP是以fastcgi的方式结合Nginx的,可以理解为Nginx代理了PHP的fastcgi。
LNMP工作模式
- 和LAMP不同的是,LNMP中提供web服务的是Nginx。
- 在LNMP架构中PHP是作为一个独立的服务存在的,这个服务叫做php-fpm。
- Nginx直接处理静态请求(支持的并发更高,速度比Apache快),动态请求转发给php-fpm处理。
2 安装MySQL
卸载MySQL
直接删除MySQL安装时的相关文件。
安装MySQL
在LNMP环境和LAMP环境安装MySQL的方法一样。
3 PHP安装
和LAMP安装PHP有区别,需要开启php-fpm服务。
准备工作
安装包卸载&解压
[root@adailinux ~]# cd /usr/local/src/
下载安装包:
[root@adailinux src]# wget http://cn2.php.net/distributions/php-5.6.30.tar.gz
解压:
[root@adailinux src]# tar zxvf php-5.6.30.tar.gz
创建账号
[root@adailinux src]# useradd -s /sbin/nologin php-fpm
说明: 该账号用来运行php-fpm服务,因为在LNMP环境中,PHP是以一种服务的形式独立存在的。
安装php
如果之前安装过PHP,需要清除其原有配置:
[root@adailinux src]# cd php-5.6.30
[root@adailinux src]# make clean
环境配置
[root@adailinux src]# cd php-5.6.30
[root@adailinux src]# ./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
报错:
错误1:
configure: error: xml2-config not found. Please check your libxml2 installation.
解决办法:
[root@adailinux php-5.6.30]# yum list |grep libxml2
查找相关库的安装包:
[root@adailinux php-5.6.30]# yum list |grep libxml2
libxml2.x86_64 2.9.1-6.el7_2.3 @anaconda
libxml2.i686 2.9.1-6.el7_2.3 base
libxml2-devel.i686 2.9.1-6.el7_2.3 base
libxml2-devel.x86_64 2.9.1-6.el7_2.3 base
libxml2-python.x86_64 2.9.1-6.el7_2.3 base
libxml2-static.i686 2.9.1-6.el7_2.3 base
libxml2-static.x86_64 2.9.1-6.el7_2.3 base
选择包进行安装:
[root@adailinux php-5.6.30]# yum install -y libxml2 libxml2-devel
错误2:
configure: error: Cannot find OpenSSL's <evp.h>
解决办法:
[root@adailinux php-5.6.30]# yum install -y openssl openssl-devel
错误3:
错误4:
configure: error: jpeglib.h not found.
解决办法:
[root@adailinux php-5.6.30]# yum install -y libjpeg libjpeg-turbo-devel
错误5:
configure: error: png.h not found.
解决办法:
[root@adailinux php-5.6.30]# yum install -y libpng libpng-devel
错误6:
configure: error: freetype-config not found.
解决办法:
[root@adailinux php-5.6.30]# yum install -y freetype freetype-devel
错误7:
configure: error: mcrypt.h not found. Please reinstall libmcrypt.
解决办法:
[root@adailinux php-5.6.30]# yum install -y libmcrypt libmcrypt-devel
说明: 每次报错后根据提示查找相应的库的安装包,选择包进行安装,安装完成后重新配置即可!
配置完成后进行检测:
[root@adailinux php-5.6.30]# echo $?
0
总结一下需要安装的包:
[root@adailinux php-5.6.30]# yum install -y gcc gcc-c++ libxml2-devel openssl-devel libcurl-devel libjpeg-devel libpng-devel freetype libmcrypt-devel
编译&安装
编译:
[root@adailinux php-5.6.30]# make
报错: virtual memory exhausted: Cannot allocate memory #虚拟内存耗尽:无法分配内存
解决办法:
1、停掉虚拟机中未在使用的进程
2、临时增加swap分区内存:
[root@adai002 ~]# dd if=/dev/zero of=/tmp/newdisk bs=1M count=100 手动创建一个新分区
记录了100+0 的读入
记录了100+0 的写出
104857600字节(105 MB)已复制,5.72931 秒,18.3 MB/秒
[root@adai002 ~]# du -sh /tmp/newdisk
100M /tmp/newdisk
[root@adai002 ~]# mkswap /tmp/newdisk 格式化该swap分区
正在设置交换空间版本 1,大小 = 102396 KiB
无标签,UUID=d42e907a-aae0-4d5f-bf58-586fac415f48
[root@adai002 ~]# free -m
total used free shared buff/cache available
Mem: 984 114 626 6 244 706
Swap: 2047 0 2047
[root@adai002 ~]# swapon /tmp/newdisk 挂载到原swap分区
swapon: /tmp/newdisk:不安全的权限 0644,建议使用 0600。
[root@adai002 ~]# free -m
total used free shared buff/cache available
Mem: 984 114 626 6 244 707
Swap: 2147 0 2147
[root@adai002 ~]# chmod 0600 /tmp/newdisk 更改权限以保安全
待PHP编译完成后要卸载掉该分区
[root@adai002 ~]# swapoff /tmp/newdisk 卸载分区
安装:
[root@adailinux php-5.6.30]# make install
php-fpm相关命令:
执行PHP相关命令的两种方式:
方法1:
[root@adailinux php-fpm]# /usr/local/php-fpm/sbin/php-fpm -m
方法2:
[root@adailinux php-fpm]# /usr/local/php-fpm/bin/php -m
php-fpm配置文件语法检测:
[root@adailinux php-fpm]# /usr/local/php-fpm/sbin/php-fpm -t
配置
添加配置文件
将配置文件添加到php-fpm配置文件目录:
[root@adailinux php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
配置文件调试
切换至配置文件所在目录:
[root@adailinux php-5.6.30]# cd /usr/local/php-fpm/etc/
手动添加配置文件,写入如下内容:
[root@adailinux etc]# vim 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,本地监听,也可以监听其他IP:port
#此处格式会影响配置Nginx和PHP结合时Nginx寻址PHP的路径
listen.mode = 666
#当监听的为socket文件时该部分才生效,用于指定.sock文件的权限
user = php-fpm
group = php-fpm
#定义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@adailinux etc]# cd /usr/local/src/php-5.6.30
添加启动脚本到系统配置:
[root@adailinux php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
更改文件权限:
[root@adailinux php-5.6.30]# chmod 755 /etc/init.d/php-fpm
添加到系统服务:
[root@adailinux php-5.6.30]# chkconfig --add php-fpm
设置开机启动:
[root@adailinux php-5.6.30]# chkconfig php-fpm on
启动php-fpm服务:
[root@adailinux php-5.6.30]# service php-fpm start
Starting php-fpm done
4 Nginx介绍
Nginx("engine x")是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、新浪、网易、腾讯、 淘宝等。
Nginx应用场景:
web服务器、反向代理、负载均衡
一、Nginx安装
4、编写Nginx启动脚本并加入系统服务和开机启动
[root@sdwaqw nginx-1.12.2]# 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=1esacexit $RETVAL
[root@sdwaqw nginx-1.12.2]# chmod 755 /etc/init.d/nginx
[root@sdwaqw nginx-1.12.2]# chkconfig --add nginx //加入系统服务
[root@sdwaqw nginx-1.12.2]# chkconfig nginx on //开机启动
4、更改Nginx配置文件
[root@sdwaqw nginx-1.12.2]# > /usr/local/nginx/conf/nginx.conf //重定向符号>,单独使用时,可以吧一个文本文档快速清空
[root@sdwaqw nginx-1.12.2]# vim /usr/local/nginx/conf/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;
}
}
}
[root@sdwaqw nginx-1.12.2]# service nginx start //启动nginx
[root@sdwaqw nginx-1.12.2]# ps aux |grep nginx
root 14119 0.0 0.0 20500 628 ? Ss 14:54 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 14120 0.0 0.1 22944 3212 ? S 14:54 0:00 nginx: worker process
nobody 14121 0.0 0.1 22944 3212 ? S 14:54 0:00 nginx: worker process
root 14123 0.0 0.0 112680 968 pts/0 R+ 14:54 0:00 grep --color=auto nginx