LNMP黄金架构

1.Ngnix安装配置
1.1.安装nginx所需的pcre库,让nginx支持url重写的rewrite功能

> [root@web01 ~]# yum install pcre pcre-devel -y

1.2.安装openssl-devel模块,nginx需要支持https

>[root@web01 ~]# yum install openssl openssl-devel -y

1.3.安装gcc编译器

> [root@web01 ~]# yum install gcc -y

1.4.下载nginx源码包

> [root@web01 ~]# mkdir -p /home/chaoge/tools 		[root@web01 ~]# cd /home/chaoge/tools 		
> [root@web01 tools]# wget http://nginx.org/download/nginx-1.16.0.tar.gz 		
> [root@web01 tools]# echo $?
0

1.5.创建nginx普通用户

> [root@web01 tools]# useradd nginx -u 1111 -s /sbin/nologin -M

1.6.开始解压缩编译nginx

> [root@web01 tools]# tar -zxf nginx-1.16.0.tar.gz  
> [root@web01 tools]#cd nginx-1.16.0 
> [root@web01 tools]# echo $? 0 
> [root@web01 nginx-1.16.0]# ./configure --user=nginx --group=nginx --prefix=/opt/nginx-1.16.0/ --with-http_stub_status_module --with-http_ssl_module 
> [root@web01 nginx-1.16.0]# make && make install 
> [root@web01 nginx-1.16.0]# echo $? 
> 0

1.7.配置软连接,生产环境常用操作,便于运维、开发、测试使用,以及nginx以后的升级

> [root@web01 nginx-1.16.0]# ln -s /opt/nginx-1.16.0/ /opt/nginx
> [root@web01 nginx-1.16.0]# ll /opt/ 
> 总用量 0 
> lrwxrwxrwx   1 root root  18 1月  29 10:40 nginx -> /opt/nginx-1.16.0/ 
> drwxr-xr-x  11 root root 171 3月   1 18:07 nginx-1.16.0 
> drwxr-xr-x.   2 root root   6 1月  21 11:19 null

1.8.配置nginx环境变量

> [root@web01 nginx-1.16.0]# tail -1 /etc/profile
PATH='/opt/nginx/sbin:/opt/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin'
> [root@web01 nginx-1.16.0]# echo $PATH 
>/opt/nginx/sbin:/opt/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

2.LNMP之MySQL数据库

#安装Mysql的三种方式

  1. Yum/rpm包安装,简单、快速、无法定制化、新手推荐使用
  2. 二进制安装,解压缩后直接简单配置即可使用,速度较快,专业DBA常用
  3. 源码编译安装,特点是可以定制化安装需求,缺点过程较为复杂

2.1.yum方式安装Mysql
#创建mysql用户

> [root@web01 ~]# useradd -s /sbin/nologin mysql 
> [root@web01 ~]# id mysql uid=1000(mysql) gid=1000(mysql) 组=1000(mysql)

#下载mysql二进制软件包,提前配置好yum源,下载wget命令

> [root@web01 ~]# yum install wget -y 			 
> [root@web01 ~]# mkdir -p /home/mysql/tools 
> [root@web01 ~]# cd /home/mysql/tools/

#该mysql文件600M左右,下载时间看网速

> [root@web01 tools]# wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
  • mysql二进制安装包体积较大,名字和源代码包有些区别,但是安装过程较快
  • mysql二进制包 mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz 615M
    mysql源码包 mysql-5.7.26.tar.gz 52M

2.2.二进制方式安装mysql

我们这里搭建LNMP环境,nginx和mysql是安装在一台机器上的,当然也可以是分开在不同的服务器上

#解压并且移动mysql二进制软件包路径

> [root@web01 tools]# tar -zxvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz  			 
> [root@web01 opt]# cd /opt/

#生成软连接

> [root@web01 opt]# ln -s mysql.5.7.26/ mysql 			
> [root@web01 opt]# ll /opt 
> 总用量 0 
> lrwxrwxrwx  1 root root   13 3月  
> 1 19:32 mysql -> mysql.5.7.26/  drwxr-xr-x  9 7161 31415 149 3月   1 19:32 mysql.5.7.26 
> lrwxrwxrwx  1 root root   18 1月  29 10:40 nginx -> /opt/nginx-1.16.0/ 
>drwxr-xr-x 11 root root  171 3月   1 18:07 nginx-1.16.0

