lnmp架构介绍

和LAMP不同的是,提供web服务的是Nginx 并且php是作为一个独立服务存在的,这个服务叫做php-fpm Nginx直接处理静态请求,动态请求会转发给php-fpm

安装配置MySQL

1.安装MySQL

  1. tar zxvf mysql-5.6.47-linux-glibc2.12-x86_64.tar.gz
  2. //解压安装包
  3. mv mysql-5.6.47-linux-glibc2.12-x86_64 /usr/local/mysql
  4. //把解压好的包转移并改名
  5. useradd -s /sbin/nologin mysql
  6. //创建mysql用户 不可登陆
  7. cd /usr/local/mysql/
  8. mkdir -p /data/mysql; chown -R mysql:mysql /data/mysql
  9. //创建存放数据目录;更改权限
  10. [root@localhost nginx-1.17.8]# cd /data/mysql/
  11. [root@localhost mysql]# ls -la
  12. . auto.cnf ib_logfile0 localhost.localdomain.err mysql test
  13. .. ibdata1 ib_logfile1 localhost.localdomain.pid performance_schem
  14. yum install -y perl-Module-Install
  15. //初始化,上面有两个ok确定安装成功
  16. cd /usr/local/mysql
  17. ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  18. //启动mysql服务

2.配置MySQL

cp support-files/mysql.server  /etc/init.d/mysqld
chmod 777 /etc/init.d/mysqld
vim /etc/my.cnf
basedir = /usr/local/mysql
datadir = /data/mysql
port = 3306
server_id = 29
socket = /tmp/mysql.sock

cp support-files/mysql.server  /etc/init.d/mysqld
chmod 777 /etc/init.d/mysqld
vim /etc/init.d/mysqld
basedir=/usr/local/mysql  
datadir=/data/mysql
//配置启动脚本
[root@localhost mysql]# chkconfig --add mysqld
[root@localhost mysql]# chkconfig mysqld on
[root@localhost mysql]# service mysqld start 
Starting MySQL. SUCCESS! 
[root@localhost mysql]# netstat -ntlp
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:80              0.0.0.0:*               LISTEN      69053/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1039/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1195/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1039/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1195/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      13729/mysqld

安装配置PHP

1.安装PHP

tar zxvf php-5.6.30.tar.gz 
//解压资源包
useradd -s /sbin/nologin php-fpm 
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 
//安装插件
./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
先安装epel因为centos没有libmcrypt-devel得借助epel的yum源
yum install -y epel-release
yum install -y libmcrypt-devel
make && make install 
[root@localhost php-5.6.30]# echo $?
0

2.配置PHP

 cp /usr/local/src/php-5.6.30/php.ini-production /usr/local/php-fpm/etc/php.ini
 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@lnmp php-5.6.30]# useradd -s /sbin/nologin php-fpm
//带有successful 表示成功。
[root@localhost php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t
[root@localhost php-5.6.30]# cp /usr/local/src/php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-5.6.30]# chmod 755 /etc/init.d/php-fpm 
[root@localhost php-5.6.30]# service php-fpm start
Starting php-fpm  done
[root@localhost php-5.6.30]# chkconfig php-fpm on
[root@localhost php-5.6.30]# ps aux |grep php-fpm
root      63622  0.0  0.5 149628  5168 pts/0    T    10:36   0:00 vim php-fpm.conf
root      63691  0.0  0.5 128400  5208 ?        Ss   10:54   0:00 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   63692  0.0  0.4 128400  4928 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63693  0.0  0.4 128400  4928 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63694  0.0  0.4 128400  4928 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63695  0.0  0.4 128400  4928 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63696  0.0  0.4 128400  4932 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63697  0.0  0.4 128400  4932 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63698  0.0  0.4 128400  4932 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63699  0.0  0.4 128400  4932 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63700  0.0  0.4 128400  4932 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63701  0.0  0.4 128400  4936 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63702  0.0  0.4 128400  4936 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63703  0.0  0.4 128400  4936 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63704  0.0  0.5 128400  5940 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63705  0.0  0.4 128400  4936 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63706  0.0  0.4 128400  4936 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63707  0.0  0.4 128400  4936 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63708  0.0  0.4 128400  4936 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63709  0.0  0.4 128400  4936 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63710  0.0  0.4 128400  4936 ?        S    10:54   0:00 php-fpm: pool www
php-fpm   63711  0.0  0.4 128400  4936 ?        S    10:54   0:00 php-fpm: pool www
root      69123  0.0  0.0 112824   984 pts/0    R+   11:50   0:00 grep --color=auto php-fpm

安装配置nginx

[root@localhost ~]# cd /usr/local/src/
[root@localhost 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@localhost src]# tar zxf nginx-1.17.8.tar.gz 
[root@localhost src]# cd nginx-1.17.8
[root@localhost nginx-1.17.8]# ./configure --prefix=/usr/local/nginx
[root@localhost nginx-1.17.8]# make && make install
[root@localhost nginx-1.17.8]# echo $?
0
[root@localhost 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@localhost nginx-1.17.8]# chmod 755 /etc/init.d/nginx
[root@localhost nginx-1.17.8]# chkconfig --add nginx
[root@localhost nginx-1.17.8]# chkconfig nginx on
[root@localhost nginx-1.17.8]# > /usr/local/nginx/conf/nginx.conf
[root@localhost nginx-1.17.8]# 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@localhost nginx-1.17.8]# /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@localhost nginx-1.17.8]# service nginx start 
Starting nginx (via systemctl):                            [  OK  ]
[root@localhost nginx-1.17.8]# ps aux |grep nginx            
root      69053  0.0  0.0  20568   636 ?        Ss   11:11   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    69054  0.0  0.3  22964  3192 ?        S    11:11   0:00 nginx: worker process
nobody    69055  0.0  0.3  22964  3692 ?        S    11:11   0:00 nginx: worker process
root      69128  0.0  0.0 112824   984 pts/0    R+   11:58   0:00 grep --color=auto nginx
[root@localhost nginx-1.17.8]# netstat -ntlp
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:80              0.0.0.0:*               LISTEN      69053/nginx: master 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1039/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1195/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1039/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1195/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      13729/mysqld 
[root@localhost nginx-1.17.8]# 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@localhost nginx-1.17.8]# vim /usr/local/nginx/html/1.php 
<?php
        echo "test php scripts"
?>             
[root@localhost nginx-1.17.8]# curl localhost/1.php
test php scripts[root@localhost nginx-1.17.8]#