LNMP架构介绍
LNMP=linux+nginx+mysql+php
和LAMP不同的是,提供web服务的是Nginx
并且php是作为一个独立服务存在的,这个服务叫做php-fpm
Nginx直接处理静态请求,动态请求会转发给php-fpm
mysql安装
- Mysql安装
- 将二进制源码包传输到/usr/local/src目录下
[root@zhounan src]# ls
mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
- 解压压缩包
[root@zhounan src]# tar -zxvf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
- 将解压后文件移动到对应文件夹下并改名
[root@zhounan src]# mv mysql-5.6.39-linux-glibc2.12-x86_64/ /usr/local/mysql
- 创建用户组与用户
[root@zhounan src]# groupadd mysql #创建mysql用户组
[root@zhounan src]# useradd -g mysql mysql #创建用户mysql并加入到mysql用户组中
[root@zhounan src]# cd /usr/local/mysql/ #进入mysql程序主目录下
[root@zhounan mysql]# chown -R mysql . #当前目录授权给MySQL用户
[root@zhounan mysql]# chgrp -R mysql . #当前目录授权给MySQL用户组
- 安装mysql
[root@zhounan mysql]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
/usr/local/mysql/scripts/mysql_install_db 安装程序路径
–user= 指定用户
–basedir= 指定安装路径
–datadir= 指定数据库路径
- 配置启动服务
[root@zhounan mysql]# cp support-files/my-default.cnf /etc/my.cnf #拷贝配置文件
[root@zhounan mysql]# cp support-files/mysql.server /etc/init.d/mysqld #拷贝启动脚本
[root@zhounan mysql]# vim /etc/init.d/mysqld #修改启动脚本
#找到如下行修改
basedir=/usr/local/mysql #安装路径
datadir=/usr/local/mysql/data #数据库存放路径
- 加入系统服务
[root@zhounan data]# chkconfig --add mysqld
[root@zhounan data]# chkconfig --list #查看其否成功
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysqld 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
- 启动mysql服务
[root@zhounan data]# service mysqld start
SUCCESS! #提示success则成功启动服务
[root@zhounan mysql]# /usr/local/mysql/bin/mysqld_safe --defaults-file=/etc/my.cnf --user=mysql --datadir=/data/mysql & #命令行方式启动mysql
- 注意事项
如登录mysql时提示未找到命令则需要建立软链接
[root@zhounan mysql]# ln -s /usr/local/mysql/bin/mysql /usr/bin/
/usr/local/mysql/bin/mysql #mysql安装路径下mysql命令路径
- 设置root密码
set password = password('your password');
安装php5.6
- 上传文件到对应目录下解压并移动到对应目录下
[root@zhounan php-5.6]# cd /usr/local/src/
[root@zhounan src]# ls php-5.6.32.tar.bz2
php-5.6.32.tar.bz2
[root@zhounan src]# tar -jxvf php-5.6.32.tar.bz2
[root@zhounan src]# cd /usr/local/php-5.6/
[root@zhounan php-5.6]# useradd -s /sbin/nologin php-fpm
- 安装php
[root@zhounan php-5.6]# ./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
- 编译PHP
make && make install
- php相关信息
配置文件
[root@zhounan1 php-5.6.32]# ls /usr/local/php-fpm/sbin/
php-fpm
使用-m查看拓展
[root@zhounan1 php-5.6.32]# /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
使用-i查看配置
[root@zhounan1 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -i
phpinfo()
PHP Version => 5.6.32
System => Linux zhounan1 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64
Build Date => Dec 12 2018 23:18:34
Configure Command => './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'
Server API => FPM/FastCGI
Virtual Directory Support => disabled
Configuration File (php.ini) Path => /usr/local/php-fpm/etc
Loaded Configuration File => /usr/local/php-fpm/etc/php.ini
Scan this dir for additional .ini files => (none)
Additional .ini files parsed => (none)
PHP API => 20131106
PHP Extension => 20131226
Zend Extension => 220131226
Zend Extension Build => API220131226,NTS
PHP Extension Build => API20131226,NTS
Debug Build => no
Thread Safety => disabled
Zend Signal Handling => disabled
Zend Memory Manager => enabled
使用-t检查配置文件
[root@zhounan1 php-5.6.32]# /usr/local/php-fpm/sbin/php-fpm -t
[14-Dec-2018 21:38:08] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[14-Dec-2018 21:38:08] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[14-Dec-2018 21:38:08] ERROR: FPM initialization failed
- 复制配置文件
[root@zhounan1 php-5.6.32]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
配置php-fpm.conf文件
程序本身无该文件,需要手动添加
[root@zhounan1 php-5.6.32]# vim /usr/local/php-fpm/etc/php-fpm.conf
[global]
pid = /usr/local/php-fpm/var/run/php-fpm.pid #定义pid目录
error_log = /usr/local/php-fpm/var/log/php-fpm.log #定义log目录
[www]
listen = /tmp/php-fcgi.sock #定义sock目录
listen = 127.0.0.1:9000 #定义鉴定ip和端口
listen.mode = 666 #定义sock文件权限
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@zhounan1 php-5.6.32]# chmod 755 /etc/init.d/php.fpm
[root@zhounan1 php-5.6.32]# chkconfig --add php.fpm
[root@zhounan1 php-5.6.32]# chkconfig --list php.fpm
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
php.fpm 0:关 1:关 2:开 3:开 4:开 5:开 6:关
- 启动程序
[root@zhounan1 php-5.6.32]# service php.fpm start
Starting php-fpm done
[root@zhounan1 php-5.6.32]# ps aux |grep php-fpm
root 3601 0.0 0.2 125772 4960 ? Ss 22:05 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 3602 0.0 0.2 127856 5080 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3603 0.0 0.2 127856 5080 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3604 0.0 0.2 127856 5080 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3605 0.0 0.2 127856 5080 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3606 0.0 0.2 127856 5084 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3607 0.0 0.2 127856 5084 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3608 0.0 0.2 127856 5088 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3609 0.0 0.2 127856 5088 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3610 0.0 0.2 127856 5088 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3611 0.0 0.2 127856 5088 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3612 0.0 0.2 127856 5088 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3613 0.0 0.2 127856 5088 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3614 0.0 0.2 127856 5088 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3615 0.0 0.2 127856 5088 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3616 0.0 0.2 127856 5084 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3617 0.0 0.2 127856 5084 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3618 0.0 0.2 127856 5084 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3619 0.0 0.2 127856 5084 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3620 0.0 0.2 127856 5084 ? S 22:05 0:00 php-fpm: pool www
php-fpm 3621 0.0 0.2 127856 5084 ? S 22:05 0:00 php-fpm: pool www
root 3641 0.0 0.0 112680 984 pts/1 S+ 22:06 0:00 grep --color=auto php-fpm
www为程序池名字,在php.fpm中有配置
安装nginx
- nginx介绍
Nginx官网 nginx.org。
Nginx应用场景:web服务、反向代理、负载均衡
Nginx著名分支,淘宝基于Nginx开发的Tengine,使用上和Nginx一致,服务名,配置文件名都一样,和Nginx的最大区别在于Tenging增加了一些定制化模块,在安全限速方面表现突出,另外它支持对js,css合并
Nginx核心+lua相关的组件和模块组成了一个支持lua的高性能web容器openresty,参考http://jinnianshilongnian.iteye.com/blog/2280928 - nginx安装
[root@zhounan1 src]# tar zxf nginx-1.12.2.tar.gz
[root@zhounan1 src]# cd nginx-1.12.2/
[root@zhounan1 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx
[root@zhounan1 nginx-1.12.2]# maske && make install
- 目录介绍
conf 配置文件目录
html 样板目录
logs log目录
sbin pid目录
[root@zhounan1 nginx-1.12.2]# ls /usr/local/nginx/
conf html logs sbin
[root@zhounan1 nginx-1.12.2]# ls /usr/local/nginx/conf/
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@zhounan1 nginx-1.12.2]# ls /usr/local/nginx/html/
50x.html index.html
[root@zhounan1 nginx-1.12.2]# ls /usr/local/nginx/logs/
[root@zhounan1 nginx-1.12.2]# ls /usr/local/nginx/sbin/
nginx
- 支持检查配置文件
[root@zhounan1 nginx-1.12.2]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
- 配置启动脚本
[root@zhounan1 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=1
esac
exit $RETVAL
- 添加进系统服务
[root@zhounan1 nginx-1.12.2]# chmod 755 /etc/init.d/nginx
[root@zhounan1 nginx-1.12.2]# chkconfig --add nginx
[root@zhounan1 nginx-1.12.2]# chkconfig nginx on
- 添加配置文件
不使用原配置文件,使用官网下载的模板
[root@zhounan1 nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@zhounan1 conf]# mv nginx.conf nginx.conf.bak #备份原模板
[root@zhounan1 conf]# ls nginx.conf.bak
nginx.conf.bak
[root@zhounan1 conf]# vim nginx.conf
########复制如下行#############
user nobody nobody; #定义启动用户
worker_processes 2; #定义可以同时启动几个子进程
error_log /usr/local/nginx/logs/nginx_error.log crit; #定义错误log目录
pid /usr/local/nginx/logs/nginx.pid; #定义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;
}
}
}
红线中为添加指定监听ip和端口形式
- 检查配置文件是否有误并启动程序
[root@zhounan1 conf]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@zhounan1 conf]# service nginx start
Starting nginx (via systemctl): [ 确定 ]
[root@zhounan1 conf]# ps aux |grep nginx
root 7188 0.0 0.0 20496 624 ? Ss 23:14 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 7189 0.0 0.1 25024 3248 ? S 23:14 0:00 nginx: worker process
nobody 7190 0.0 0.1 25024 3248 ? S 23:14 0:00 nginx: worker process
root 7203 0.0 0.0 112676 980 pts/1 R+ 23:15 0:00 grep --color=auto nginx
- 登录测试页面
- 写入1.php测试
[root@zhounan1 conf]# vim /usr/local/nginx/html/1.php
#########写入以下内容#########
<?php
echo "this is nginx test";