Linux系統之LNMP及nginx反向代理實現


  1、編譯安裝LNMP,並安裝wordpress

  首先准備環境,編譯安裝LNMP可以是多台主機,也可以是單台主機,把nginx,mysql,php都集中安裝在一個主機上;我這里以一台主機為例吧!!

  一、編譯安裝nginx

  1)准備編譯環境

yum groupinstall "development tools" "server platform development" -y
yum install pcre-devel openssl-devel zlib-devel geoip geoip-devel perl-devel perl-ExtUtils-Embed gd-devel libxml2-devel libxslt-devel -y

  提示:可能上面安裝的包依賴有epel倉庫,所以建議還需要把epel倉庫配好;

  2)下載nginx源碼包

[root@lnmp ~]# ls
[root@lnmp ~]# wget http://nginx.org/download/nginx-1.16.1.tar.gz
--2020-03-18 12:47:31--  http://nginx.org/download/nginx-1.16.1.tar.gz
Resolving nginx.org (nginx.org)... 62.210.92.35, 95.211.80.227, 2001:1af8:4060:a004:21::e3
Connecting to nginx.org (nginx.org)|62.210.92.35|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1032630 (1008K) [application/octet-stream]
Saving to: ‘nginx-1.16.1.tar.gz’

100%[======================================================================================>] 1,032,630   27.8KB/s   in 51s    

2020-03-18 12:48:23 (19.6 KB/s) - ‘nginx-1.16.1.tar.gz’ saved [1032630/1032630]

[root@lnmp ~]# ls
nginx-1.16.1.tar.gz
[root@lnmp ~]# tar xf nginx-1.16.1.tar.gz 
[root@lnmp ~]# cd nginx-1.16.1
[root@lnmp nginx-1.16.1]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  man  README  src
[root@lnmp nginx-1.16.1]# 

  3)創建nginx用戶用於后續啟動nginx用的用戶

[root@lnmp nginx-1.16.1]# getent passwd nginx
[root@lnmp nginx-1.16.1]# useradd -r nginx -s /sbin/nologin -d /var/lib/nginx
[root@lnmp nginx-1.16.1]# getent passwd nginx                                
nginx:x:997:995::/var/lib/nginx:/sbin/nologin
[root@lnmp nginx-1.16.1]# 

  4)加載預編譯環境檢查

[root@lnmp nginx-1.16.1]# ./configure --prefix=/usr/share/nginx \
> --sbin-path=/usr/sbin/nginx \
> --modules-path=/usr/lib64/nginx/modules \
> --conf-path=/etc/nginx/nginx.conf \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --http-client-body-temp-path=/var/lib/nginx/tmp/client_body \
> --http-proxy-temp-path=/var/lib/nginx/tmp/proxy \
> --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi \
> --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi \
> --http-scgi-temp-path=/var/lib/nginx/tmp/scgi \
> --pid-path=/run/nginx.pid \
> --lock-path=/run/lock/subsys/nginx \
> --user=nginx --group=nginx \
> --with-file-aio \
> --with-http_ssl_module \
> --with-http_v2_module \
> --with-http_realip_module \
> --with-stream_ssl_preread_module \
> --with-http_addition_module \
> --with-http_xslt_module=dynamic \
> --with-http_image_filter_module=dynamic \
> --with-http_sub_module \
> --with-http_dav_module \
> --with-http_flv_module \
> --with-http_mp4_module \
> --with-http_gunzip_module \
> --with-http_gzip_static_module \
> --with-http_random_index_module \
> --with-http_secure_link_module \
> --with-http_degradation_module \
> --with-http_slice_module \
> --with-http_stub_status_module \
> --with-http_perl_module=dynamic \
> --with-http_auth_request_module \
> --with-mail=dynamic \
> --with-mail_ssl_module \
> --with-pcre \
> --with-pcre-jit \
> --with-stream=dynamic \
> --with-stream_ssl_module \
> --with-google_perftools_module \
> --with-debug \
> --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' \
> --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

  提示:在這個過程中可能會出現缺少環境依賴包,這個要看提示什么,對應安裝好提示的包即可解決問題;

checking for GD library ... found
checking for GD WebP support ... not found
checking for perl
 + perl version: This is perl 5, version 16, subversion 3 (v5.16.3) built for x86_64-linux-thread-multi
 + perl interpreter multiplicity found
