• 周日. 10 月 6th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

LNMP架构

admin

11 月 28, 2021

 LNMP架构介绍

lnmp里的php会启动一个服务,nginx把用户请求的php交给php-fpm处理,比如登录网站php要和mysql做交互,查用户的账号和密码,处理完之后,php-fpm会告诉nginx,nginx返回给用户一个结果。Nginx对于一些静态请求就自己处理了,不需要交给php处理。

 MySQL安装

1.如果之前安装过mysql先将mysql删除掉

[root@bogon ~]# cd /usr/local/src/
[root@bogon src]# ls
apr-1.6.3              httpd-2.4.28                                  mongodb-linux-x86_64-rhel70-3.2.9.tgz      php-7.1.6
apr-1.6.3.tar.gz       httpd-2.4.28.tar.gz                           mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz  php-7.1.6.tar.gz
apr-util-1.6.1         mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz  php-5.6.30                                 phpredis-develop
apr-util-1.6.1.tar.gz  mongodb-linux-x86_64-rhel70-3.2.9             php-5.6.30.tar.gz                          phpredis-develop.zip
[root@bogon src]# rm -rf /usr/local/mysql
[root@bogon src]# rm -rf /etc/init.d/mysqld 
[root@bogon src]# 

2.wget下载源码包,解压

[root@bogon src]# tar -zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 

3.移动目录

