溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

CentOS 7上源碼編譯安裝和配置LNMP Web+phpMyAdmin服務(wù)器環(huán)境

發(fā)布時(shí)間:2020-08-06 11:24:35 來源:網(wǎng)絡(luò) 閱讀:8343 作者:HMLinux 欄目:建站服務(wù)器

什么是LNMP?

 

LNMP(別名LEMP)是指由Linux, Nginx, MySQL/MariaDB, PHP/Perl/Python組合成的動(dòng)態(tài)Web應(yīng)用程序和服務(wù)器,它是一組Web應(yīng)用程序的基礎(chǔ)軟件包,在這個(gè)基礎(chǔ)環(huán)境上我們可以搭建任何使用PHP/Perl/Python等語言的動(dòng)態(tài)網(wǎng)站,如商務(wù)網(wǎng)站、博客、論壇和開源Web應(yīng)用程序軟件等,它是互聯(lián)網(wǎng)上被廣泛使用的Web網(wǎng)站架構(gòu)之一。

 

部署方式

 

從網(wǎng)站規(guī)模大小(訪問流量、注冊用戶等)角度來看,LNMP架構(gòu)可以使用單機(jī)部署方式和集群部署方式。單機(jī)部署方式即所有的軟件都部署在一臺(tái)Linux服務(wù)器上;集群部署方式可以將Nginx網(wǎng)絡(luò)服務(wù)器,MySQL/MariaDB數(shù)據(jù)庫,PHP軟件分開安裝到不同的服務(wù)器上,它們彼此之間通過TCP網(wǎng)絡(luò)協(xié)議相互通信協(xié)助工作,以及對外提供Web應(yīng)用訪問服務(wù)。因此,當(dāng)單臺(tái)服務(wù)器不能滿足性能要求時(shí),可以使用集群方式部署。

 

本教程將指導(dǎo)您如何在RHEL 7/CentOS 7以及Fedora 23/24/25等衍生版本中使用Nginx 1.10, MariaDB 10PHP 6安裝LNMP應(yīng)用服務(wù)器環(huán)境。(在其他系統(tǒng)如SUSE、Ubuntu中源碼編譯安裝LNMP同樣可以參考本教程。)


前提要求

 

準(zhǔn)備2臺(tái)RHEL 7CentOS 7服務(wù)器,一臺(tái)用于安裝MariaDB,另一臺(tái)用于安裝NginxPHP。當(dāng)然你也可以僅僅使用1臺(tái)服務(wù)器部署LNMP以通過實(shí)驗(yàn)來驗(yàn)證LNMP。

HOSTNAMEIPOSNODE

hming-server217-mdb

10.0.6.217CentOS 7.2 x86_64MariaDB
hming-server218-web10.0.6.218CentOS 7.2 x86_64MariaDB,Nginx,PHP


主軟件包(源碼包)

 

DB      mariadb-10.1.20.tar.gz

PHP     php-5.6.30.tar.bz2

Nginx   nginx-1.10.2.tar.gz


注意:主軟件包在這之前我已經(jīng)提前下載好了,可能并不是最新和穩(wěn)定版,你也可以根據(jù)你的需求選擇其他版本。


依賴軟件


源碼編譯安裝LNMP有許多依賴軟件(如gcc、make)是必要的,一般可以通過YUM軟件管理程序來安裝,另外小部分需要源碼編譯安裝。


通過yum安裝gcc、gcc-c++、make等必需軟件包

# yum install -y gcc gcc-c++ make automake


安裝MariaDB


1. 使用yum安裝gcc、makebison、ncurses-devel等依賴軟件包

# yum install zlib-devel ncurses ncurses-devel bison


2. 安裝cmake,cmake是編譯MySQL/MariaDB的必要工具(MySQL5.6/MariaDB 10及以后的版本) 你可以在網(wǎng)站https://cmake.org/中下載cmake軟件包

 

