源码安装LNMP架构,保姆级教程,从安装服务搭建到部署网站,超详细,全文步骤搭配 *** 作解释

源码安装LNMP架构,保姆级教程,从安装服务搭建到部署网站,超详细,全文步骤搭配 *** 作解释,第1张

环境准备:

*** 作系统:Centos 7.5   关闭防火墙、selinux

源码包下载地址:https://nginx.org/download/nginx-1.21.6.tar.gz   (环境中使用的是1.21.3)

或者百度网盘链接:https://pan.baidu.com/s/1QyPSNBOJhc5-p-6Yv9Zx9Q    提取码:8888

Mysql包下载地址:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.25.tar.gz

php包下载地址:https://www.php.net/distributions/php-7.4.29.tar.gz

源码编译nginx:

[root@localhost ~]# systemctl stop firewalld          //关闭防火墙

[root@localhost ~]# setenforce 0                   //临时关闭selinux

[root@localhost ~]# useradd -M -s /sbin/nologin nginx   //创建nginx专属用户,以更精确的控制访问权限,增加灵活性、降低安全风险。

[root@localhost ~]# tar -zxvf nginx-1.21.3.tar.gz    //解压源码包

[root@localhost ~]# cd nginx-1.21.3/             //进入源码包目录

[root@localhost nginx-1.21.3]# yum -y install gcc gcc-c++ make pcre-devel expat-devel perl zlib*

//安装源码编译nginx需要用到的依赖包

[root@localhost nginx-1.21.3]# ./configure --prefix=/usr/local/nginx/ --user=nginx --group=nginx --with-http_stub_status_module && make && make install   //预编译和编译nginx

[root@localhost nginx-1.21.3]# ln -s /usr/local/nginx/conf/nginx.conf /etc/   //优化路径

[root@localhost nginx-1.21.3]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/

//创建nginx软链接,以便于直接使用nginx命令就可以直接调用nginx主程序

[root@localhost nginx-1.21.3]# nginx -t     //测试nginx是否正常

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    //启动nginx

[root@localhost ~]# netstat -lntp |grep 80   //查看nginx服务是否开启

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14672/nginx: master

[root@localhost ~]# vim /etc/init.d/nginx    //把nginx服务添加到系统服务中,以便于可以直接使用systemctl、service、chkconfig命令进行管理

[root@localhost ~]# cat /etc/init.d/nginx     //查看配置文件中的内容

#!/bin/bash

# chkconfig: - 35 20 80

# description: Nginx Service Control Script

PROG="/usr/local/nginx/sbin/nginx"

PIDF="/usr/local/nginx/logs/nginx.pid"

case "$1" in

  start)

        $PROG

        ;;

  stop)

        kill -s QUIT $(cat $PIDF)

        ;;

  restart)

        $0 stop

        $0 start

        ;;

  reload)

        kill -s HUP $(cat $PIDF)

        ;;

  *)

        echo "Usage:$0 {start|stop|restart|reload}"

        exit 1

esac

exit 0

[root@localhost ~]# chmod +x /etc/init.d/nginx   //给nginx赋权为可执行文件

[root@localhost ~]# chkconfig --add nginx       //将nginx加入到系统服务中

[root@localhost ~]# systemctl restart nginx      //重新启动nginx

[root@localhost ~]# netstat -lntp |grep 80       //查看nginx服务是否开启

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14672/nginx: master

源码编译mysql:

[root@localhost ~]# yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake  //安装编译mysql所需要的依赖包

[root@localhost ~]# cmake \    //编译需要的引擎以及文件的安装路径

-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \   #指定mysql的安装路径

 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ #指定mysql进程监听套接字文件(数据库连接文件)的存储路径

 -DSYSCONFDIR=/etc \                             #指定配置文件的存储路径

 -DSYSTEMD_PID_DIR=/usr/local/mysql \            #指定进程文件的存储路径

 -DDEFAULT_CHARSET=utf8  \                       #指定默认使用的字符集编码,如 utf8

 -DDEFAULT_COLLATION=utf8_general_ci \       #指定默认使用的字符集校对规则

 -DWITH_EXTRA_CHARSETS=all \                #指定支持其他字符集编码

 -DWITH_INNOBASE_STORAGE_ENGINE=1 \              #安装INNOBASE存储引擎

 -DWITH_ARCHIVE_STORAGE_ENGINE=1 \               #安装ARCHIVE存储引擎

 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \             #安装BLACKHOLE存储引擎

 -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \            #安装FEDERATED存储引擎

 -DMYSQL_DATADIR=/usr/local/mysql/data \         #指定数据库文件的存储路径

 -DWITH_BOOST=boost \          #指定boost的路径,若使用mysql和boot包安装则使用-DWITH_BOOST=/usr/local/boost

 -DWITH_SYSTEMD=1                #生成便于systemctl管理的文件

