1. 程式人生 > 實用技巧 >Linux專案之部署lnmp架構

Linux專案之部署lnmp架構

目錄


環境說明:

系統 IP 所需服務
CentOS7 192.168.159.144 nginx、mysql、php

實驗前請關閉防火牆和SELINUX,並配置好網路源

1. 安裝nginx


//安裝依賴包
[root@longnian ~]#  yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ wget
[root@longnian ~]#  yum -y groups mark install 'Development Tools'
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
Marked install: Development Tools

//建立系統使用者nginx
[root@longnian ~]# useradd -r -M -s /sbin/nologin nginx
[root@longnian ~]# id nginx
uid=996(nginx) gid=994(nginx) groups=994(nginx)

//建立日誌存放目錄
[root@longnian ~]#  mkdir -p /var/log/nginx
[root@longnian ~]# chown -R nginx.nginx /var/log/nginx

//下載nginx並安裝
[root@longnian ~]# wget http://nginx.org/download/nginx-1.18.0.tar.gz
[root@longnian src]#  tar xf nginx-1.18.0.tar.gz
[root@longnian src]#  cd nginx-1.18.0
[root@longnian nginx-1.18.0]#  ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@longnian nginx-1.18.0]#  make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install

//配置環境變數
[root@longnian ~]#  echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@longnian ~]#  source /etc/profile.d/nginx.sh
[root@longnian ~]# ss -anlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port                              
LISTEN      0      128             *:80                          *:*                  
LISTEN      0      128             *:22                          *:*                                                               
LISTEN      0      128            :::22                         :::*                  
LISTEN      0      100           ::1:25                         :::*                              :::*

埠起來後,用瀏覽器訪問驗證

2. 安裝mysql


#安裝依賴包
[root@longnian ~]# yum -y install libaio ncurses-devel openssl-devel openssl cmake mariadb-devel
 
#建立使用者和組
[root@longnian ~]# useradd -r -M -s /sbin/nologin mysql

//建立mysql使用者和組
[root@longnian ~]# useradd -r -M -s /sbin/nologin -u 306 mysql
[root@longnian ~]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)

//下載二進位制格式的mysql軟體包
[root@longnian ~]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz
[root@longnian ~]# tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz -C /usr/local

//建立軟連線,修改屬主和環境變數
[root@longnian ~]#  cd  /usr/local/
[root@longnian local]#  ln -s mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
[root@longnian local]#  chown -R mysql.mysql mysql*
[root@longnian local]#  ll -d mysql
lrwxrwxrwx. 1 mysql mysql 36 Jul  6 09:39 mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/
[root@longnian ~]#  echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@longnian ~]#  source /etc/profile.d/mysql.sh

//建立資料存放目錄
[root@longnian ~]#  mkdir /opt/data
[root@longnian ~]#  chown -R mysql.mysql /opt/data
[root@longnian ~]# ll -d /opt/data/
drwxr-xr-x 6 mysql mysql 4096 Jul 31 18:50 /opt/data/

初始化資料庫,並配置
[root@longnian ~]# /usr/local/mysql/bin/mysqld --initialize-insecure --user=mysql --datadir=/opt/data
//這裡加入了-insecure選項,可以不要密碼

[root@longnian ~]# ln -s /usr/local/mysql/include /usr/local/include/mysql
[root@longnian ~]# echo '/usr/local/mysql/lib' > /etc/ld.so.conf.d/mysql.conf
[root@longnian ~]# ldconfig

//生成配置檔案
[root@longnian ~]# cat > /etc/my.cnf <<EOF
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
EOF