將軟件包上傳到/usr/local/src目錄下,然后解壓cmake包,cdcmake目錄執(zhí)行./bootstrap腳本進(jìn)行安裝,同時(shí)可以使用--prefix=<install dir>選項(xiàng)指定安裝的目錄。

# tar -zxf cmake-3.7.2.tar.gz
# cd cmake-3.7.2/
# ./bootstrap --prefix=/usr/local/cmake
# make && make install


當(dāng)安裝完之后,你可以使用執(zhí)行/usr/local/cmake/bin/cmake --version查看安裝的cmake版本

[root@hming-server217-mdb ~ ]# /usr/local/cmake/bin/cmake --version
cmake version 3.7.2
 
CMake suite maintained and supported by Kitware (kitware.com/cmake).


3. 安裝完cmake編譯工具之后,接下來我們安裝MariaDB(你可以訪問https://mariadb.org/下載MariaDB), 首先創(chuàng)建MariaDB安裝目錄/usr/local/mysql和數(shù)據(jù)庫服務(wù)器的數(shù)據(jù)目錄/data/mysql/webdata,并創(chuàng)建MySQL用戶,并將/data/mysql/webdata目錄的所屬主權(quán)限修改為mysql用戶


創(chuàng)建安裝目錄和數(shù)據(jù)目錄

# mkdir /usr/local/mysql
# mkdir /data/mysql/webdata -p


創(chuàng)建mysql用戶,并修改數(shù)據(jù)目錄的權(quán)限

# groupadd mysql
# useradd -r -g mysql -s /sbin/nologin mysql -M
# chown -R mysql:mysql /data/mysql/


4. 編譯安裝MariaDB,將MariaDB軟件包解壓后使用cmake進(jìn)行編譯,根據(jù)需求選擇相應(yīng)的編譯參數(shù)進(jìn)行編譯(查看編譯選項(xiàng)/usr/local/cmake/bin/cmake . -LH).  (或者訪問MariaDB編譯安裝幫助文檔https://mariadb.com/kb/en/mariadb/compiling-mariadb-from-source/)

[root@hming-server217-mdb /usr/local/src ]# tar -zxf mariadb-10.1.20.tar.gz
[root@hming-server217-mdb /usr/local/src ]# cd mariadb-10.1.20/
 
[root@hming-server217-mdb /usr/local/src/mariadb-10.1.20 ]# /usr/local/cmake/bin/cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql/webdata -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DDEFAULT_CHARSET=utf8 -DEXTRA_CHARSETS=all -DDEFAULT_COLLATION=utf8_general_ci -DMYSQL_UNIX_ADDR=/data/mysql/webdata/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_MEMORY_STORAGE_ENGINE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_EMBEDDED_SERVER=1 -DWITH_READLINE=1 -DWITH_DEBUG=0


執(zhí)行gmake gmake install進(jìn)行最后的安裝

[root@hming-server217-mdb /usr/local/src/mariadb-10.1.20 ]# gmake
[root@hming-server217-mdb /usr/local/src/mariadb-10.1.20 ]# gmake install


5. 配置mysql環(huán)境變量,如果不配置默認(rèn)系統(tǒng)找不到mysql的可執(zhí)行程序和命令,否則你必須使用全路徑執(zhí)行與mysql相關(guān)的任何命令,查看系統(tǒng)默認(rèn)的環(huán)境變量


使用export命令添加mysql環(huán)境變量

[root@hming-server217-mdb ~ ]# export PATH=$PATH:/usr/local/mysql/bin
[root@hming-server217-mdb ~ ]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/mysql/bin


最后將mysql環(huán)境配置添加到/etc/profile或者/etc/profile.d/的自定義文件中

[root@hming-server217-mdb ~ ]# echo "export PATH=$PATH:/usr/local/mysql/bin" > /etc/profile.d/mysql.sh
[root@hming-server217-mdb ~ ]# source /etc/profile.d/mysql.sh


配置了環(huán)境變量,我們就可以直接在shell終端下執(zhí)行mysql命令,使用mysql -V查看mysql版本

[root@hming-server217-mdb ~ ]# mysql -V
mysql  Ver 15.1 Distrib 10.1.20-MariaDB, for Linux (x86_64) using readline 5.1


6. 添加MariaDB庫文件

[root@hming-server217-mdb ~ ]# echo "/usr/local/mysql/lib" >> /etc/ld.so.conf.d/mariadb-x86_64.conf


7. 拷貝MariaDB服務(wù)啟動(dòng)腳本到/etc/init.d/目錄中

[root@hming-server217-mdb ~ ]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@hming-server217-mdb ~ ]# chmod 755 /etc/init.d/mysqld
[root@hming-server217-mdb ~ ]# chkconfig --add mysqld
[root@hming-server217-mdb ~ ]# chkconfig mysqld on
[root@hming-server217-mdb ~ ]# systemctl daemon-reload


8. 執(zhí)行初始化數(shù)據(jù)庫腳本,安裝MariaDB基礎(chǔ)數(shù)據(jù)庫(執(zhí)行./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/webdata --user=mysql --no-defaults)

[root@hming-server217-mdb ~ ]# cd /usr/local/mysql/scripts/
[root@hming-server217-mdb /usr/local/mysql/scripts ]# ./mysql_install_db --basedir=/usr/local/mysql --datadir=/data/mysql/webdata --user=mysql --no-defaults
[root@hming-server217-mdb /usr/local/mysql/scripts ]# ls /data/mysql/webdata/
aria_log.00000001  ibdata1      ib_logfile1  performance_schema
aria_log_control   ib_logfile0  mysql        test


9. 配置/etc/my.cnf,該文件是mysql服務(wù)的主要配置文件

[root@hming-server217-mdb ~ ]# cp /etc/my.cnf /etc/my.cnf.save
[root@hming-server217-mdb ~ ]# cat /etc/my.cnf
[client]
port=3306
socket=/data/mysql/webdata/mysql.sock
default-character-set=utf8
 
[mysqld]
port=3306
user=mysql
basedir=/usr/local/mysql
datadir=/data/mysql/webdata
socket=/data/mysql/webdata/mysql.sock
character-set-server=utf8
external-locking=FALSE
skip-name-resolv
default-storage-engine=InnoDB
back_log=1024
transaction_isolation=REPEATABLE-READ
max_connections=5000
max_connect_errors=6000
open_files_limit=65535
table_open_cache=512
 
[mysqldump]
quick
max_allowed_packet=32M
 
[mysql]
no-auto-rehash
default-character-set=utf8
 
[mysqld_safe]
open-files-limit=8192
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid


創(chuàng)建/var/log/mariadb/var/run/mariadb目錄

[root@hming-server217-mdb ~ ]# mkdir /var/log/mariadb
[root@hming-server217-mdb ~ ]# mkdir /var/run/mariadb
[root@hming-server217-mdb ~ ]# chown mysql:mysql /var/log/mariadb
[root@hming-server217-mdb ~ ]# chown mysql:mysql /var/run/mariadb


10. 啟動(dòng)MariaDB服務(wù)

[root@hming-server217-mdb ~ ]# systemctl start mysqld
[root@hming-server217-mdb ~ ]# systemctl status mysqld

[root@hming-server217-mdb ~ ]# systemctl status mysqld
● mysqld.service - LSB: start and stop MySQL
   Loaded: loaded (/etc/rc.d/init.d/mysqld)
   Active: active (running) since Fri 2017-06-02 23:53:09 CST; 3 days ago
     Docs: man:systemd-sysv-generator(8)
  Process: 27419 ExecStop=/etc/rc.d/init.d/mysqld stop (code=exited, status=0/SUCCESS)
  Process: 27449 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/mysqld.service
           ├─27460 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/webdata --pid-file=/data/mysq...
           └─27595 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/webdata --plugi...

Jun 02 23:53:08 hming-server217-mdb systemd[1]: Starting LSB: start and stop MySQL...
Jun 02 23:53:08 hming-server217-mdb mysqld[27449]: Starting MySQL.170602 23:53:08 mysqld_safe Logging to '/...og'.
Jun 02 23:53:09 hming-server217-mdb mysqld[27449]: SUCCESS!
Jun 02 23:53:09 hming-server217-mdb systemd[1]: Started LSB: start and stop MySQL.
Hint: Some lines were ellipsized, use -l to show in full.


查看MariaDB服務(wù)進(jìn)程

[root@hming-server217-mdb ~ ]# ps aux | grep mysql
root     27460  0.0  0.0 115380  1744 ?        S    23:53   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/webdata --pid-file=/data/mysql/webdata/hming-server217-mdb.pid
mysql    27595  1.0  5.2 1209380 99428 ?       Sl   23:53   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/webdata --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mariadb/mariadb.log --open-files-limit=8192 --pid-file=/data/mysql/webdata/hming-server217-mdb.pid --socket=/data/mysql/webdata/mysql.sock --port=3306
root     27627  0.0  0.0 112644   952 pts/2    S+   23:53   0:00 grep --color=auto mysql


11. 執(zhí)行mysql_secure_installation初始化數(shù)據(jù)庫,設(shè)mysql用戶root密碼

[root@hming-server217-mdb ~ ]# mysql_secure_installation
 
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!
 
In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
 
Enter current password for root (enter for none):
OK, successfully used password, moving on...
 
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
 
Set root password? [Y/n] y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!
 
 
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.
 
Remove anonymous users? [Y/n] y
 ... Success!
 
Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.
 
Disallow root login remotely? [Y/n] y
 ... Success!
 
By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.
 
Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!
 
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
 
Reload privilege tables now? [Y/n] y
 ... Success!
 
Cleaning up...
 
All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.
 
Thanks for using MariaDB!


12登錄MariaDB數(shù)據(jù)庫

[root@hming-server217-mdb ~ ]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 353
Server version: 10.1.20-MariaDB Source distribution
 
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> select version();
+-----------------+
| version()       |
+-----------------+
| 10.1.20-MariaDB |
+-----------------+
1 row in set (0.00 sec)
 
MariaDB [(none)]>


安裝Nginx


1. 安裝依賴軟件包

[root@hming-server218-web ~ ]# yum install gcc gcc-c++ zlib zlib-devel pcre pcre-devel openssl-devel gd gd-devel perl-devel perl-ExtUtils-Embed


2. 創(chuàng)建nginx用戶

[root@hming-server218-web ~ ]# groupadd nginx
[root@hming-server218-web ~ ]# useradd -g nginx -s /sbin/nologin nginx -M


3. 編譯安裝Nginx

[root@hming-server218-web /usr/local/src ]# tar -zxf nginx-1.10.2.tar.gz
[root@hming-server218-web /usr/local/src ]# cd nginx-1.10.2/
[root@hming-server218-web /usr/local/src/nginx-1.10.2 ]# ./configure \
--user=nginx \
--group=nginx \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/subsys/nginx \
--with-http_stub_status_module \
--with-pcre \
--with-http_ssl_module \
--with-mail_ssl_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log \
--http-client-body-temp-path=/tmp/nginx/client_body \
--http-proxy-temp-path=/tmp/nginx/proxy \
--http-fastcgi-temp-path=/tmp/nginx/fastcgi \
--http-uwsgi-temp-path=/tmp/nginx/uwsgi \
--with-http_degradation_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_sub_module \
--with-http_perl_module \
--with-http_p_w_picpath_filter_module=dynamic \
--with-http_flv_module

[root@hming-server218-web /usr/local/src/nginx-1.10.2 ]# make && make install


4. 啟動(dòng)Nginx服務(wù)

[root@hming-server218-web ~ ]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@hming-server218-web ~ ]# mkdir /tmp/nginx
[root@hming-server218-web ~ ]# nginx
[root@hming-server218-web ~ ]# ps -ef | grep nginx
root     27913     1  0 00:49 ?        00:00:00 nginx: master process nginx
nginx    27914 27913  0 00:49 ?        00:00:00 nginx: worker process
root     27923 22381  0 00:49 pts/1    00:00:00 grep --color=auto nginx