[root@localhost mysql-5.7.25]# make && make install  //开始编译和安装

[root@localhost mysql-5.7.25]# rm -rf /etc/my.cnf    //删除原有的配置文件

[root@localhost mysql-5.7.25]# vim /etc/my.cnf      //重新编写配置文件

[root@localhost mysql-5.7.25]# cat /etc/my.cnf      //配置文件中的内容

[client]  #客户端设置

port = 3306

socket = /usr/local/mysql/mysql.sock

[mysql]   #服务端设置

port = 3306

socket = /usr/local/mysql/mysql.sock

auto-rehash

[mysqld]  #服务全局设置

user = mysql  #设置管理用户

basedir=/usr/local/mysql  #指定数据库的安装目录

datadir=/usr/local/mysql/data  #指定数据库文件的存储路径

port = 3306  #指定端口3306  

character-set-server=utf8  #设置服务器字符集编码格式为utf8

pid-file = /usr/local/mysql/mysqld.pid   #指定pid 进程文件路径

socket=/usr/local/mysql/mysql.sock      #指定数据库连接文件

bind-address = 0.0.0.0  #设置监听地址,0.0.0.0代表允许所有,如允许多个IP需空格隔开

max_connections=2048            #设置mysql的最大连接数

default-storage-engine=INNODB       #指定默认存储引擎

max_allowed_packet=16M            #设置数据库接收的数据包大小的最大值

server-id = 1               #指定服务ID号

sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

[root@localhost mysql-5.7.25]# chown -R mysql:mysql /usr/local/mysql/   //更改mysql目录所属者和所属组为mysql

[root@localhost mysql-5.7.25]# chown mysql:mysql /etc/my.cnf    //更改mysql配置文件的所属者和所属组为mysql

[root@localhost mysql-5.7.25]# echo 'export PATH=/usr/local/mysql/bin:/usr/local/lib:$PATH' >> /etc/profile    //设置mysql路径的环境变量

[root@localhost mysql-5.7.25]# source /etc/profile    //刷新文件

[root@localhost mysql-5.7.25]# cd /usr/local/mysql/bin/    //进入mysql的运行目录

[root@localhost bin]# ./mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data     //初始化数据库

2022-04-21T10:09:53.585140Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2022-04-21T10:09:54.306868Z 0 [Warning] InnoDB: New log files created, LSN=45790

2022-04-21T10:09:54.435386Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.

2022-04-21T10:09:54.503745Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 30b5a149-c15b-11ec-92b1-000c291d483a.

2022-04-21T10:09:54.505695Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.

2022-04-21T10:09:54.506197Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.

2022-04-21T10:09:55.780885Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.

2022-04-21T10:09:55.780909Z 1 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.

2022-04-21T10:09:55.780916Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.

2022-04-21T10:09:55.780930Z 1 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.

2022-04-21T10:09:55.780935Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.

2022-04-21T10:09:55.780945Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.

2022-04-21T10:09:55.780969Z 1 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.

2022-04-21T10:09:55.780976Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.

[root@localhost ~]# cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/    //将mysql服务添加为系统服务

[root@localhost ~]# systemctl daemon-reload    //刷新系统服务识别

[root@localhost ~]# netstat -lntp |grep 3306     //查看服务是否开启

[root@localhost ~]# systemctl start mysqld      //启动mysqld服务

[root@localhost ~]# netstat -lntp |grep 3306     //查看服务是否开启

tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      10681/mysqld        

[root@localhost ~]# mysql     //初始化完的数据库不需要密码,可直接进入

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.25 Source distribution

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> select version();     //查看版本

+-----------+

| version() |

+-----------+

| 5.7.25    |

+-----------+

1 row in set (0.00 sec)

mysql> set password for root@localhost = password('1');   //修改root密码为1

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all privileges on *.* to 'root'@'%' identified by '1';   //开启mysql的远程权限,远程连接的密码也为1

Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;   //刷新权限

Query OK, 0 rows affected (0.00 sec)

mysql> quit    //退出

Bye

[root@localhost ~]#

[root@localhost ~]# mysql   //再次尝试免密登陆,发现失败

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

[root@localhost ~]# mysql -uroot -p1   //使用密码登录

mysql: [Warning] Using a password on the command line interface can be insecure.

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 4

Server version: 5.7.25 Source distribution

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> quit   //退出

源码编译php:

[root@localhost ~]# tar -zxvf php-7.4.29.tar.gz   //解压php源码包

