实验环境
#系统
[root@lnmp ~]# cat /etc/redhat-release
CentOS Linux release 7.7.1908 (Core)
#安装包
nginx-1.14.2
mariadb-10.2.26-linux-x86_64
php-7.3.10
编译安装php需要知道mysql的安装路径,所有整个编译安装过程式nginx-->mariadb-->php-->nginx整合php-->php连接数据库
一、nginx安装(端口80)
1、安装依赖,准备编译安装环境
[root@lnmp ~]# yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre-devel
[root@lnmp ~]# groupadd nginx
[root@lnmp ~]# useradd nginx -g nginx -s /sbin/nologin -M
2、解压并进入nginx目录配置参数选项
[root@lnmp ~]# tar -zxf nginx-1.14.2.tar.gz
[root@lnmp ~]# cd nginx-1.14.2
[root@lnmp nginx-1.14.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module --with-http_gzip_static_module
3、编译安装并添加到环境变量
[root@lnmp nginx-1.14.2]# make && make install
[root@lnmp nginx-1.14.2]# vim /etc/profile.d/nginx.sh
export PATH=$PATH:/usr/local/nginx/sbin
[root@lnmp nginx-1.14.2]# source /etc/profile
4、添加系统服务启动脚本
#/sbin/nginx路径一定要跟编译安装路径一致,否则脚本无法使用
[root@lnmp nginx-1.14.2]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/bin/rm -f /usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@lnmp nginx-1.14.2]# systemctl start nginx
[root@lnmp nginx-1.14.2]# systemctl enable nginx
[root@lnmp nginx-1.14.2]# ss -ntlp|grep 80
LISTEN 0 128 *:80 *:* users:(("nginx",pid=12137,fd=6),("nginx",pid=12136,fd=6))
二、mariadb二进制安装(端口3306)
1、准备安装环境
[root@lnmp ~]# groupadd -r -g 306 mysql
[root@lnmp ~]# useradd -g 306 -r -u 306 mysql
[root@lnmp ~]# mkdir -p /mydata/data
[root@lnmp ~]# chown -R mysql.mysql /mydata/data/
[root@lnmp ~]# chmod o-rwx /mydata/data/
2、二进制包必须解压到/usr/local,然后创建软连接命名为mysql(保留版本名)
[root@lnmp ~]# tar -zxvf mariadb-10.2.26-linux-x86_64.tar.gz -C /usr/local/
[root@lnmp ~]# cd /usr/local/
[root@lnmp local]# ln -sv mariadb-10.2.26-linux-x86_64 mysql
二进制包里面也提供了安装方法
3、初始化并添加 到环境变量,方便启动
[root@lnmp local]# cd mysql/
[root@lnmp mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/data/
[root@lnmp mysql]# echo $?
0
[root@lnmp mysql]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@lnmp mysql]# source /etc/profile
4、恢复目录权限为root
[root@lnmp mysql]# chown -R root.root /usr/local/mysql/*
5、拷贝服务启动脚本,添加到开机启动
[root@lnmp mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lnmp mysql]# chkconfig --add mysqld
6、根据主机内存大小,选择拷贝配置文件。修改配置文件,添加数据存储路径
[root@lnmp mysql]# cp /usr/local/mysql/support-files/my-large.cnf /etc/my.cnf
cp:是否覆盖"/etc/my.cnf"? y
[root@lnmp mysql]# vim /etc/my.cnf
[mysqld]
datadir = /mydata/data
7、启动并查看
[root@lnmp mysql]# systemctl start mysqld
[root@lnmp mysql]# ss -ntlp|grep 3306
LISTEN 0 80 [::]:3306 [::]:* users:(("mysqld",pid=12603,fd=21))
8、修改mariadb的root用户密码为123456,php连接需要使用
[root@lnmp mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.26-MariaDB-log MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> SET PASSWORD FOR 'root'@'localhost'=PASSWORD('123456');
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> quit
Bye
[root@lnmp mysql]# mysql #测试已经无法直接登录
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@lnmp mysql]# mysql -uroot -p #测试输入密码登录
Enter password:
三、php安装(端口:9000)
1、安装依赖,准备安装环境
[root@lnmp ~]# yum -y install gd zlib zlib-devel openssl openssl-devel libxml2 libxml2-devel libjpeg libjpeg-devel libpng libpng-devel
[root@lnmp ~]# tar -zxvf php-7.3.10.tar.gz -C /usr/local/
[root@lnmp ~]# mv /usr/local/php-7.3.10 /usr/local/php7
2、配置参数选择
[root@lnmp ~]# cd /usr/local/php7/
[root@lnmp php7]# ./configure --prefix=/usr/local/php7 --with-gd --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --enable-mbstring --enable-fpm --with-mysql=/usr/local/mysql --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd
3、编译安装
[root@lnmp php7]# ./configure --prefix=/usr/local/php7 --with-gd --enable-mysqlnd --with-pdo-mysql=mysqlnd --with-openssl --enable-mbstring --enable-fpm --with-mysql=/usr/local/mysql --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql=mysqlnd
[root@lnmp php7]# echo $?
0
#加上-j 4,4是CPU个数,加快编译速度
[root@lnmp php7]# make -j 4
[root@lnmp php7]# make install
4、拷贝配置文件
[root@lnmp php7]# cp etc/php-fpm.conf.default etc/php-fpm.conf
[root@lnmp php7]# cp etc/php-fpm.d/www.conf.default etc/php-fpm.d/www.conf
5、添加系统服务启动脚本
[root@lnmp php7]# vim /lib/systemd/system/php7.service
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target
[Service]
Type=simple
PIDFile=/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm --nodaemonize --fpm-config /usr/local/php7/etc/php-fpm.conf
ExecReload=/bin/kill -USR2 $MAINPID
ExecStop=/bin/kill -SIGINT $MAINPID
[Install]
WantedBy=multi-user.target
[root@lnmp php7]# systemctl start php7
[root@lnmp php7]# systemctl enable php7
[root@lnmp php7]# ss -ntlp |grep 9000
LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=36188,fd=8),("php-fpm",pid=36187,fd=8),("php-fpm",pid=36186,fd=6))
四、整合nginx+php
1、修改nginx配置文件,去掉注释。创建php页面存储路径
[root@lnmp ~]# vim /usr/local/nginx/conf/nginx.conf
[root@lnmp ~]# mkdir -p /var/www/html
[root@lnmp ~]# vim /var/www/html/index.php
This is Nginx_PHP
<?php
phpinfo();
?>
2、重启nginx服务,并查看结果
[root@lnmp ~]# systemctl restart nginx
五、php连接数据库
1、修改index.php,添加连接数据库测试
[root@lnmp ~]# vim /var/www/html/index.php
This is Nginx_PHP
<br >
<?php
$mysqli = new mysqli("localhost", "root", "123456");
if(!$mysqli) {
echo"Faild!连接错误,用户名或密码不对";
}else{
echo"OK!PHP连接数据库正常";
}
$mysqli->close();
phpinfo();
?>
2、重启服务
[root@lnmp ~]# systemctl restart nginx
3、查看结果
4、停止mariadb,就会出现错误信息
[root@lnmp ~]# systemctl stop mysqld
至此,lnmp环境搭建并验证成功