5. 添加防火墻

[root@hming-server218-web ~ ]# firewall-cmd --permanent --add-port=80/tcp
success
[root@hming-server218-web ~ ]# firewall-cmd --reload
success


6. 通過客戶端訪問http://10.0.6.218

CentOS 7上源碼編譯安裝和配置LNMP Web+phpMyAdmin服務(wù)器環(huán)境


安裝PHP


  1. 安裝依賴軟件包

[root@hming-server218-web ~ ]# yum install gcc gcc-c++ libtool libxslt-devel libpng libpng-devel bzip2 bzip2-devel libxml2-devel libXpm-devel libcurl-devel curl libmcrypt expat libxslt freetype freetype-devel libmcrypt-devel autoconf libpng zlib-devel zlib net-snmp net-snmp-devel

2. 安裝libiconv

[root@hming-server218-web ~ ]# tar -zxf libiconv-1.15.tar.gz  
[root@hming-server218-web ~ ]# cd libiconv-1.15/
[root@hming-server218-web ~/libiconv-1.15 ]# ./configure --prefix=/usr/local/libiconv
[root@hming-server218-web ~/libiconv-1.15 ]# make && make install
[root@hming-server218-web ~ ]# echo "/usr/local/libiconv/lib" >> /etc/ld.so.conf
[root@hming-server218-web ~ ]# ldconfig