#卸载centos7自带的mariadb库,防止冲突

> [root@web01 opt]# rpm -e --nodeps mariadb-libs

#手动创建mysql配置文件 vim /etc/my.cnf

> [root@web01 mysql]# vim /etc/my.cnf
>[mysqld]  #这是代表对服务端生效的配置文件 basedir=/opt/mysql/
> datadir=/opt/mysql/data socket=/tmp/mysql.sock server_id=1 port=3306
>log_error=/opt/mysql/data/mysql_err.log
>[mysql]  #这是代表对客户端生效的配置文件 socket=/tmp/mysql.sock

2.3.初始化mysql数据库
#卸载系统自带的centos7 mariadb-libs,且安装mysql的依赖环境

> [root@web01 mysql]# rpm -qa mariadb-libs #检查是否存在
> [root@web01 mysql]# yum install libaio-devel -y

#创建mysql数据文件夹且授权

> [root@web01 mysql]# mkdir -p /opt/mysql/data
> [root@web01 mysql]# chown -R mysql.mysql /opt/mysql/

#初始化数据库

[root@web01 mysql]# /opt/mysql/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data 		

# 参数解释
–user=mysql 指定用户
–basedir 指定mysql安装目录
–datadir=/opt/mysql/data 指定数据文件夹
–initialize-insecure 关闭mysql安全策略
–initialize 开启mysql安全模式

2.4.配置mysql客户端

> [root@web01 mysql]# vim /etc/systemd/system/mysqld.service 
> [Unit]
> Description=MySQL server by chaoge 
> Documentation=man:mysqld(8)
> Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
> After=network.target After=syslog.target 
> [Install] 
> WantedBy=multi-user.target 
> [Service] 
> User=mysql 
> Group=mysql
> ExecStart=/opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf
> LimitNOFILE=5000

2.5.启动mysql数据库

> [root@web01 mysql]# systemctl start mysqld 		
> [root@web01 mysql]# systemctl enable mysqld 		
> [root@web01 mysql]# systemctl status mysqld

2.6.检查mysql启动状态

> [root@web01 mysql]# netstat -tunlp |grep mysqld  
> tcp6       0      0      :::3306                 :::*          LISTEN      34082/mysqld         		 
> [root@web01 mysql]# ps -ef |grep mysql|grep -v grep 
> mysql     34082      1  0 19:54 ?        00:00:01  /opt/mysql/bin/mysqld --defaults-file=/etc/my.cnf

2.7.配置mysql命令环境变量

> [root@web01 mysql]# echo "export PATH=/opt/mysql/bin:$PATH" >>
> /etc/profile 
> [root@web01 mysql]# source /etc/profile 
> [root@web01 mysql]# echo $PATH
>/opt/mysql/bin:/opt/nginx/sbin:/opt/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

2.8.登录mysql

> [root@web01 ~]# mysql Welcome to the MySQL monitor.  Commands end with
> ; or \g. Your MySQL connection id is 3 Server version: 5.7.26 MySQL
> Community Server (GPL)
> 
> Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights
> reserved.
> 
> Oracle is a registered trademark of Oracle Corporation and/or its
> affiliates. Other names may be trademarks of their respective owners.
> 
> Type 'help;' or '\h' for help. Type '\c' to clear the current input
> statement.
> 
> mysql> show databases;
> +--------------------+ | Database           |
> +--------------------+ | information_schema | | mysql              | | performance_schema | | sys                |
> +--------------------+ 4 rows in set (0.02 sec)
> 
> mysql> select user();            # 查看当前登录的用户
> +----------------+ | user()         |
> +----------------+ | root@localhost |
> +----------------+ 1 row in set (0.00 sec)
> 
> \# 查看mysql所有用户信息 mysql> select user,authentication_string,host from mysql.user;
> +---------------+-------------------------------------------+-----------+ | user          | authentication_string                     | host    
> |
> +---------------+-------------------------------------------+-----------+ | root          |                                           |
> localhost | | mysql.session |
> *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost | | mysql.sys     | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | localhost |
> +---------------+-------------------------------------------+-----------+ 3 rows in set (0.01 sec)

2.9.默认mysql账号root是没有密码的,我们给其设置密码,加大安全性

> [root@web01 mysql]# mysqladmin -uroot password '000000' mysqladmin: [Warning] Using a password on the command line interface can be insecure. 
> Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.

