简介
LNMP架构平台介绍
L:Linux(平台是linux)
N:Nginx(请求转发,反向代理,处理静态资源)
M:MySQL(存储数据)
P:PHP(处理动态语言)
静态页面请求:client–>nginx(location)–>html–>client
动态页面请求:client–>nginx(location)–>fastcgi(快速通用网关接口)–>php-fpm–>wrappe(真正执行者)–>php–>mysql–>php–>wrapper–>php-fpm–>fastcgi–>nginx–>client
MySQL
下载MySQL
在这里下载包:link
我下载了5.7.30的安装包
解压后我们可以看到:
[root@localhost mysql-5.7.30]# ls
boost Docs libservices README testclients
BUILD Doxyfile-perfschema LICENSE regex unittest
client extra man scripts VERSION
cmake include mysql-test source_downloads vio
CMakeLists.txt INSTALL mysys sql win
cmd-line-utils libbinlogevents mysys_ssl sql-common zlib
config.h.cmake libbinlogstandalone packaging storage
configure.cmake libmysql plugin strings
dbug libmysqld rapid support-files
预编译,编译安装
接下来我们按理说应该使用./configure来进行预编译
但是mysql是通过cmake来进行预编译
在企业中,我们为了安全,会创建一个新的用户进行对mysql的操作
useradd -s /sbin/nologin -M mysql
在cmake之前,我们先安装几个依赖包,解决依赖性问题,如果你已经有了这些包,就可以跳过这一步。如果还是缺少包,出问题后可以下载解决
yum install -y cmake gcc gcc-c++ ncurses-devel openssl-devel.x86_64
安装cmake,进行预编译:
yum install -y gcc gcc-c++ ncurses-devel bison cmake openssl-devel.x86_64
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql -DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_READLINE=1 -DENABLED_LOCAL_INFILE=1 -DMYSQL_USER=mysql -DMYSQL_TCP_PROT=3306 -DDOWNLOAD_BOOST=1 -DWITH_BOOST=boost/boost_1_59_0/
make && make install
配置
编译安装完成之后,会出现指定目录,在/usr/local/lnmp/mysql
创建data目录,并对其中的权限进行设置:
mkdir data
chown -R root . 让mysql目录下的所有目录只有root用户有权限更改
chown -R mysql data 让刚才建立mysql用户可以向data目录中写入数据
ln -s /usr/local/lnmp/mysql/bin/* /usr/local/bin 做软连接,可以直接mysql启动
更改配置文件位置,以便修改配置:
它的配置文件在:
/usr/local/lnmp/mysql/mysql-test/include/default_my.cnf
备份mariadb数据库配置文件:
mv /etc/my.cnf /etc/my.cnf.bak
把mysql的配置文件放到/etc/下:
cp default_my.cnf /etc/my.cnf
它的启动脚本放在:
/usr/local/lnmp/mysql/support-files/mysql.server
方便起见,我们将启动脚本挪出来,放在/etc/init.d下:
cp -a mysql.server /etc/init.d/mysqld ##就可以用systemctl的方式启动了
给它执行权限:
chmod +x /etc/init.d/mysqld
配置开机启动:
chkconfig mysqld on
启动
先对mysql进行初始化,生成一些相应的表:
mysqld --user=mysql --initialize
在这一步会有一些warning并生成一个临时密码,记住这个临时密码,之后要用。
如果忘记了,戳:
启动mysql:
方法一:
systemctl start mysqld.service
方法二:
/etc/init.d/mysqld start
登录mysql,使用上面生成的密码:
mysql -uroot -p
现在还不能去show databases,会报错,提示重置密码
我们进行一个安全初始化;
mysql_secure_installation
更改密码,并进行一些简单设置,就可以看到内容了:
[root@localhost init.d]# 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.30 Source distribution
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> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
mysql>
PHP
下载
下载安装包,我用7.4.1
解压编译
解压:
[root@server1 /]# tar zxf php-7.4.1.tar.gz
预编译,编译安装
预编译:
cd php-7.4.1
./configure --prefix=/usr/local/lnmp/php \
--with-config-file-path=/usr/local/lnmp/php/etc \
--with-mysqli=/usr/local/lnmp/mysql/bin/mysql_config \
--enable-soap \
--enable-mbstring=all \
--enable-sockets \
--with-pdo-mysql=/usr/local/lnmp/mysql \
--enable-gd \
--without-pear \
--enable-fpm
发现在这个过程中,需要安装一些包:
yum install -y oniguruma-5.9.5-3.el7.x86_64.rpm oniguruma-devel-5.9.5-3.el7.x86_64.rpm
yum install libxml2-devel.x86_64 sqlite-devel.x86_64 libpng-devel.x86_64 -y
预编译完成后,显示:
+--------------------------------------------------------------------+
| License: |
| This software is subject to the PHP License, available in this |
| distribution in the file LICENSE. By continuing this installation |
| process, you are bound by the terms of this license agreement. |
| If you do not agree with the terms of this license, you must abort |
| the installation process at this point. |
+--------------------------------------------------------------------+
Thank you for using PHP.
编译安装:
make && make install
完成之后就可以在预定义的目录中看到php目录
[root@server1 php]# ls
bin etc include lib php sbin var
[root@server1 php]# pwd
/usr/local/lnmp/php
配置
拷贝一份默认的配置文件为自己所用:
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
[root@server1 etc]# pwd
/usr/local/lnmp/php/etc
打开里面的pid进程:
[root@server1 etc]# vim php-fpm.conf
[global]
; Pid file
; Note: the default prefix is /usr/local/lnmp/php/var
; Default Value: none
pid = run/php-fpm.pid
进入php安装目录里面的etc文件,同样拷贝一份里面的配置文件:
[root@server1 etc]# cd php-fpm.d/
[root@server1 php-fpm.d]# cp www.conf.default www.conf
[root@server1 php-fpm.d]# ls
www.conf www.conf.default
更改配置文件里面的用户和群组,因为php是处理nginx传过来的请求的:
[root@server1 php-fpm.d]# vim www.conf ##配置配置文件
; Unix user/group of processes
; Note: The user is mandatory. If the group is not set, the default user's group
; will be used.
user = nginx
group = nginx
我们创建一个nginx用户:
[root@server1 php-fpm.d]# useradd -s /sbin/nologin -M nginx
拷贝源码包中的配置文件到php的安装目录:
[root@server1 php-7.4.1]# cp /php-7.4.1/php.ini-production /usr/local/lnmp/php/etc/php.ini
在这个配置目录里面我们只需要更改一下时区:
[Date]
; Defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = Asia/Shanghai
启动
配置启动脚本:
把源码包中的启动脚本复制到/etc/init.d中去,并添加执行权限:
[root@server1 fpm]# cp /php-7.4.1/sapi/fpm/init.d.php-fpm /etc/init.d/
[root@server1 init.d]# chmod +x /etc/init.d/php-fpm
此时就可以使用执行脚本的方式启动php:
[root@server1 init.d]# /etc/init.d/php-fpm start
Starting php-fpm done
检查端口是否可以打开:
[root@server1 init.d]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 130654/php-fpm: mas
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 8074/X
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 8137/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7618/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 7611/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 8082/master
tcp6 0 0 :::3306 :::* LISTEN 11176/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::6000 :::* LISTEN 8074/X
tcp6 0 0 :::22 :::* LISTEN 7618/sshd
tcp6 0 0 ::1:631 :::* LISTEN 7611/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 8082/master
3306和9000端口都已经开启!
Nginx
下载
[root@server1 /]# tar zxf nginx-1.18.0.tar.gz
预编译,编译安装
对Nginx进行简单的编译安装:
./configure --prefix=/usr/local/lnmp/nginx \ ##安装目录
--with-http_ssl_module \
--with-http_stub_status_module \
--user=nginx --group=nginx ## 将它的用户和用户组设置为nginx
make && make install
成功后会看到相应目录:
[root@server1 lnmp]# cd nginx/
[root@server1 nginx]# pwd
/usr/local/lnmp/nginx
配置
修改配置文件:
[root@server1 conf]# pwd
/usr/local/lnmp/nginx/conf
[root@server1 conf]# vim nginx.conf
......
location / {
root html;
index index.php index.html index.htm;
}
......
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi.conf;
}
连接到环境变量方便我们的使用:
[root@server1 conf]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/bin/
访问过程是:
先访问index.php,将动态访问请求转到本地9000(php-fpm)端口
启动
检查配置文件是否正确:
[root@server1 conf]# nginx -t
nginx: the configuration file /usr/local/lnmp/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/lnmp/nginx/conf/nginx.conf test is successful
打开nginx:
[root@server1 conf]# nginx
查看端口是否启用:
[root@server1 conf]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN 130654/php-fpm: mas
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3264/nginx: master
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 8074/X
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 8137/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 7618/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 7611/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 8082/master
tcp6 0 0 :::3306 :::* LISTEN 11176/mysqld
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::6000 :::* LISTEN 8074/X
tcp6 0 0 :::22 :::* LISTEN 7618/sshd
tcp6 0 0 ::1:631 :::* LISTEN 7611/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 8082/master
启用成功!
测试访问
关掉主机的防火墙
[root@server1 conf]# systemctl stop firewalld.service
网页访问主机ip:192.168.1.11
此时因为我们没有index.php页面,所以它默认访问的是index.html
我们可以写一个index.php
[root@server1 html]# pwd
/usr/local/lnmp/nginx/html
重新加载nginx
[root@server1 html]# ps aux| grep nginx ##先关闭nginx进程
root 3264 0.0 0.0 45872 1148 ? Ss 13:52 0:00 nginx: master process nginx
nginx 3266 0.0 0.1 48404 2248 ? S 13:52 0:00 nginx: worker process
root 3559 0.0 0.0 112708 976 pts/2 S+ 14:04 0:00 grep --color=auto nginx
nginx 130655 0.0 0.2 196844 4000 ? S 13:19 0:00 php-fpm: pool www
nginx 130656 0.0 0.2 196844 4000 ? S 13:19 0:00 php-fpm: pool www
[root@server1 html]# kill 3264
[root@server1 html]# ps aux| grep nginx
root 3575 0.0 0.0 112708 976 pts/2 S+ 14:04 0:00 grep --color=auto nginx
nginx 130655 0.0 0.2 196844 4000 ? S 13:19 0:00 php-fpm: pool www
nginx 130656 0.0 0.2 196844 4000 ? S 13:19 0:00 php-fpm: pool www
[root@server1 html]# nginx ##重新开启
在浏览器上面进行访问:
到此为止,php页面就访问成功了。
后记
1.安装的nginx比较大
2.nginx版本号对外可见,并不安全
3.mysql并没有使用到