3. 安裝libmcrypt

[root@hming-server218-web ~ ]# tar -jxf libmcrypt-2.5.8.tar.bz2
[root@hming-server218-web ~ ]# cd libmcrypt-2.5.8/
[root@hming-server218-web ~/libmcrypt-2.5.8 ]# ./configure --prefix=/usr/local/libmcrypt
[root@hming-server218-web ~/libmcrypt-2.5.8 ]# make && make install

4. 安裝 jpeg

# tar -zxf jpegsrc.v9b.tar.gz
# cd jpeg-9b/
# mkdir /usr/local/jpeg9 && mkdir /usr/local/jpeg9/{bin,lib,include}
# mkdir /usr/local/jpeg9/man/man1 -p
#  cp -rf /usr/share/libtool/config/config.sub . && cp -rf /usr/share/libtool/config/config.guess .
cp: overwrite \u2018./config.sub\u2019? y
cp: overwrite \u2018./config.guess\u2019? y
# ./configure --prefix=/usr/local/jpeg9 --enable-shared --enable-static
# make && make install


5. 安裝libgd

[root@hming-server218-web ~ ]# tar -zxf libgd-2.2.3.tar.gz
[root@hming-server218-web ~ ]# cd libgd-2.2.3/
[root@hming-server218-web ~/libgd-2.2.3 ]# ./configure --prefix=/usr/local/libgd2 --with-zlib --with-jpeg=/usr/local/jpeg9 --with-png --with-freetype
[root@hming-server218-web ~/libgd-2.2.3 ]# make && make install