//配置服務啟動指令碼
[root@longnian ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@longnian ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@longnian ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld

//啟動mysql,並修改密碼
[root@longnian ~]# chkconfig mysqld on    //設定為開機自動啟動
[root@longnian ~]# service mysqld start
[root@longnian ~]# ss -antl |grep 3306
LISTEN     0      80          :::3306                    :::*

[root@longnian ~]# mysql -e "set password = password('longnian123.')"
[root@longnian ~]# mysql -uroot -plongnian123.
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 29
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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
Bye

3. 安裝PHP


//配置yum源
[root@longnian ~]# wget http://rpms.remirepo.net/enterprise/remi-release-7.rpm
[root@longnian ~]# yum -y install remi-release-7.rpm
[root@longnian ~]# yum clean all
[root@longnian ~]# yum makecache --enablerepo=remi-php74

//安裝依賴包
[root@longnian ~]# yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php74-php-mysqlnd

//下載並安裝php
[root@longnian ~]# wget https://www.php.net/distributions/php-7.4.7.tar.gz
[root@longnian ~]# tar xf php-7.4.7.tar.gz
[root@longnian ~]# cd php-7.4.7
[root@longnian ~]# ./configure --prefix=/usr/local/php7  \
--with-config-file-path=/etc \
--enable-fpm \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-png \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-json \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--enable-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix
////這裡如果報錯需要安裝兩個依賴包,安裝好後再次執行上面的命令
[root@longnian php-7.4.7]# yum -y install libsqlite3x-devel oniguruma-devel
[root@longnian php-7.4.7]# make -j $(cat /proc/cpuinfo |grep processor|wc -l)
[root@longnian php-7.4.7]# make install

//配置環境變數
[root@longnian ~]#  echo 'export PATH=/usr/local/php7/bin:$PATH' > /etc/profile.d/php7.sh
[root@longnian ~]#  source /etc/profile.d/php7.sh
[root@longnian ~]# php -v
PHP 7.4.7 (cli) (built: Jul 31 2020 18:00:53) ( NTS )
Copyright (c) The PHP Group
Zend Engine v3.4.0, Copyright (c) Zend Technologies

//配置php-fpm
[root@longnian ~]# cp php-7.4.7/php.ini-production /etc/php.ini
[root@longnian ~]# cp php-7.4.7/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@longnian ~]# chmod +x /etc/rc.d/init.d/php-fpm
[root@longnian ~]# cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
[root@longnian ~]# cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
[root@longnian ~]# vim /usr/local/php7/etc/php-fpm.conf
//新增以下四行
pm.max_children = 50       //最多同時提供50個程序提供50個併發服務
pm.start_servers = 5       //啟動時啟動5個程序
pm.min_spare_servers = 2   //最小空閒程序數
pm.max_spare_servers = 8   //最大空閒程序數

//啟動php-fpm
[root@longnian ~]# service php-fpm start
Starting php-fpm  done
[root@node2 ~]# chkconfig php-fpm on
[root@longnian ~]# ss -anlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128             *:9000                        *:*                  
LISTEN      0      128             *:80                          *:*                  
LISTEN      0      128             *:22                          *:*                  
LISTEN      0      100     127.0.0.1:25                          *:*                  
LISTEN      0      80             :::3306                       :::*                  
LISTEN      0      100            :::8080                       :::*                  
LISTEN      0      128            :::22                         :::*                  
LISTEN      0      100           ::1:25                         :::*                  
LISTEN      0      1        ::ffff:127.0.0.1:8005                       :::*

//配置監聽所有IP
[root@longnian ~]# vim /usr/local/php7/etc/php-fpm.d/www.conf
listen = 127.0.0.1:9000     `//將這裡的127.0.0.1改為0.0.0.0`
[root@node2 ~]# service php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@longnian ~]# ss -anlt
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128             *:9000                        *:*                  
LISTEN      0      128             *:80                          *:*                  
LISTEN      0      128             *:22                          *:*                  
LISTEN      0      100     127.0.0.1:25                          *:*                  
LISTEN      0      80             :::3306                       :::*                  
LISTEN      0      100            :::8080                       :::*                  
LISTEN      0      128            :::22                         :::*                  
LISTEN      0      100           ::1:25                         :::*                  
LISTEN      0      1        ::ffff:127.0.0.1:8005   

4. 配置nginx


//建立php測試頁面
[root@longnian ~]# vim /usr/local/nginx/html/index.php
<?php
    phpinfo();
?>

//修改nginx主配置檔案
[root@longnian ~]# vim /usr/local/nginx/conf/nginx.conf
...
location / {
            root   html;
            index  index.php index.html index.htm;  //在index後面新增index.php,表示有限訪問php頁面
        }
...
//將以下內容取消註釋並修改
location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
...

//重讀配置檔案
[root@node2 ~]# nginx -s reload

訪問網頁

5. 利用lnmp搭建WordPress


5.1 配置資料庫

[root@longnian ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 30
Server version: 5.7.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, 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> CREATE DATABASE wordpress;
..........
mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY '123456';
..........
mysql> GRANT ALL PRIVILEGES ON wordpress.* TO 'user'@'localhost' IDENTIFIED BY '123456';
..........
mysql> FLUSH PRIVILEGES;
mysql> quit
Bye

5.2 安裝和配置 WordPress

下載WordPress

//進入/usr/local/nginx/html/目錄,並下載與解壓 WordPress
[root@longnian html]#  wget https://cn.wordpress.org/wordpress-5.0.4-zh_CN.tar.gz
[root@longnian html]# tar zxvf wordpress-5.0.4-zh_CN.tar.gz

//修改 WordPress 配置檔案
//找到檔案中 MySQL 的部分,並將相關配置資訊修改為 配置 WordPress 資料庫 中的內容
[root@longnian html]# cd wordpress
[root@longnian wordpress]# cp wp-config-sample.php wp-config.php
[root@longnian wordpress]# vim wp-config.php
........
 // ** MySQL settings - You can get this info from your web host ** //
 /** The name of the database for WordPress */
 define('DB_NAME', 'wordpress');

 /** MySQL database username */
 define('DB_USER', 'user');

 /** MySQL database password */
 define('DB_PASSWORD', '123456');

 /** MySQL hostname */
 define('DB_HOST', 'localhost');

驗證 WordPress 安裝
在瀏覽器上訪問安裝介面

安裝過程填寫資訊

安裝完成!

登入

成功登入 WordPress 部落格