> [root@web01 mysql]# mysql -uroot -p Enter password:  Welcome to the
> MySQL monitor.  Commands end with ; or \g. Your MySQL connection id is
> 4 Server version: 5.7.31 MySQL Community Server (GPL)
> 
> Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights
> reserved.
> 
> Oracle is a registered trademark of Oracle Corporation and/or its
> affiliates. Other names may be trademarks of their respective owners.
> 
> Type 'help;' or '\h' for help. Type '\c' to clear the current input
> statement.
> 
> mysql>

3.FastCGI部署(本地部署)
3.1.检查Nginx和mysql的安装路径

> [root@web01 ~]# ll /opt/ 
> 总用量 0
> lrwxrwxrwx  1 root  root   18 3月  18 10:15 mysql -> /opt/mysql-5.7.26/ 
> drwxr-xr-x 10 mysql mysql 213 3月  18 10:47 mysql-5.7.26 
> lrwxrwxrwx  1 root  root   18 3月  18 22:45 nginx -> /opt/nginx-1.16.0/ 
> drwxr-xr-x  6 root  root   54 3月  18 22:43 nginx-1.16.0

3.2.保证nginx、mysql都启动了

> [root@web01 ~]# netstat -tunlp|grep -E "nginx|mysql" 
> tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      17214/nginx: master 
> tcp6       0      0 :::3306                 :::*   LISTEN      5315/mysqld

3.3.安装部署PHP程序所需的系统库,不要求必须安装,而是安装上之后,可以扩展php更多功能

> yum install  gcc gcc-c++ make zlib-devel libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel \ freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel -y

3.4.默认yum源中缺少libiconv-devel软件包,需要编译安装,用于php的编码转换

> [root@web01 ~]# wget -P /home/chaoge/tools/ 
> http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz 
> [root@web01 ~]# cd /home/chaoge/tools/ 
> [root@web01 tools]# ls libiconv-1.15.tar.gz nginx-1.16.0  nginx-1.16.0.tar.gz 
> [root@web01 tools]# tar -zxvf libiconv-1.15.tar.gz 
> [root@web01 libiconv-1.15]# cd libiconv-1.15
> [root@web01 libiconv-1.15]# ./configure -- prefix=/opt/libiconv
> [root@web01 libiconv-1.15]# make && make install

4.安装PHP(FastCGI形式)
4.1.下载获取php软件包

> [root@web01 tools]# wget http://mirrors.sohu.com/php/php-7.3.5.tar.gz

4.2.解压缩php源码包,编译安装

> [root@web01 tools]# tar -zxvf php-7.3.5.tar.gz 
> [root@web01 tools]# cd
> php-7.3.5 
> [root@web01 php-7.3.5]#./configure --prefix=/opt/php7.3.5 \
> --enable-mysqlnd \
> --with-mysqli=mysqlnd \
> --with-pdo-mysql=mysqlnd \
> --with-iconv-dir=/opt/libiconv \
> --with-freetype-dir \
> --with-jpeg-dir \
> --with-png-dir \
> --with-zlib \
> --with-libxml-dir=/usr \
> --enable-xml \
> --disable-rpath \
> --enable-bcmath \
> --enable-shmop \
> --enable-sysvsem \
> --enable-inline-optimization \
> --with-curl \
> --enable-mbregex \
> --enable-fpm \
> --enable-mbstring \
> --with-gd \
> --with-openssl \
> --with-mhash \
> --enable-pcntl \
> --enable-sockets \
> --with-xmlrpc \
> --enable-soap \
> --enable-short-tags \
> --enable-static \
> --with-xsl \
> --with-fpm-user=nginx \
> --with-fpm-group=nginx \
> --enable-ftp \
> --enable-opcache=no

#对于如上的参数,根据自己实际工作环境优化增删即可

> --prefix=  指定php安装路径
> --enable-mysqlnd 使用php自带的mysql相关软件包
> --with-fpm-user=nginx  指定PHP-FPM程序的用户是nginx,和nginx服务保持统一
> --enable-fpm 激活php-fpm方式,以FastCGI形式运行php程序 待看到如下显示,表示正确的编译了php

4.3.在执行完编译脚本文件后,开始执行编译安装

> [root@web01 php-7.3.5]# make && make install

4.4.看到如此的画面,表示php正确安装结束了,可以用特殊变量验证