[root@localhost ~]# cd php-7.4.29/      //进入源码包目录

[root@localhost php-7.4.29]#yum -y install libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel curl curl-devel openssl openssl-devel

//安装编译php所需要的的依赖包

[root@localhost php-7.4.29]# ./configure --prefix=/usr/local/php --with-mysql-sock=/usr/local/mysql/mysql.sock --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo --enable-tokenizer --enable-zip --enable-fpm && make && make install   //编译和安装php

configure: WARNING: unrecognized options: --with-freetype-dir, --with-gd, --with-libxml-dir, --with-pcre-regex, --with-png-dir, --with-jpeg-dir, --enable-libxml, --enable-zip

checking for grep that handles long lines and -e... /usr/bin/grep

checking for egrep... /usr/bin/grep -E

...............

checking for sqlite3 > 3.7.4... no   //此处报错,提示sqlite包不存在

configure: error: Package requirements (sqlite3 > 3.7.4) were not met:

No package 'sqlite3' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you

installed software in a non-standard prefix.

Alternatively, you may set the environment variables SQLITE_CFLAGS

and SQLITE_LIBS to avoid the need to call pkg-config.

See the pkg-config man page for more details.

[root@localhost php-7.4.29]#

[root@localhost php-7.4.29]# yum -y install sqlite sqlite-devel  //直接再下sqlite和依赖包就可解决

[root@localhost php-7.4.29]#  ./configure --prefix=/usr/local/php --with-mysql-sock=/usr/local/mysql/mysql.sock --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo --enable-tokenizer --enable-zip --enable-fpm && make && make install     //重新编译安装

configure: WARNING: unrecognized options: --with-freetype-dir, --with-gd, --with-libxml-dir, --with-pcre-regex, --with-png-dir, --with-jpeg-dir, --enable-libxml, --enable-zip

checking for grep that handles long lines and -e... /usr/bin/grep

checking for egrep... /usr/bin/grep -E

...............

checking for oniguruma... no

configure: error: Package requirements (oniguruma) were not met:   //此时又有一个错误,提示没有oniguruma包

No package 'oniguruma' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you

installed software in a non-standard prefix.

Alternatively, you may set the environment variables ONIG_CFLAGS

and ONIG_LIBS to avoid the need to call pkg-config.

See the pkg-config man page for more details.

[root@localhost php-7.4.29]# wget http://www6.atomicorp.com/channels/atomic/centos/7/x86_64/RPMS/oniguruma-5.9.5-3.el7.art.x86_64.rpm      //直接下载centos7官方源即可解决

[root@localhost php-7.4.29]# yum localinstall oniguruma-5.9.5-3.el7.art.x86_64.rpm -y   //安装oniguruma包

[root@localhost php-7.4.29]# wget http://www6.atomicorp.com/channels/atomic/centos/7/x86_64/RPMS/oniguruma-devel-5.9.5-3.el7.art.x86_64.rpm  //下载oniguruma依赖包的官方源

[root@localhost php-7.4.29]# yum localinstall oniguruma-devel-5.9.5-3.el7.art.x86_64.rpm -y

//安装oniguruma的依赖包

[root@localhost php-7.4.29]# ./configure --prefix=/usr/local/php --with-mysql-sock=/usr/local/mysql/mysql.sock --with-mysqli --with-zlib --with-curl --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --with-openssl --enable-mbstring --enable-xml --enable-session --enable-ftp --enable-pdo --enable-tokenizer --enable-zip --enable-fpm && make && make install    //再次编译安装

configure: WARNING: unrecognized options: --with-freetype-dir, --with-gd, --with-libxml-dir, --with-pcre-regex, --with-png-dir, --with-jpeg-dir, --enable-libxml, --enable-zip

checking for grep that handles long lines and -e... /usr/bin/grep

checking for egrep... /usr/bin/grep -E

...............

config.status: creating ext/phar/phar.1

config.status: creating ext/phar/phar.phar.1

config.status: creating main/php_config.h

config.status: executing default commands

+--------------------------------------------------------------------+

| 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.     //编译php成功

configure: WARNING: unrecognized options: --with-freetype-dir, --with-gd, --with-libxml-dir, --with-pcre-regex, --with-png-dir, --with-jpeg-dir, --enable-libxml, --enable-zip

[root@localhost php-7.4.29]# cp php.ini-development /usr/local/php/lib/php.ini

//复制模板文件作为PHP的主配置文件,并修改其配置

[root@localhost php-7.4.29]# vim /usr/local/php/lib/php.ini  //修改配置

date.timezone = Asia/Shanghai      #位于939行附近

mysqli.default_socket = /usr/local/mysql/mysql.sock   #位于1170行附近

[root@localhost php-7.4.29]# /usr/local/php/bin/php -m  //验证安装的模块

[root@localhost php-7.4.29]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf  //优化php-fpm的模块

[root@localhost php-7.4.29]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf

[root@localhost php-7.4.29]# vim /usr/local/php/etc/php-fpm.conf

pid = run/php-fpm.pid #去掉前面的注释";"

[root@localhost php-7.4.29]# ln -s /usr/local/php/bin/* /usr/local/bin/  //创建软连接

[root@localhost php-7.4.29]# ln -s /usr/local/php/sbin/* /usr/local/sbin/

[root@localhost php-7.4.29]# php-fpm -c /usr/local/php/lib/php.ini    //启动php-fpm

[root@localhost php-7.4.29]# netstat -lnpt | grep 9000  //查看端口,php-fpm已开启

tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      21238/php-fpm: mast

[root@localhost php-7.4.29]# vim /etc/init.d/nginx   //添加PHP-FPM启动设置

[root@localhost php-7.4.29]# cat /etc/init.d/nginx    //查看配置文件中的内容

#!/bin/bash

# chkconfig: - 35 20 80

# description: Nginx Service Control Script

PROG="/usr/local/nginx/sbin/nginx"

PIDF="/usr/local/nginx/logs/nginx.pid"

PROG_FPM="/usr/local/php/sbin/php-fpm"

PIDF_FPM="/usr/local/php/var/run/php-fpm.pid"

case "$1" in

  start)

        $PROG

$PROG_FPM

        ;;

  stop)

        kill -s QUIT $(cat $PIDF)

kill -s QUIT $(cat $PIDF_FPM)

        ;;

  restart)

        $0 stop

        $0 start

        ;;

  reload)

        kill -s HUP $(cat $PIDF)

kill -s HUP $(cat $PIDF_FPM)

        ;;

  *)

        echo "Usage:$0 {start|stop|restart|reload}"

        exit 1

esac

exit 0

[root@localhost ~]# systemctl restart nginx     //重新启动nginx      

[root@localhost ~]# netstat -lntp |grep 80      //查看nginx服务

tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      21233/nginx: master

[root@localhost ~]# netstat -lntp |grep 9000    //查看php-fpm服务

tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      21238/php-fpm: mast

[root@localhost ~]# vim /etc/nginx.conf    //修改nginx.conf文件,使nginx支持php功能模块,取消以下几行的注释,并重新配置为:

        location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;    

            include        fastcgi.conf;

        }

[root@localhost ~]# php -v   //查看安装的php版本

PHP 7.4.29 (cli) (built: Apr 21 2022 23:30:41) ( NTS )

Copyright (c) The PHP Group

Zend Engine v3.4.0, Copyright (c) Zend Technologies

[root@localhost ~]# vim /usr/local/nginx/html/index.php    //编写一个php测试文件

phpinfo();

?>

[root@localhost ~]# systemctl restart nginx     //重启nginx,再进网页测试

  

测试:http://服务器ip/index.php

[root@localhost ~]# cd /usr/local/nginx/html/    //进入nginx站点目录下

[root@localhost html ]# rm -rf index.php     //删除测试文件

[root@localhost html ]# rz          //上传Discuz论坛源码包

[root@localhost html]# ls    

50x.html index.html Discuz_X3.4_SC_UTF8_20210520.zip

[root@localhost html]# unzip Discuz_X3.4_SC_UTF8_20210520.zip  //解压源码包

[root@localhost html]# ls

50x.html                          index.html  qqqun.png  readme.html  utility

Discuz_X3.4_SC_UTF8_20210520.zip  LICENSE     readme     upload

[root@localhost html]# cd upload/     //进入论坛系统的安装目录下

[root@localhost upload]# ls

admin.php  config           favicon.ico  index.php   misc.php    search.php  uc_client

api        connect.php      forum.php    install     plugin.php  source      uc_server

api.php    crossdomain.xml  group.php    m           portal.php  static

archiver   data             home.php     member.php  robots.txt  template

[root@localhost upload]# chmod 777 -R data/ uc_server/ uc_client/ config/    //修改安装目录的文件权限

[root@localhost upload]# systemctl restart nginx    //重新启动nginx

//进入网页测试,http://服务器ip/upload

 

 

 至此,使用LNMP架构部署论坛系统全部完成,如果在部署过程中遇到问题、报错或是文档中存在问题,可在评论区留言。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/732331.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-27
下一篇 2022-04-27

发表评论

登录后才能评论

评论列表(0条)

保存