6. 編譯安裝PHP

[root@hming-server218-web ~ ]# groupadd php-fpm
[root@hming-server218-web ~ ]# useradd -g php-fpm -s /sbin/nologin php-fpm -M
[root@hming-server218-web ~ ]# tar -jxf php-5.6.30.tar.bz2
[root@hming-server218-web ~ ]# cd php-5.6.30/
[root@hming-server218-web ~ ]# ./configure \
--prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--with-fpm-user=php-fpm \
--with-fpm-group=php-fpm \
--with-mysql=/usr/local/mysql \
--with-mysqli=/usr/local/mysql/bin/mysql_config \
--with-jpeg-dir=/usr/local/jpeg9 \
--with-mcrypt=/usr/local/libmcrypt \
--with-gd=/usr/local/libgd2 \
--with-iconv-dir=/usr/local/libiconv \
--with-openssl-dir \
--with-freetype-dir \
--with-libxml-dir \
--with-png-dir \
--with-zlib \
--with-curl \
--with-mhash \
--with-pear \
--with-pcre-dir \
--with-gettext \
--enable-soap \
--enable-gd-native-ttf \
--enable-mbstring \
--enable-exif \
--enable-sockets \
--enable-ftp \
--disable-ipv6 \
--enable-bcmath \
--enable-shmop \
--with-snmp \
--enable-sysvsem