checking for Google perftools ... found
creating objs/Makefile

Configuration summary
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/usr/share/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/usr/lib64/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/lib/nginx/tmp/client_body"
  nginx http proxy temporary files: "/var/lib/nginx/tmp/proxy"
  nginx http fastcgi temporary files: "/var/lib/nginx/tmp/fastcgi"
  nginx http uwsgi temporary files: "/var/lib/nginx/tmp/uwsgi"
  nginx http scgi temporary files: "/var/lib/nginx/tmp/scgi"

[root@lnmp nginx-1.16.1]# 

  提示:出現以上內容表示我們的編譯環境是可以正常編譯nginx,接下來就可以使用make && make install 編譯安裝了

[root@lnmp nginx-1.16.1]# make -j 4 && make install
make -f objs/Makefile
make[1]: Entering directory `/root/nginx-1.16.1'
cc -c -pipe  -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic -I src/core -I src/event -I src/event/modules -I src/os/unix -I src/http/modules/perl -I /usr/include/libxml2 -I objs \
……省略部分內容……
Installing /usr/local/lib64/perl5/auto/nginx/nginx.bs
Installing /usr/local/lib64/perl5/auto/nginx/nginx.so
Installing /usr/local/lib64/perl5/nginx.pm
Installing /usr/local/share/man/man3/nginx.3pm
Appending installation info to /usr/lib64/perl5/perllocal.pod
make[2]: Leaving directory `/root/nginx-1.16.1/objs/src/http/modules/perl'
test -d '/usr/share/nginx' || mkdir -p '/usr/share/nginx'
test -d '/usr/sbin' \
        || mkdir -p '/usr/sbin'
test ! -f '/usr/sbin/nginx' \
        || mv '/usr/sbin/nginx' \
                '/usr/sbin/nginx.old'
cp objs/nginx '/usr/sbin/nginx'
test -d '/etc/nginx' \
        || mkdir -p '/etc/nginx'
cp conf/koi-win '/etc/nginx'
cp conf/koi-utf '/etc/nginx'
cp conf/win-utf '/etc/nginx'
test -f '/etc/nginx/mime.types' \
        || cp conf/mime.types '/etc/nginx'
cp conf/mime.types '/etc/nginx/mime.types.default'
test -f '/etc/nginx/fastcgi_params' \
        || cp conf/fastcgi_params '/etc/nginx'
cp conf/fastcgi_params \
        '/etc/nginx/fastcgi_params.default'
test -f '/etc/nginx/fastcgi.conf' \
        || cp conf/fastcgi.conf '/etc/nginx'
cp conf/fastcgi.conf '/etc/nginx/fastcgi.conf.default'
test -f '/etc/nginx/uwsgi_params' \
        || cp conf/uwsgi_params '/etc/nginx'
cp conf/uwsgi_params \
        '/etc/nginx/uwsgi_params.default'
test -f '/etc/nginx/scgi_params' \
        || cp conf/scgi_params '/etc/nginx'
cp conf/scgi_params \
        '/etc/nginx/scgi_params.default'
test -f '/etc/nginx/nginx.conf' \
        || cp conf/nginx.conf '/etc/nginx/nginx.conf'
cp conf/nginx.conf '/etc/nginx/nginx.conf.default'
test -d '/run' \
        || mkdir -p '/run'
test -d '/var/log/nginx' \
        || mkdir -p '/var/log/nginx'
test -d '/usr/share/nginx/html' \
        || cp -R html '/usr/share/nginx'
test -d '/var/log/nginx' \
        || mkdir -p '/var/log/nginx'
test -d '/usr/lib64/nginx/modules' \
        || mkdir -p '/usr/lib64/nginx/modules'
test ! -f '/usr/lib64/nginx/modules/ngx_http_xslt_filter_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_http_xslt_filter_module.so' \
                '/usr/lib64/nginx/modules/ngx_http_xslt_filter_module.so.old'
cp objs/ngx_http_xslt_filter_module.so '/usr/lib64/nginx/modules/ngx_http_xslt_filter_module.so'
test ! -f '/usr/lib64/nginx/modules/ngx_http_image_filter_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_http_image_filter_module.so' \
                '/usr/lib64/nginx/modules/ngx_http_image_filter_module.so.old'
cp objs/ngx_http_image_filter_module.so '/usr/lib64/nginx/modules/ngx_http_image_filter_module.so'
test ! -f '/usr/lib64/nginx/modules/ngx_http_perl_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_http_perl_module.so' \
                '/usr/lib64/nginx/modules/ngx_http_perl_module.so.old'
cp objs/ngx_http_perl_module.so '/usr/lib64/nginx/modules/ngx_http_perl_module.so'
test ! -f '/usr/lib64/nginx/modules/ngx_mail_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_mail_module.so' \
                '/usr/lib64/nginx/modules/ngx_mail_module.so.old'
cp objs/ngx_mail_module.so '/usr/lib64/nginx/modules/ngx_mail_module.so'
test ! -f '/usr/lib64/nginx/modules/ngx_stream_module.so' \
        || mv '/usr/lib64/nginx/modules/ngx_stream_module.so' \
                '/usr/lib64/nginx/modules/ngx_stream_module.so.old'
cp objs/ngx_stream_module.so '/usr/lib64/nginx/modules/ngx_stream_module.so'
make[1]: Leaving directory `/root/nginx-1.16.1'
[root@lnmp nginx-1.16.1]#

  提示:出現以上提示就表示nginx編譯安裝就完成了;

  4)為了管理方便,我們給nginx添加一個unix_file腳本

[root@lnmp nginx-1.16.1]# cat > /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
 
[Service]
Type=forking
PIDFile=/run/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target
^C
[root@lnmp nginx-1.16.1]# ll /usr/lib/systemd/system/nginx.service
-rw-r--r-- 1 root root 620 Mar 18 13:20 /usr/lib/systemd/system/nginx.service
[root@lnmp nginx-1.16.1]# 

  5)加載systemctl配置文件,啟動nginx

[root@lnmp nginx-1.16.1]# systemctl daemon-reload
[root@lnmp nginx-1.16.1]# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@lnmp nginx-1.16.1]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Wed 2020-03-18 13:22:28 CST; 11s ago
  Process: 14504 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=1/FAILURE)
  Process: 14501 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

Mar 18 13:22:28 lnmp systemd[1]: Starting The nginx HTTP and reverse proxy server...
Mar 18 13:22:28 lnmp nginx[14504]: nginx: the configuration file /etc/nginx/nginx.con... ok
Mar 18 13:22:28 lnmp nginx[14504]: nginx: [emerg] mkdir() "/var/lib/nginx/tmp/client_...ry)
Mar 18 13:22:28 lnmp nginx[14504]: nginx: configuration file /etc/nginx/nginx.conf te...led
Mar 18 13:22:28 lnmp systemd[1]: nginx.service: control process exited, code=exited s...s=1
Mar 18 13:22:28 lnmp systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Mar 18 13:22:28 lnmp systemd[1]: Unit nginx.service entered failed state.
Mar 18 13:22:28 lnmp systemd[1]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.
[root@lnmp nginx-1.16.1]# tail -f /var/log/messages 
Mar 18 01:01:01 lnmp systemd: Starting Session 2 of user root.
Mar 18 01:22:21 lnmp systemd: Reloading.
Mar 18 01:22:28 lnmp systemd: Starting The nginx HTTP and reverse proxy server...
Mar 18 01:22:28 lnmp nginx: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Mar 18 01:22:28 lnmp nginx: nginx: [emerg] mkdir() "/var/lib/nginx/tmp/client_body" failed (2: No such file or directory)
Mar 18 01:22:28 lnmp nginx: nginx: configuration file /etc/nginx/nginx.conf test failed
Mar 18 01:22:28 lnmp systemd: nginx.service: control process exited, code=exited status=1
Mar 18 01:22:28 lnmp systemd: Failed to start The nginx HTTP and reverse proxy server.
Mar 18 01:22:28 lnmp systemd: Unit nginx.service entered failed state.
Mar 18 01:22:28 lnmp systemd: nginx.service failed.

  提示:啟動nginx時它提示我們缺少/var/lib/nginx/tmp/client_body這個目錄  ,我們直接創建即可

[root@lnmp nginx-1.16.1]# mkdir -p /var/lib/nginx/tmp/client_body
[root@lnmp nginx-1.16.1]# systemctl start nginx
[root@lnmp nginx-1.16.1]# ss -ntl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128               *:80                            *:*                  
LISTEN      0      128               *:22                            *:*                  
LISTEN      0      100       127.0.0.1:25                            *:*                  
LISTEN      0      128              :::22                           :::*                  
LISTEN      0      100             ::1:25                           :::*                  
[root@lnmp nginx-1.16.1]# 

  提示:可以看到我們把對應目錄創建起來,nginx啟動就沒有任何問題;到此nginx編譯安裝就完成了;我們可以選擇用瀏覽器訪問nginx測試主頁,如果能看到,說明我們編譯的nginx是沒有問題的;

   提示:我們訪問nginx對外監聽的地址,告訴我們沒有權限;這不是nginx的問題 ,是我們對應目錄下沒有資源,我們可以在其根目錄創建一主頁文件即可;

[root@lnmp ~]# ll /usr/share/nginx/html/
total 0
[root@lnmp ~]# echo "this is test page." > /usr/share/nginx/html/index.html
[root@lnmp ~]# 

   提示:到此我們能夠正常訪問的nginx的測試頁面,接下來我們再來編譯安裝mysql和php

   二、編譯安裝mariadb

  1)下載源碼包,並將源碼包上傳至服務器並解壓

[root@lnmp ~]# ls
nginx-1.16.1  nginx-1.16.1.tar.gz
[root@lnmp ~]# rz
rz waiting to receive.
 zmodem trl+C ȡ

  100%   70172 KB 70172 KB/s 00:00:01       0 Errors..
Transferring mariadb-10.2.19.tar.gz...

[root@lnmp ~]# ls
mariadb-10.2.19.tar.gz  nginx-1.16.1  nginx-1.16.1.tar.gz
[root@lnmp ~]# tar xf mariadb-10.2.19.tar.gz 
[root@lnmp ~]# 

  2)安裝環境依賴包

[root@lnmp ~]# yum install bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel gcc gcc-c++ cmake ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel -y

  3)創建系統賬號

[root@lnmp ~]# getent passwd mysql
[root@lnmp ~]# useradd -r mysql -s /sbin/nologin -d /app/mysql
[root@lnmp ~]# getent passwd mysql                            
mysql:x:995:993::/app/mysql:/sbin/nologin
[root@lnmp ~]# 

  4)進入到解壓后的目錄,cmake 編譯並指定編譯選項

[root@lnmp ~]# cd mariadb-10.2.19
[root@lnmp mariadb-10.2.19]# cmake . \
> -DCMAKE_INSTALL_PREFIX=/app/mysql \
> -DMYSQL_DATADIR=/data/mysql/ \
> -DSYSCONFDIR=/etc/mysql \
> -DMYSQL_USER=mysql \
> -DWITH_INNOBASE_STORAGE_ENGINE=1 \
> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \
> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
> -DWITH_PARTITION_STORAGE_ENGINE=1 \
> -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \
> -DWITH_DEBUG=0 \
> -DWITH_READLINE=1 \
> -DWITH_SSL=system \
> -DWITH_ZLIB=system \
> -DWITH_LIBWRAP=0 \
> -DENABLED_LOCAL_INFILE=1 \
> -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \
> -DDEFAULT_CHARSET=utf8 \
> -DDEFAULT_COLLATION=utf8_general_ci

  提示:如果中間編譯環境檢查出現error環境缺少庫或者包的情況,我們需要安裝對應的包和庫,然后刪除CMakeCache.txt文件后,在運行cmake指定的編譯選項來檢查環境,以及生成makefile文件;

-- Looking for strndup - found
-- Looking for krb5_free_unparsed_name
-- Looking for krb5_free_unparsed_name - found
-- Looking for event.h
-- Looking for event.h - found
-- Configuring done
-- Generating done
-- Build files have been written to: /root/mariadb-10.2.19
[root@lnmp mariadb-10.2.19]# 

  提示:如果環境檢查沒有什么問題,就不會出現error的字眼,如上,就表示環境檢查沒有問題,接下來我們就可以直接來編譯安裝了

[root@lnmp mariadb-10.2.19]# make -j 4&& make install 

  提示:-j表示啟用多線程來編譯 -j 4表示4個線程同時進行編譯,這樣對於編譯大型項目是可以縮短一定的時間;

-- Installing: /app/mysql/support-files/policy/apparmor/README
-- Installing: /app/mysql/support-files/policy/apparmor/usr.sbin.mysqld
-- Installing: /app/mysql/support-files/policy/apparmor/usr.sbin.mysqld.local
-- Installing: /app/mysql/share/pkgconfig/mariadb.pc
-- Installing: /app/mysql/share/aclocal/mysql.m4
-- Installing: /app/mysql/support-files/mysql.server
[root@lnmp mariadb-10.2.19]# 

  提示:如果編譯安裝過程中沒有出現error的情況,就表示編譯安裝沒有問題如上

  通常情況下我們編譯安裝指定的路徑都沒有在path環境變量中,接下來我們給mariadb的路徑加入到環境變量中,讓其我們使用不用每次都寫絕對路徑

[root@lnmp mariadb-10.2.19]# echo 'PATH=/app/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lnmp mariadb-10.2.19]# cat /etc/profile.d/mysql.sh
PATH=/app/mysql/bin:$PATH
[root@lnmp mariadb-10.2.19]# . /etc/profile.d/mysql.sh
[root@lnmp mariadb-10.2.19]# 

  提示:環境變量准備好后,接下來生成數據庫文件

[root@lnmp mariadb-10.2.19]# cd /app/mysql/
[root@lnmp mysql]# scripts/mysql_install_db --datadir=/data/mysql --user=mysql
Installing MariaDB/MySQL system tables in '/data/mysql' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system


PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:

'./bin/mysqladmin' -u root password 'new-password'
'./bin/mysqladmin' -u root -h lnmp password 'new-password'

Alternatively you can run:
'./bin/mysql_secure_installation'

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.

You can start the MariaDB daemon with:
cd '.' ; ./bin/mysqld_safe --datadir='/data/mysql'

You can test the MariaDB daemon with mysql-test-run.pl
cd './mysql-test' ; perl mysql-test-run.pl

Please report any problems at http://mariadb.org/jira

The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/

[root@lnmp mysql]# 

  提示:生成數據庫相關文件,我們需要用到scripts/mysql_install_db腳本,同時指定數據存放目錄 和創建這些文件的用戶即可,系統數據庫相關文件生成完成后,我們需要給mariadb准備配置文件,接下來准備配置文件

[root@lnmp mysql]# cp /app/mysql/support-files/my-huge.cnf /etc/my.cnf
cp: overwrite ‘/etc/my.cnf’? y
[root@lnmp mysql]# 

  提示:mariadb自身提供了幾個配置文件示例文件,我們可以選擇對應的配置文件,然后復制到我們編譯時指定的配置文件目錄,並改名為對應的文件名即可;

  准備啟動腳本

[root@lnmp mysql]# cp /app/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@lnmp mysql]# 

  提示:mairadb也提供了一個bash腳本啟動腳本,我們可以將其直接復制到/etc/init.d/並改名為mysqld,當然這個啟動腳本也可以自己寫,或者直接弄一個unix file腳本用systemctl來管理mairadb 也是可以的

  把腳本加入到chkconfig中,設置為開機啟動,並啟動服務

[root@lnmp mysql]# chkconfig --add mysqld
[root@lnmp mysql]# chkconfig --list mysqld

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@lnmp mysql]# service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]
[root@lnmp mysql]# ss -ntl
State       Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN      0      128               *:80                            *:*                  
LISTEN      0      128               *:22                            *:*                  
LISTEN      0      100       127.0.0.1:25                            *:*                  
LISTEN      0      128              :::22                           :::*                  
LISTEN      0      100             ::1:25                           :::*                  
LISTEN      0      80               :::3306                         :::*                  
[root@lnmp mysql]# 

  提示:默認情況我們把mysqld加入到chkconfig中,3模式默認就是開機啟動,所以我們這里可以不用指定3級別運行時開機啟動;可以看到我們把腳本加入到chkconfig中后,直接用service命令將mariadb給啟動起來了;

  測試:我們用本地客戶端連接mairadb看看是否能夠連接上,並進入mariadb數據庫?

[root@lnmp mysql]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.19-MariaDB-log Source distribution

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)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

MariaDB [(none)]> \q
Bye
[root@lnmp mysql]# 

  提示:到此mairadb編譯就完成了,后續我們就可以來配置mariadb使用mariadb存儲wordpress程序所需要用到數據;

   三、編譯安裝php

  1)准備php源碼包

[root@lnmp ~]# ls
mariadb-10.2.19  mariadb-10.2.19.tar.gz  nginx-1.16.1  nginx-1.16.1.tar.gz
[root@lnmp ~]# wget https://www.php.net/distributions/php-7.4.3.tar.bz2
--2020-03-18 15:33:23--  https://www.php.net/distributions/php-7.4.3.tar.bz2
Resolving www.php.net (www.php.net)... 185.85.0.29, 2a02:cb40:200::1ad
Connecting to www.php.net (www.php.net)|185.85.0.29|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 12695760 (12M) [application/octet-stream]
Saving to: ‘php-7.4.3.tar.bz2’

100%[======================================================================================>] 12,695,760  1.35MB/s   in 11s    

2020-03-18 15:33:35 (1.12 MB/s) - ‘php-7.4.3.tar.bz2’ saved [12695760/12695760]

[root@lnmp ~]# ls
mariadb-10.2.19  mariadb-10.2.19.tar.gz  nginx-1.16.1  nginx-1.16.1.tar.gz  php-7.4.3.tar.bz2
[root@lnmp ~]# tar xf php-7.4.3.tar.bz2 
[root@lnmp ~]# cd php-7.4.3
[root@lnmp php-7.4.3]# ls
appveyor             buildconf.bat        docs        NEWS                 README.REDIST.BINS  travis               Zend
azure                CODING_STANDARDS.md  ext         pear                 run-tests.php       TSRM
azure-pipelines.yml  configure            EXTENSIONS  php.ini-development  sapi                UPGRADING
build                configure.ac         LICENSE     php.ini-production   scripts             UPGRADING.INTERNALS
buildconf            CONTRIBUTING.md      main        README.md            tests               win32
[root@lnmp php-7.4.3]# 

  2)指定編譯參數,檢查編譯環境

[root@lnmp php-7.4.3]# ./configure --prefix=/app/php \
> --enable-mysqlnd \
> --with-mysqli=mysqlnd \
> --with-openssl \
> --with-pdo-mysql=mysqlnd \
> --enable-mbstring \
> --with-freetype-dir \
> --with-jpeg-dir \
> --with-png-dir \
> --with-zlib \
> --with-libxml-dir=/usr \
> --enable-xml \
> --enable-sockets \
> --with-apxs2=/usr/bin/apxs \
> --with-config-file-path=/etc \
> --with-config-file-scan-dir=/etc/php.d \
> --enable-maintainer-zts \
> --disable-fileinfo

  提示:如果以上環境檢查沒有問題,即可進行編譯安裝,如果有問題,根據提示安裝環境依賴包組件;如下

Configuring SAPI modules
checking for Apache 2 handler module support via DSO through APXS... 

Sorry, I cannot run apxs.  Possible reasons follow:

1. Perl is not installed
2. apxs was not found. Try to pass the path using --with-apxs2=/path/to/apxs
3. Apache was not built using --enable-so (the apxs usage page is displayed)

The output of /usr/bin/apxs follows:
./configure: line 6336: /usr/bin/apxs: No such file or directory
configure: error: Aborting
[root@lnmp php-7.4.3]# 

  提示:以上提示我們/usr/bin/apxs未找到,我們安裝httpd-devel包即可解決

[root@lnmp php-7.4.3]# yum install -y httpd-devel

  提示:安裝好缺少的包后,然后在進行環境檢查,看看還有其他問題

checking for sqlite3 > 3.7.4... no
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@lnmp php-7.4.3]# 

  提示:系統告訴我們所sqlite3沒有,我們安裝sqlite-devel試試

checking whether to enable multibyte regex support (requires oniguruma)... yes
checking for oniguruma... no
configure: error: Package requirements (oniguruma) were not met:

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.

  提示:安裝sqlite-devel包后,重新檢查環境提示我們沒有oniguruma,我們可以嘗試安裝oniguruma-devel試試

+--------------------------------------------------------------------+
| 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.

configure: WARNING: unrecognized options: --with-freetype-dir, --with-jpeg-dir, --with-png-dir, --with-libxml-dir
[root@lnmp php-7.4.3]# 

  提示:最后能夠看到thank you for using php就表示我們可以編譯安裝php了;

[root@lnmp php-7.4.3]# make -j 8 && make install
/bin/sh /root/php-7.4.3/libtool --silent --preserve-dup-deps --mode=compile cc -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -Iext/standard/ -I/root/php-7.4.3/ext/standard/ -DPHP_ATOM_INC -I/root/php-7.4.3/include -I/root/php-7.4.3/main -I/root/php-7.4.3 -I/root/php-7.4.3/ext/date/lib -I/usr/include/libxml2 -I/root/php-7.4.3/ext/mbstring/libmbfl -I/root/php-7.4.3/ext/mbstring/libmbfl/mbfl -I/root/php-7.4.3/TSRM -I/root/php-7.4.3/Zend  -D_REENTRANT -pthread  -I/usr/include -g -O2 -fvisibility=hidden -pthread -Wall -Wno-strict-aliasing -DZTS -DZEND_SIGNALS   -c /root/php-7.4.3/ext/standard/info.c -o ext/standard/info.lo 
……省略部分信息……
Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /app/php/lib/php/extensions/no-debug-zts-20190902/
Installing PHP CLI binary:        /app/php/bin/
Installing PHP CLI man page:      /app/php/php/man/man1/
Installing PHP FPM binary:        /app/php/sbin/
Installing PHP FPM defconfig:     /app/php/etc/
Installing PHP FPM man page:      /app/php/php/man/man8/
Installing PHP FPM status page:   /app/php/php/php/fpm/
Installing phpdbg binary:         /app/php/bin/
Installing phpdbg man page:       /app/php/php/man/man1/
Installing PHP CGI binary:        /app/php/bin/
Installing PHP CGI man page:      /app/php/php/man/man1/
Installing build environment:     /app/php/lib/php/build/
Installing header files:          /app/php/include/php/
Installing helper programs:       /app/php/bin/
  program: phpize
  program: php-config
Installing man pages:             /app/php/php/man/man1/
  page: phpize.1
  page: php-config.1
/root/php-7.4.3/build/shtool install -c ext/phar/phar.phar /app/php/bin
ln -s -f phar.phar /app/php/bin/phar
Installing PDO headers:           /app/php/include/php/ext/pdo/
[root@lnmp php-7.4.3]#

  提示:到此php就編譯安裝好了,接下給php提供給配置文件和服務啟動腳本

[root@lnmp php-7.4.3]# cp php.ini-production /etc/php.ini
[root@lnmp php-7.4.3]# cp sapi/fpm/init.d.php-fpm.in /etc/init.d/php-fpm
[root@lnmp php-7.4.3]# chmod +x /etc/init.d/php-fpm
[root@lnmp php-7.4.3]# chkconfig --add php-fpm
[root@lnmp php-7.4.3]# chkconfig php-fpm on
[root@lnmp php-7.4.3]#  cd /app/php/etc/
[root@lnmp etc]# cp php-fpm.conf.default php-fpm.conf
[root@lnmp etc]# cp php-fpm.d/www.conf.default php-fpm.d/www.conf
[root@lnmp etc]# 

  提示:到此我們就把php fpm方式的腳本和配置文件就准備好了,接下就可以啟動php-fpm了

[root@lnmp etc]# service php-fpm start
Starting php-fpm /etc/init.d/php-fpm: line 57: @sbindir@/php-fpm: No such file or directory
 failed
[root@lnmp etc]#

  提示:啟動提示我們@sbindir@/php-fpm沒有找到,這很明顯是一個變量沒有賦值引起的問題,我們可以把腳本稍微改動下,把我們php安裝路徑告訴腳本相關變量,即可啟動起來,如下

    提示:就只需要根據自己的環境來設定這幾個變量就好了

[root@lnmp ~]# ss -ntl
State      Recv-Q Send-Q                   Local Address:Port                                  Peer Address:Port              
LISTEN     0      128                                  *:80                                               *:*                  
LISTEN     0      128                                  *:22                                               *:*                  
LISTEN     0      100                          127.0.0.1:25                                               *:*                  
LISTEN     0      128                                 :::22                                              :::*                  
LISTEN     0      100                                ::1:25                                              :::*                  
LISTEN     0      80                                  :::3306                                            :::*                  
[root@lnmp ~]# service  php-fpm start
Starting php-fpm  done
[root@lnmp ~]# ss -ntl
State      Recv-Q Send-Q                   Local Address:Port                                  Peer Address:Port              
LISTEN     0      128                                  *:80                                               *:*                  
LISTEN     0      128                                  *:22                                               *:*                  
LISTEN     0      100                          127.0.0.1:25                                               *:*                  
LISTEN     0      128                          127.0.0.1:9000                                             *:*                  
LISTEN     0      128                                 :::22                                              :::*                  
LISTEN     0      100                                ::1:25                                              :::*                  
LISTEN     0      80                                  :::3306                                            :::*                  
[root@lnmp ~]# 

  提示:到此php就編譯完成了,接下來就是把nginx和php相連,php同mariadb要能夠互相通信即可

  首先配置nginx,讓其把.php結尾的請求反代到后端127.0.0.1:9000端口上

   提示:以上配置信息表示,訪問.php結尾的資源,ngxin都會通過fastcgi協議反代之127.0.0.1:9000端口處理,后端php的工作目錄為/usr/share/nginx/html,意思就是我們需要把.php的腳本放到該目錄才能夠php執行后響應給客戶端;

  測試:在php的工作目錄下新建一個測試腳本,看看nginx是否能夠代理.php結尾的資源?

[root@lnmp ~]# cd /usr/share/nginx/html/
[root@lnmp html]# ls
index.html
[root@lnmp html]# cat >> test.php
<?php
        phpinfo();
?>   
^C
[root@lnmp html]# cat test.php 
<?php
        phpinfo();
?>
[root@lnmp html]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lnmp html]# nginx -s reload
[root@lnmp html]# 

  提示:我們在php的工作目錄里寫了一個測試腳本,我們如果用瀏覽器訪問test.php能夠拿到對應的php信息的一個頁面就說明nginx 代理php的請求是沒有問題的

  提示:可以看到我們是可以正常訪問php信息頁的,說明nginx和php的交互沒有問題了,接下我們還需要測試php連接mariadb數據

  測試:php連接mairiadb

  我們在php工作目錄創建一個連接數據庫測試腳本,如果php能夠連接mysql就打印ok,不能就打印Failure,在這之前我們需要到數據庫里創建一個賬號,用於php應用服務器連接mysql用

MariaDB [(none)]> grant all on *.* to 'php_app'@'%' identified by 'admin';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

  提示:創建好賬號后,我們就可以寫測試腳本了

    提示:創建好php連接mysql的測試腳本后,我們可以通過瀏覽器訪問該腳本,就可以看到php是否能夠連接mysql數據庫了

    提示:可以看到顯示是OK的,這就說明php是可以正常連接mysql數據庫的,到此LNMP的環境就准備好了,接下來我們就可以把我們的wordpress 給部署到LNMP環境中即可以;

  部署wordpress

  1)將wordpress源碼程序解壓放到nginx和php的工作目錄(如果是多台服務器,需要在nginx前端代理放一份,用於靜態資源的響應,另外還需要在php-fpm服務器上放一份,用於動態頁面的解析,如果是像我這種測試環境,LNMP在一台主機上,放一份即可)

  提示:我們把wordpress程序放到我們預先搭建好的LNMP環境中,接下來就可以直接瀏覽器上訪問對應的目錄下的index.php就可以進行安裝wordpress了

  在此之前我們需要在數據庫里事先創建一個庫用於存放wordpress程序各個表格數據

MariaDB [(none)]> create database wordpress;
Query OK, 1 row affected (0.00 sec)

  到此我們就可以通過瀏覽器來安裝wordpress

   提示:到此wordpress就安裝好了,我們就可以通過安裝時指定的用戶名和密碼進行登錄到后台管理wordpress

  2、配置虛擬主機,www.x.com域名實現首頁訪問;admin.x.com域名實現wordpress的后台訪問

   提示:以上配置表示3個虛擬主機,第一個是基於域名www.x.com的配置,第二個是基於域名admin.x.com的配置,第三個是基於主機名是localhost或者IP地址的虛擬主機的配置;

  測試:

 

  3、配置nginx反向代理,實現api.x.com域名代理本地9001端口

 

   提示:在nginx里創建一個基於本地9001的server,然后在建立一個server將其用作代理來把請求代理至192.168.0.11:9001上;

  測試:

  單獨訪問9001端口

   通過aip.x.com代理

   提示:做以上是實驗需要在windows主機上做好hosts解析;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM