LNMP架构的基础和安装
LNMP架构
LNMP介绍
LNMP是linux+nginx+mysql+php
Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为 “engine X”, 是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP 代理服务器。
Nginx 能超越 Apache 的高性能和稳定性,这得益于Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的select模型。目前Linux下能够承受高并发访问的 Squid、Memcached都采用的是epoll网络I/O模型。
它和LAMP最大的区别是在LNMP中PHP是一个单独的服务启动,而LAMP中PHP只是一个模块。
LNMP工作原理
①. 用户请求的静态文件,由nginx服务自行处理,根据静态的location配置进行处理
用户请求的动态文件,由php服务进行处理,根据动态的location配置进行处理
②. nginx服务接收到动态请求,会将请求抛送给fastcgi,类似于nginx服务接收动态请求的秘书,秘书会将动态请求送给PHP程序
③. PHP如果可以处理,会将处理结果直接通过fastcgi返回给nginx程序;如果不可以处理,还会请求后端数据库,最终再把处理结果返回给nginx
LNMP环境搭建
1.安装mysql
[root@antong ~]# yum install -y net-tools.x86_64 tree.x86_64 //安装常用工具
[root@antong ~]# cd /usr/local/src/ //所有软件包都放在这个目录下,方便管理
[root@antong src]# tar -zxvf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@antong src]# mv /usr/local/mysql-5.6.47-linux-glibc2.12-x86_64/ /usr/local/mysql
[root@antong src]# useradd -s /sbin/nologin mysql
[root@antong src]# cd /usr/local/mysql/
[root@antong mysql]# mkdir -p /data/mysql //创建存放数据目录
[root@antong mysql]# chown -R mysql:mysql /data/mysql/ //更改权限
[root@antong mysql]# ls -la /data/mysql/ //查看权限
total 0
drwxr-xr-x. 2 mysql mysql 6 Sep 5 21:34 .
drwxr-xr-x. 3 root root 19 Sep 5 21:34 ..
[root@antong mysql]# yum install -y perl-Module-Install //安装依赖软件
[root@antong mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql/
//初始化数据库,有两个ok确定安装成功
2.配置MySQL
[root@antong mysql]# cp support-files/my-default.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
[root@antong mysql]# vim /etc/my.cnf
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 29
socket = /tmp/mysql.sock
//配置启动脚本
[root@antong mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@antong mysql]# chmod 777 /etc/init.d/mysqld
[root@antong mysql]# vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/data/mysql
[root@antong mysql]# chkconfig --add mysqld
[root@antong mysql]# chkconfig mysql on
[root@antong mysql]# service mysqld start
Starting MySQL. SUCCESS!
[root@antong mysql]# netstat -nltp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 876/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 964/master
tcp6 0 0 :::3306 :::* LISTEN 12466/mysqld
tcp6 0 0 :::22 :::* LISTEN 876/sshd
tcp6 0 0 ::1:25 :::* LISTEN 964/master
3.安装PHP
[root@antong src]# yum install -y gcc libxml2-devel openssl openssl-devel bzip2 bzip2-devel libpng libpng-devel freetype freetype-devel eplel-release libmcrypt-devel libcurl-devel libjpeg-devel epel-release.noarch libmcrypt-devel //安装依赖软件
[root@antong src]# tar -zxvf php-5.6.30.tar.gz
[root@antong src]# cd php-5.6.30/
[root@antong 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-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 --disable-ipv6 --with-pear --with-curl --with-openssl
[root@antong php-5.6.30]# echo $?
0
[root@antong php-5.6.30]# make && make install
[root@antong php-5.6.30]# echo $?
0
4.配置PHP
[root@antong php-5.6.30]# cd /usr/local/php-fpm/etc/
[root@antong php-5.6.30]# vim /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.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
[root@antong etc]# useradd -s /sbin/nologin php-fpm
[root@antong etc]# /usr/local/php-fpm/sbin/php-fpm -t
[05-Sep-2021 22:38:31] NOTICE: configuration file /usr/local/php-fpm/etc/php-fpm.conf test is successful //successful为成功
[root@antong etc]# cp /usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@antong etc]# chmod 755 /etc/init.d/php-fpm
[root@antong etc]# service php-fpm start
Starting php-fpm done
[root@antong etc]# ps aux | grep php-fpm
root 128728 0.0 0.2 128244 5036 ? Ss 22:42 0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm 128729 0.0 0.2 128244 4804 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128730 0.0 0.2 128244 4804 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128731 0.0 0.2 128244 4804 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128732 0.0 0.2 128244 4804 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128733 0.0 0.2 128244 4808 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128734 0.0 0.2 128244 4808 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128735 0.0 0.2 128244 4808 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128736 0.0 0.2 128244 4808 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128737 0.0 0.2 128244 4808 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128738 0.0 0.2 128244 4808 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128739 0.0 0.2 128244 4808 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128740 0.0 0.2 128244 4808 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128741 0.0 0.2 128244 4808 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128742 0.0 0.2 128244 4812 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128743 0.0 0.2 128244 4812 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128744 0.0 0.2 128244 4812 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128745 0.0 0.2 128244 4812 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128746 0.0 0.2 128244 4812 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128747 0.0 0.2 128244 4812 ? S 22:42 0:00 php-fpm: pool www
php-fpm 128748 0.0 0.2 128244 4812 ? S 22:42 0:00 php-fpm: pool www
root 128766 0.0 0.0 112812 968 pts/0 S+ 22:43 0:00 grep --color=auto php-fpm
5.安装nginx
[root@antong src]# ls
mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz php-5.6.30
nginx-1.17.8.tar.gz php-5.6.30.tar.gz
[root@antong src]# tar -zxvf nginx-1.17.8.tar.gz
[root@antong src]# cd nginx-1.17.8/
[root@antong nginx-1.17.8]# ./configure --prefix=/usr/local/nginx
[root@antong nginx-1.17.8]# make && make install
[root@antong nginx-1.17.8]# echo $?
0
[root@antong nginx-1.17.8]# 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@antong nginx-1.17.8]# chmod 755 /etc/init.d/nginx
[root@antong nginx-1.17.8]# chkconfig --add nginx
[root@antong nginx-1.17.8]# chkconfig nginx on
[root@antong nginx-1.17.8]# cd /usr/local/nginx/conf/
[root@antong conf]# mv nginx.conf nginx.conf.bak
[root@antong conf]# vim 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@antong 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@antong conf]# service nginx start
Starting nginx (via systemctl): [ OK ]
[root@antong conf]# ps aux | grep nginx
root 840 0.0 0.0 20568 636 ? Ss 23:00 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody 841 0.0 0.1 22964 3200 ? S 23:00 0:00 nginx: worker process
nobody 842 0.0 0.1 22964 3200 ? S 23:00 0:00 nginx: worker process
root 844 0.0 0.0 112812 968 pts/0 S+ 23:00 0:00 grep --color=auto nginx
[root@antong conf]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
进行测试:
[root@antong conf]# vim /usr/local/nginx/html/1.php
<?php
echo "test php scripts"
?>
[root@antong conf]# curl localhost/1.php
test php scripts