[root@hming-server218-web ~/php-5.6.30 ]# make
[root@hming-server218-web ~/php-5.6.30 ]# make test
[root@hming-server218-web ~/php-5.6.30 ]# make install


7. 拷貝php.ini配置文件

[root@hming-server218-web ~/php-5.6.30 ]# cp php.ini-production /usr/local/php/etc/php.ini


8. 修改php-fpm.conf配置文件

[root@hming-server218-web ~ ]# vim /usr/local/php/etc/php-fpm.conf
[global]
pid = /usr/local/php/var/run/php-fpm.pid
error_log = /usr/local/php/var/log/php-fpm.log
[www]
listen = /var/lib/php/php-fcgi.sock
user = php-fpm
group = php-fpm
listen.owner = nginx
listen.group = nginx
pm = dynamic
pm.max_children = 100
pm.start_servers = 20
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
rlimit_files = 1024

9. 檢查配置

[root@hming-server218-web ~ ]# /usr/local/php/sbin/php-fpm -t
[05-Jun-2017 20:23:27] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful


10. 拷貝啟動(dòng)腳本

[root@hming-server218-web ~ ]# cp php-5.6.30/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@hming-server218-web ~ ]# chmod 755 /etc/init.d/php-fpm
[root@hming-server218-web ~ ]# /etc/init.d/php-fpm start
Starting php-fpm  done


測試nginx是否正常解析PHP腳本

1. 修改nginx.conf配置文件

user  nginx nginx;
worker_processes  4;
error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx/nginx.pid;
worker_rlimit_nofile    65535;
 
events {
    use epoll;
    worker_connections  65535;
}
 
http {
    include       mime.types;
    default_type  application/octet-stream;
    server_names_hash_bucket_size 2048;
    server_names_hash_max_size 4096;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile       on;
    tcp_nopush     on;
    tcp_nodelay    on;
    keepalive_timeout  65;
 
    client_header_timeout 30;
    client_body_timeout 3m;
    client_max_body_size 10m;
    client_body_buffer_size 256k;
 
    send_timeout 3m;
    connection_pool_size 256;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 64k;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;
 
    client_body_temp_path /tmp/nginx/client_body;
    proxy_temp_path       /tmp/nginx/proxy;
fastcgi_temp_path     /tmp/nginx/fastcgi;
 
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_comp_level 3;
    gzip_http_version 1.1;
    gzip_types text/plain application/x-javascript text/css text/htm application/xml;
    gzip_vary on;
 
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;
    fastcgi_intercept_errors on;
 
    server {
        listen       80;
        server_name  10.0.6.218;
        charset UTF8;
        index index.html index.htm index.php index.jsp;
        root  /usr/local/nginx/html;
 
        #access_log  logs/host.access.log  main;
 
        location / {
            root   html;
            index  index.html index.htm;
        }
 
        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
 
        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            root           html;
            fastcgi_pass   unix:/var/lib/php/php-fcgi.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
 
    }
 
}


2. 測試nginx配置文件語法是否存在錯(cuò)誤

[root@hming-server218-web ~ ]# nginx -t
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


3. 創(chuàng)建測試解析php腳本

[root@hming-server218-web ~ ]# vim /usr/local/nginx/html/info.php
<?php
phpinfo();
?>
 
