搭建LNMP架构

搭建LNMP架构

1.关闭防火墙,安装依赖包,创建运行用户、组

点击查看代码
[root@localhost ~]#  systemctl disable --now  firewalld
[root@localhost ~]#  setenforce 0

[root@localhost ~]#  yum -y install pcre-devel zlib-devel gcc gcc-c++ make

[root@localhost ~]#  useradd -M -s /sbin/nologin nginx

2.编译安装nginx

点击查看代码
[root@localhost ~]#  cd /opt
[root@localhost opt]#  rz

[root@localhost opt]#  ls
nginx-1.22.0.tar.gz  rh
[root@localhost opt]#  tar xf nginx-1.22.0.tar.gz    //解压
[root@localhost opt]#  ls
nginx-1.22.0  nginx-1.22.0.tar.gz  rh

[root@localhost opt]#  cd nginx-1.22.0/
[root@localhost nginx-1.22.0]#  ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.22.0]#  make && make install

[root@localhost nginx-1.22.0]#  cd /usr/local/nginx/
[root@localhost nginx]#  ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/   //软链接

[root@localhost nginx]#  tee /lib/systemd/system/nginx.service   <<eof
> [Unit]
> Description=nginx
> After=network.target
> [Service]
> Type=forking
> PIDFile=/usr/local/nginx/logs/nginx.pid
> ExecStart=/usr/local/nginx/sbin/nginx
> ExecReload=/bin/kill -1 $MAINPID
> ExecStop=/bin/kill -3 $MAINPID
> PrivateTmp=true
> [Install]
> WantedBy=multi-user.target
> eof
[root@localhost nginx]#  systemctl daemon-reload 
[root@localhost nginx]#  systemctl start nginx
[root@localhost nginx]#  chmod 777 /lib/systemd/system/nginx.service //修改权限

3.yum安装mysql

点击查看代码
[root@localhost nginx]#  tee /etc/yum.repos.d/mysql.repo <<EOF      //官方源
> [mysql57-community]
> name=MySQL 5.7 Community Server
> baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/
> enabled=1
> gpgcheck=0
> EOF

[root@localhost nginx]#  yum -y install mysql-community-server   //社区版
[root@localhost nginx]#  systemctl start mysqld

[root@localhost ~]#  grep password /var/log/mysqld.log   //过滤出密码
2024-06-10T02:42:00.592178Z 1 [Note] A temporary password is generated for root@localhost: P7V>dNlF8XIi
2024-06-10T02:43:37.028857Z 2 [Note] Access denied for user 'root'@'localhost' (using password: NO)

[root@localhost ~]#  mysql -u root -p"P7V>dNlF8XIi"
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.44

Copyright (c) 2000, 2023, Oracle and/or its affiliates.

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> set global validate_password_policy=0;     //修改密码策略
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;     //修改密码策略
Query OK, 0 rows affected (0.00 sec)

mysql> alter user root@'localhost' identified by 'abc123';   //修改密码
Query OK, 0 rows affected (0.00 sec)
mysql> ^DBye    //ctrl+D退出

[root@localhost ~]#  mysql -u root -p"abc123"   //修改密码后可以直接使用新密码登录


4.编译安装php

点击查看代码
`1、安装环境依赖包`
[root@localhost ~]#  yum -y install gd \    //安装依赖环境
> libjpeg libjpeg-devel \
> libpng libpng-devel \
> freetype freetype-devel \
> libxml2 libxml2-devel \
> zlib zlib-devel \
> curl curl-devel \
> openssl openssl-devel


`2、编译安装`
[root@localhost ~]#  cd /opt
[root@localhost opt]#  rz

[root@localhost opt]#  ls
Discuz_X3.4_SC_UTF8.zip  nginx-1.22.0  nginx-1.22.0.tar.gz  php-7.1.10.tar.bz2  rh
[root@localhost opt]#  tar xf php-7.1.10.tar.bz2   //解压
[root@localhost opt]#  ls
Discuz_X3.4_SC_UTF8.zip  nginx-1.22.0  nginx-1.22.0.tar.gz  php-7.1.10  php-7.1.10.tar.bz2  rh

[root@localhost opt]#  cd php-7.1.10/
[root@localhost php-7.1.10]#  ./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-fpm \
> --enable-mbstring \
> --enable-xml \
> --enable-session \
> --enable-ftp \
> --enable-pdo \
> --enable-tokenizer \
> --enable-zip
[root@localhost php-7.1.10]#  make -j2 && make install


`3、路径优化`
[root@localhost php-7.1.10]#  ln -s /usr/local/php/bin/* /usr/local/bin/ 
[root@localhost php-7.1.10]#  ln -s /usr/local/php/sbin/* /usr/local/sbin/


点击查看代码
`4、调整PHP配置文件`
php有三个配置文件: 
php.ini		主配置文件  
php-fpm.conf	进程服务配置文件 
www.conf          扩展配置文件


修改主配置文件
[root@localhost php-7.1.10]#  cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini
[root@localhost php-7.1.10]#  vim /usr/local/php/lib/php.ini  
 939 date.timezone = Asia/Shanghai                       //修改时区
1170 mysqli.default_socket = /var/lib/mysql/mysql.sock   //指明mysql文件位置

修改进程服务配置文件
[root@localhost php-7.1.10]#  cd /usr/local/php/etc/
[root@localhost etc]#  ls
pear.conf  php-fpm.conf.default  php-fpm.d
[root@localhost etc]#  cp  php-fpm.conf.default php-fpm.conf
[root@localhost etc]#  vim php-fpm.conf
 17 pid = run/php-fpm.pid    //去掉";"注释

修改扩展配置文件
[root@localhost etc]#  cd /usr/local/php/etc/php-fpm.d/
[root@localhost php-fpm.d]#  ls
www.conf.default
[root@localhost php-fpm.d]#  cp www.conf.default www.conf
[root@localhost php-fpm.d]#  ls
www.conf  www.conf.default

点击查看代码
`5、启动php-fpm`
[root@localhost php-fpm.d]#  cd /opt/php-7.1.10/sapi/fpm
[root@localhost fpm]#  cp php-fpm.service /usr/lib/systemd/system/php-fpm.service
[root@localhost fpm]#  systemctl daemon-reload 
[root@localhost fpm]#  systemctl start php-fpm.service 

[root@localhost fpm]#  ss -natp |grep 9000
LISTEN     0      128    127.0.0.1:9000                     *:*                   users:(("php-fpm",pid=15127,fd=0),("php-fpm",pid=15126,fd=0),("php-fpm",pid=15125,fd=6))

5.配置nginx支持php解析
修改主配置文件

点击查看代码
[root@localhost fpm]#  vim /usr/local/nginx/conf/nginx.conf
 
 45             index  index.html index.htm index.php;
 .................
 65         location ~ \.php$ {
 66             root           html;
 67             fastcgi_pass   127.0.0.1:9000;
 68             fastcgi_index  index.php;
 69             fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;    //注意路径!
 70             include        fastcgi_params;
 71         }
 
[root@localhost fpm]#  nginx -s reload

验证php测试页

点击查看代码
[root@localhost fpm]#  cd /usr/local/nginx/html/
[root@localhost html]#  ls
50x.html  index.html

[root@localhost html]#  vim index.php   //编辑一个测试页面
<?php
phpinfo();
?>
[root@localhost html]#  nginx -s reload

浏览器中输入192.168.204.40/index.php进行验证

验证数据库工作是否正常

点击查看代码
[root@localhost html]#  mysql -u root -p"abc123"

CREATE DATABASE bbs;
GRANT all ON bbs.* TO 'bbsuser'@'%' IDENTIFIED BY 'admin123';
GRANT all ON bbs.* TO 'bbsuser'@'localhost' IDENTIFIED BY 'admin123';
flush privileges;

修改测试页内容

点击查看代码
[root@localhost html]#  vim /usr/local/nginx/html/index.php

<?php
$link=mysqli_connect('192.168.204.40','bbsuser','admin123');   //注意ip地址
if($link) echo "<h1>Success!!</h1>";
else echo "Fail!!";
?>

[root@localhost html]#  nginx -s reload

浏览器中输入192.168.204.40/index.php进行验证

安装论坛

点击查看代码
[root@localhost html]#  cd /opt
[root@localhost opt]#  ls
Discuz_X3.4_SC_UTF8.zip  nginx-1.22.0  nginx-1.22.0.tar.gz  php-7.1.10  php-7.1.10.tar.bz2  rh
[root@localhost opt]#  unzip Discuz_X3.4_SC_UTF8.zip   //解压

[root@localhost opt]#  cd /opt/dir_SC_UTF8/
[root@localhost dir_SC_UTF8]#  cp -r upload/ /usr/local/nginx/html/bbs/

[root@localhost dir_SC_UTF8]#  cd /usr/local/nginx/html/bbs/  //修改权限
[root@localhost bbs]#  chown -R nobody ./config/
[root@localhost bbs]#  chown -R nobody ./data/
[root@localhost bbs]#  chown -R nobody ./uc_client/
[root@localhost bbs]#  chown -R nobody ./uc_server/

浏览器访问安装

posted @   leikj  阅读(11)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示