[root@bogon src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
[root@bogon src]# ls /usr/local/mysql/
bin  COPYING  data  docs  include  lib  man  mysql-test  README  scripts  share  sql-bench  support-files
[root@bogon src]# 

4.添加mysql用户(因为之前添加过了就不用再useradd了),删除之前的mysql文件

[root@bogon src]# id mysql
uid=1001(mysql) gid=1001(mysql) 组=1001(mysql)
[root@bogon src]# rm -rf /data/mysql/*
[root@bogon src]# 

5.配置mysql修改my.cnf文件和mysql文件

[root@bogon src]# cd /usr/local/mysql/
[root@bogon mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
[root@bogon mysql]# cp support-files/my-default.cnf /etc/my.cnf
[root@bogon mysql]# cp support-files/mysql.server /etc/init.d/mysql[root@bogon mysql]# vim /etc/my.cnf
[mysqld]
datadir=/data/mysql
socket=/tmp/mysql.sock  

[root@bogon mysql]# vim /etc/init.d/mysql basedir=/usr/local/mysql datadir=/data/mysql
[root@bogon mysql]# mv /etc/init.d/mysql /etc/init.d/mysqld

6.启动mysql,检查是否启动成功

[root@bogon mysql]# /etc/init.d/mysqld start
[root@bogon mysql]# ps aux|grep mysql root 33506 0.0 0.0 113268 324 pts/5 S 17:34 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/bogon.pid root 33642 1.6 41.2 453136 412676 pts/5 D 17:34 0:02 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/bogon.err --pid-file=/data/mysql/bogon.pid --socket=/tmp/mysql.sock root 33652 0.1 0.0 112680 584 pts/5 R+ 17:36 0:00 grep --color=auto mysql [root@bogon mysql]#

7.将mysql服务添加到服务列表里并开机启动

[root@bogon mysql]# chkconfig --add mysqld
[root@bogon mysql]# chkconfig mysqld on
[root@bogon mysql]# service mysqld stop Shutting down MySQL.. SUCCESS! [root@bogon mysql]# service mysqld start Starting MySQL.... SUCCESS! [root@bogon mysql]#

PHP安装

 

1.和LAMP安装PHP方法有差别,这里需要开启php-fpm服务,之前php是作为Apache的一个模块,需要指定Apache的路径,用apxs工具配置模块怎么加载。这里就不需要了,因为用不到Apache。Php是作为独立的服务。

2.清理之前编译过的文件,make clean 之后就是刚刚解压的状态

[root@bogon src]# cd php-5.6.30/
[root@bogon php-5.6.30]# make clean
[root@bogon php-5.6.30]# useradd -s /sbin/nologin php-fpm (添加php-fpm用户,-s指定不能登录)

3.重新编译php

[root@bogon 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

4.提示少curl包,yum安装

checking for cURL support... yes
checking for cURL in default path... not found
configure: error: Please reinstall the libcurl distribution -
    easy.h should be in <curl-dir>/include/curl/
[root@bogon php-5.6.30]# 

 

[root@bogon php-5.6.30]# yum install libcurl-devel

5.make && make install

6.查看php-fpm配置文件语法是否正确

[root@bogon php-5.6.30]# ls /usr/local/php-fpm/
bin  etc  include  lib  php  sbin  var
[root@bogon php-5.6.30]# ls /usr/local/php
bin  etc  include  lib  php
[root@bogon php-5.6.30]# ls /usr/local/php-fpm/sbin/php-fpm -t
/usr/local/php-fpm/sbin/php-fpm
[root@bogon php-5.6.30]# /usr/local/php-fpm/sbin/php-fpm -t
[08-Feb-2018 22:26:48] ERROR: failed to open configuration file '/usr/local/php-fpm/etc/php-fpm.conf': No such file or directory (2)
[08-Feb-2018 22:26:48] ERROR: failed to load configuration file '/usr/local/php-fpm/etc/php-fpm.conf'
[08-Feb-2018 22:26:48] ERROR: FPM initialization failed
[root@bogon php-5.6.30]#

  

7.拷贝配置文件

 cp php.ini-production /usr/local/php-fpm/etc/php.ini  (复制目录并改名字)
[root@bogon php-5.6.30]# cp php.ini-production /usr/local/php-fpm/etc/php.ini
[root@bogon php-5.6.30]# cd /usr/local/php-fpm/etc/
[root@bogon etc]# ls
pear.conf  php-fpm.conf.default  php.ini
[root@bogon 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.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 

8.拷贝启动的脚本

[root@bogon etc]# cd /usr/local/src/php-5.6.30/
[root@bogon php-5.6.30]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@bogon php-5.6.30]# chmod 755 /etc/init.d/php-fpm 
[root@bogon php-5.6.30]# chkconfig --add php-fpm
[root@bogon php-5.6.30]# 

 

[root@bogon php-5.6.30]# chkconfig php-fpm on
[root@bogon php-5.6.30]# service php-fpm start

9.提示用户不存在

[root@bogon php-5.6.30]# useradd -s /sbin/nologin php-fpm

  

[root@bogon php-5.6.30]# service php-fpm start
Starting php-fpm  done
[root@bogon php-5.6.30]# ps aux|grep php-fpm

10.php-fcgi的权限为666

[root@bogon php-5.6.30]# ls -l /tmp/php-fcgi.sock 
srw-rw-rw-. 1 root root 0 2月   8 22:48 /tmp/php-fcgi.sock
[root@bogon php-5.6.30]# 

Nginx介绍  

 

Nginx安装 

  

 1.下载Nginx

[root@bogon php-5.6.30]# cd /usr/local/src/
[root@bogon src]# ls
apr-1.6.3              mariadb-10.2.6-linux-glibc_214-x86_64.tar.gz  php-7.1.6
apr-1.6.3.tar.gz       mongodb-linux-x86_64-rhel70-3.2.9             php-7.1.6.tar.gz
apr-util-1.6.1         mongodb-linux-x86_64-rhel70-3.2.9.tgz         phpredis-develop
apr-util-1.6.1.tar.gz  mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz     phpredis-develop.zip
httpd-2.4.28           php-5.6.30
httpd-2.4.28.tar.gz    php-5.6.30.tar.gz
[root@bogon src]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

 2.解压

[root@bogon src]# tar zxvf nginx-1.12.2.tar.gz 

3.编译

[root@bogon src]# cd nginx-1.12.2/
[root@bogon nginx-1.12.2]# ./configure --prefix=/usr/local/nginx

4.安装

[root@bogon nginx-1.12.2]# make && make install

5.创建nginx启动脚本

[root@bogon nginx-1.12.2]# ls /usr/local/nginx/
conf  html  logs  sbin
[root@bogon nginx-1.12.2]# vim /etc/init.d/nginx

6.启动脚本文件内容  

#!/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

7.更改权限,添加到开机启动

[root@bogon nginx-1.12.2]# chmod 755 /etc/init.d/nginx 
[root@bogon nginx-1.12.2]# che
check-binary-files  checkmodule         checksctp           
checkisomd5         checkpolicy         cheese              
[root@bogon nginx-1.12.2]# chkconfig --add nginx
[root@bogon nginx-1.12.2]# chkconfig nginx on
[root@bogon nginx-1.12.2]# 

8.编辑nginx配置文件

[root@bogon nginx-1.12.2]# cd /usr/local/nginx/conf/
[root@bogon conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@bogon conf]# mv nginx.conf nginx.conf.bak
[root@bogon conf]# vim nginx.conf
[root@bogon conf]#

  

user nobody nobody;                                  (启动nginx的用户)
worker_processes 2;                                (定义子进程)
error_log /usr/local/nginx/logs/nginx_error.log crit;               (错误日志)
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                                  (http服务)
    {
        listen 80;                                 (监听80端口)
        server_name localhost;                          (设置域名)
        index index.html index.htm index.php;                (设置主页)
        root /usr/local/nginx/html;                       (设置访问主目录)
        location ~ .php$                               (定义php解析)
        {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
           #fastcgi_pass 127.0.0.1:9000;   (和上面一行的意思相同,只是不同的写法,监听127.0.0.1:9000)
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }    
    }
}

9.检查配置文件是否有错

  

[root@bogon 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@bogon conf]# 

  

[root@bogon ~]# /etc/init.d/nginx start
[root@bogon logs]# service nginx start
Starting nginx (via systemctl):                            [  确定  ]
[root@bogon logs]# 

 

[root@bogon logs]# ps aux|grep nginx
root      49403  0.0  0.0  20500   628 ?        Ss   01:30   0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
nobody    49405  0.0  0.3  25028  3252 ?        S    01:30   0:00 nginx: worker process
nobody    49406  0.0  0.3  25028  3252 ?        S    01:30   0:00 nginx: worker process
root      49670  0.0  0.0 112680   976 pts/7    R+   01:33   0:00 grep --color=auto nginx
[root@bogon logs]# 

  

[root@bogon logs]# curl localhost
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>

发表回复