> [root@web01 php-7.3.5]# echo $? 
> 0

4.5.编译安装结束后,配置环境变量

> [root@web01 php-7.3.5]# ln -s /opt/php7.3.5/ /opt/php

5.php配置文件
5.1.配置文件路径

> [root@web01 php-7.3.5]# ls php.ini* 
> php.ini-development  php.ini-production

5.2.俩配置文件,分别默认用于开发环境,生成环境,配置参数有所不同
# 可以用如下命令对比文件区别

> [root@web01 php-7.3.5]# vimdiff php.ini-development php.ini-production

#开发环境下开起了更多的日志、调试信息,生产环境该参数都关闭了
5.3.拷贝php配置文件到php默认目录,且改名

> [root@web01 php-7.3.5]# cp php.ini-development /opt/php/lib/php.ini

6.FastCGI的配置文件
6.1.默认FastCGI的配置文件路径

> [root@web01 etc]# pwd /opt/php/etc 
> [root@web01 etc]# ls pear.conf 
> php-fpm.conf.default  php-fpm.d

6.2.生成2个php-frpm的配置文件,先用默认配置,后续可以再后话

> [root@web01 etc]# cp php-fpm.conf.default php-fpm.conf 
> [root@web01 etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf

7.启动PHP服务(FastCGI模式)
# 启动服务,并且检查状态

[root@web01 etc]# /opt/php/sbin/php-fpm 
[root@web01 etc]# netstat -tunlp|grep php 
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      109647/php-fpm: mas

8.修改Nginx支持PHP
8.1.修改nginx配置文件,在最底行添加 包含文件参数,建议删除nginx.conf原有的server配置

> [root@web01 conf]# vim /opt/nginx/conf/nginx.conf http {
>     include       mime.types;
>     default_type  application/octet-stream;
>     sendfile        on;
>     keepalive_timeout  65;
>     gzip  on; 
>     include extra/01_www.conf; 
>     include extra/02_bbs.conf; 
>     include extra/03_blog.conf; 
>     include extra/04_status.conf; 
>     }

8.2.配置PHP的解析配置文件

> [root@web01 conf]# mkdir extra [root@web01 conf]# vim
> extra/03_blog.conf 
> [root@web01 conf]# [root@web01 conf]# [root@web01
> conf]# cat extra/03_blog.conf server { listen 80; server_name
> blog.chaoge.com; location / {
>     root html/blog;
>     index index.html; }
> 
> #添加有关php程序的解析 location ~ .*\.(php|php5)?$ {
>     root html/blog;
>     fastcgi_pass 127.0.0.1:9000;
>     fastcgi_index index.php;
>     include fastcgi.conf; }
> 
> }

8.3.检查且启动nginx

> [root@web01 conf]# touch extra/01_www.conf 
> [root@web01 conf]# touch extra/02_bbs.conf 
> [root@web01 conf]# touch extra/04_status.conf
> [root@web01 conf]# nginx -t 
> nginx: the configuration file /opt/nginx-1.16.0//conf/nginx.conf syntax is ok 
> nginx: configuration file /opt/nginx-1.16.0//conf/nginx.conf test is successful

#看到以下页面,表示lnmp环境已经能够正确解析了
在这里插入图片描述
#测试正确后,保证服务器安全,就得删除该test_info.php文件了

9.测试php访问mysql
9.1.php的代码,直接在页面中解析即可

[root@web01 conf]# vim ../html/blog/test_mysql.php
<?php
$link_id=mysqli_connect('localhost','root','000000') 
or mysql_error();
if($link_id){

    echo "mysql successful by chaoge.\n";
}else {

    echo mysql_error();
}
?>

9.2.看到如此的画面,lnmp环境基本搭建完毕,nginx+php+mysql的形式。
在这里插入图片描述

10.LNMP远程部署
上述php-fpm和nginx在一起,这里的讲解,是php-fpm和nginx在两台服务器,远程访问

  • 参考博客: https://xuchen.wang/archives/nginxphp.html

还有注意,修改php-fpm的启动地址,改为0.0.0.0:9000

配置文件夹在安装目录的etc/php-fpm.d/下面的已conf为后缀的文件,一般在安装时我们设置成了www.conf。

把 listen = 127.0.0.1:9000 后面的端口号9000换成你需要的端口,然后重启php

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值