[root@hming-server218-web ~ ]# nginx -s reload

4. 訪問http://10.0.6.218/info.php

CentOS 7上源碼編譯安裝和配置LNMP Web+phpMyAdmin服務(wù)器環(huán)境

注意:該測試腳本僅用于我們查看當(dāng)前安裝的php支持的模塊信息,在測試之后應(yīng)當(dāng)立即刪除


安裝phpMyAdmin

phpMyAdmin是一個(gè)使用PHP語言編寫的免費(fèi)軟件,旨在通過Web界面管理MySQL數(shù)據(jù)庫。phpMyAdmin支持MySQLMariaDB上的各種操作。用戶可以通過Web界面執(zhí)行數(shù)據(jù)庫的常用操作(數(shù)據(jù)庫管理,,字段,索引,用戶,權(quán)限,視圖,函數(shù)等),以及直接執(zhí)行任何SQL語句。


1. 創(chuàng)建phpMyAdmin Web目錄

[root@hming-server218-web ~ ]# mkdir /app/data/phpMyAdmin -p

2. 將phpMyAdmin軟件包解壓到/app/data/phpMyAdmin 目錄下

# tar -zxf phpMyAdmin-4.7.1-all-languages.tar.gz
# cp -ar phpMyAdmin-4.7.1-all-languages/* /app/data/phpMyAdmin/
# chown -R nginx:nginx /app/data/phpMyAdmin
# chown -R php-fpm:php-fpm /app/data/phpMyAdmin


3. 修改nginx配置,創(chuàng)建一個(gè)server虛擬機(jī)配置文件

