搭建LNMP架構

leikj發表於2024-06-17

搭建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/

瀏覽器訪問安裝

相關文章