http {
    ......
    # phpMyAdmin
    server {
        listen       8000;
        server_name  10.0.6.218;
        charset UTF8;
        index index.html index.htm index.php index.jsp;
        root  /app/data;
        access_log  /var/log/nginx/phpMyAdmin.log  main;
 
        location /phpMyAdmin/ {
            index index.html index.htm index.php;
        }
 
        location ~ \.php$ {
            fastcgi_pass   unix:/var/lib/php/php-fcgi.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /app/data$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}


4. 創(chuàng)建數(shù)據(jù)庫root用戶權(quán)限,允許php服務(wù)器使用root用戶管理數(shù)據(jù)服務(wù)器

[root@hming-server217-mdb ~ ]# mysql -uroot -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 13
Server version: 10.1.20-MariaDB Source distribution
 
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
 
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 
MariaDB [(none)]> select user,host,password from mysql.user;
+------+-----------+-------------------------------------------+
| user | host      | password                                  |
+------+-----------+-------------------------------------------+
| root | localhost | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | 127.0.0.1 | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
| root | ::1       | *84BB5DF4823DA319BBF86C99624479A198E6EEE9 |
+------+-----------+-------------------------------------------+
3 rows in set (0.00 sec)
 
MariaDB [(none)]> grant all privileges on *.* to 'root'@'10.0.6.218' identified by 'phpMyAdmin_123';
Query OK, 0 rows affected (0.00 sec)
 
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)


5. 配置防火墻規(guī)則

# iptables -I INPUT -p tcp --dport 3306 -s 10.0.6.0/24 -j ACCEPT

或者

# firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=10.0.6.0/24 port port=3306 protocol=tcp accept'


6. 修改phpMyAdmin配置文件

#cp/app/data/phpMyAdmin/libraries/config.default.php /app/data/phpMyAdmin/libraries/config.default.php.save
# vim /app/data/phpMyAdmin/libraries/config.default.php

/**
 * The 'cookie' auth_type uses AES algorithm to encrypt the password. If
 * at least one server configuration uses 'cookie' auth_type, enter here a
 * pass phrase that will be used by AES. The maximum length seems to be 46
 * characters.
 *
 * @global string $cfg['blowfish_secret']
 */
$cfg['blowfish_secret'] = '123456';
 
 
/**
 * MySQL hostname or IP address
 *
 * @global string $cfg['Servers'][$i]['host']
 */
$cfg['Servers'][$i]['host'] = '10.0.6.217';
 
/**
 * MySQL port - leave blank for default port
 *
 * @global string $cfg['Servers'][$i]['port']
 */
$cfg['Servers'][$i]['port'] = '3306';
 
/**
 * Path to the socket - leave blank for default socket
 *
 * @global string $cfg['Servers'][$i]['socket']
 */
$cfg['Servers'][$i]['socket'] = '';
 
 
/**
 * Authentication method (valid choices: config, http, signon or cookie)
 *
 * @global string $cfg['Servers'][$i]['auth_type']
 */
$cfg['Servers'][$i]['auth_type'] = 'cookie';
 
/**
 * HTTP Basic Auth Realm name to display (only used with 'HTTP' auth_type)
 *
 * @global string $cfg['Servers'][$i]['auth_http_realm']
 */
$cfg['Servers'][$i]['auth_http_realm'] = '';
 
/**
 * MySQL user
 *
 * @global string $cfg['Servers'][$i]['user']
 */
$cfg['Servers'][$i]['user'] = 'root';
 
/**
 * MySQL password (only needed with 'config' auth_type)
 *
 * @global string $cfg['Servers'][$i]['password']
 */
$cfg['Servers'][$i]['password'] = 'phpMyAdmin_123';


7. 登錄phpMyAdmin Web管理界面,打開http://10.0.6.218:8000/phpMyAdmin/

CentOS 7上源碼編譯安裝和配置LNMP Web+phpMyAdmin服務(wù)器環(huán)境


輸入數(shù)據(jù)庫用戶和密碼

CentOS 7上源碼編譯安裝和配置LNMP Web+phpMyAdmin服務(wù)器環(huán)境


測試創(chuàng)建數(shù)據(jù)庫

CentOS 7上源碼編譯安裝和配置LNMP Web+phpMyAdmin服務(wù)器環(huán)境CentOS 7上源碼編譯安裝和配置LNMP Web+phpMyAdmin服務(wù)器環(huán)境


配置phpMyAdmin增加Nginx登錄身份驗(yàn)證功能


1. 修改Nginx配置文件,/phpMyAdmin/處增加允許訪問規(guī)則和auth_basic身份驗(yàn)證配置

    # phpMyAdmin
    server {
        listen       8000;
        server_name  10.0.6.218;
        charset UTF8;
        index index.html index.htm index.php index.jsp;
        root  /app/data;
        access_log  /var/log/nginx/phpMyAdmin.log  main;
 
        location /phpMyAdmin/ {
            allow 10.0.6.0/24;
            deny all;
            auth_basic            "Auth";
            auth_basic_user_file  /usr/local/nginx/.htpassword;
            index index.html index.htm index.php;
        }
 
        location ~ \.php$ {
            fastcgi_pass   unix:/var/lib/php/php-fcgi.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /app/data$fastcgi_script_name;
            include        fastcgi_params;
        }
    }


2. 使用htpasswd創(chuàng)建用戶(HM)和密碼

[root@hming-server218-web ~ ]# yum install httpd
[root@hming-server218-web ~ ]# htpasswd -c /usr/local/nginx/.htpassword HM
New password: 
Re-type new password: 
Adding password for user HM
[root@hming-server218-web ~ ]# ls -l /usr/local/nginx/.htpassword 
-rw-r--r-- 1 root root 41 Jun  6 18:04 /usr/local/nginx/.htpassword


3. 重新訪問phpMyAdmin,再次登錄需要進(jìn)行身份驗(yàn)證,以后的每一次登錄都將需要進(jìn)行此驗(yàn)證

CentOS 7上源碼編譯安裝和配置LNMP Web+phpMyAdmin服務(wù)器環(huán)境

輸入用戶和密碼

CentOS 7上源碼編譯安裝和配置LNMP Web+phpMyAdmin服務(wù)器環(huán)境



向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI