Debian9 (Stretch) 下編譯安裝 LNMP 環境

未進化的類人猿發表於2019-08-14

一、前言

之前,我的開發環境是Windows-10+PHP-7.1+Nginx-1.10+MariaDB-10.1。

後面開發需要使用到memcached,redis等nosql比較多,而在Windows下定製難度,很多PHP擴充並沒有.dll檔案,且PHP擴充在Windows下compile難度還是比較大的。

所以促使我轉向Linux下開發。

首先,我search了一下,主要是Red Hat 與Debian。

基於Red Hat:商業版,Centos,Fedora
基於Debian: Debian,Ubuntu

我選擇了Debian 9,PHP-7.2,MariaDB-10.2,Nginx-1.13

二、Requirements

一般安裝順序,mariadb > nginx > php,以下的涉及的軟體,library均是基於Debian(Ubuntu)。

2.1 PHP的需要的額外庫:

## 原始碼需要的詞法分析器
apt install bison
## 原始碼都是c程式,需要c編譯器,注意編譯器版本
apt install gcc-6
## C++編譯器
apt install g++
## xml解析庫
apt install libxml2 libxml2-dev
## make cmake m4 autoconf
apt install make cmake m4 autoconf
## webp 格式,能夠帶來更小體積的圖片
apt install libwebp6 libwebp-dev
## jpeg格式支援
apt install libjpeg-dev
## png格式支援
apt install libpng-dev
## 免費開源字型引擎
apt install libfreetype6 libfreetype6-dev
## ssl加密庫支援(原始碼安裝openssl,可以選擇使用Debian 包來安裝openssl)
apt install openssl
## ssh2 庫(原始碼安裝)
apt install libssh2-1-dev
## mhash 庫
apt install libmhash2
## zlib 壓縮庫(原始碼安裝)
apt install zlib1g zlib1g-dev
## pcre 正規表示式庫(原始碼安裝)
apt install libpcre3-dev libpcre3
## gzip
apt install gzip
## bz2
apt install libbz2-1.0 libbz2-dev
## soduim php7.2新特性 現代加密標準
apt install libsodium-dev
## argon2 php7.2新特性 新的加密函式,由PHC(Password Hashing Competition)釋出
apt install argon2 libargon2-0 libargon2-0-dev

2.2 Nginx 需要的額外庫

主要是三個,openssl,zlib,pcre,可以透過Debian本身的庫安裝,也可以選擇原始碼安裝。我選擇後者,所以,

並不會與上面的衝突,後面會涉及到原因。

2.3 MariaDB 需要的額外庫

## bison詞法分析器
apt install bison
## libncurses 一個可用於編寫獨立終端基於文字的的程式庫
apt install libncurses5 libncurses5-dev
## libevent-dev 一個事件庫
apt install libevent-dev
## openssl 一個加密庫
apt install openssl

三、 安裝過程

按照MariaDB > Nginx > PHP的順序安裝,安裝前請再次檢查上述所需的額外庫都已安裝。

3.1 對應的系統使用者建立

為什麼要建立使用者?
答:因為安裝完成後,我們只需要這些程式只用於系統服務就好(daemon或者其他自己執行的程式),並不需要使用一個具體使用者身份去操作他。即建立系統賬戶,以及系統使用者組。

groupadd -r mysql
useradd -r -g mysql -s /bin/false   -M mysql
mkdir /usr/local/data/mysql
chown -R mysql:mysql /usr/local/data/mysql 

note 引數含義

透過man groupadd 或者man useradd 可以調出具體的手冊

-r  建立系統使用者或者使用者組
-g  指定使用者所屬使用者組
-s  指定使用者登入shell名字,sh,bash,因為是系統使用者,並不需要,設定 /bin/false或者/usr/sbin/nologin
-M  不建立使用者主目錄

同樣,分別建立nginx,php-fpm

groupadd -r php-fpm
useradd -r -g php-fpm -s /bin/false -M php-fpm

groupadd -r nginx
useradd -r -g nginx -s /bin/false -M nginx 

3.2 MariaDB

MariaDB 安裝可能略顯麻煩,並不是常見的make方式,而是cmake方式。

獲取mariadb-10.2原始碼

wget http://mirror.jaleco.com/mariadb//mariadb-10.2.12/source/mariadb-10.2.12.tar.gz 
tar -zxvf mariadb-10.2.12.tar.gz
mkdir build-mariadb
cd build-mariadb
cmake ../ -DCMAKE_INSTALL_PREFIX=/opt/soft/mariadb-10.3.4 \
-DMYSQL_DATADIR=/var/data/mysql \
-DSYSCONFDIR=/etc \
-DWITHOUT_TOKUDB=1 \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STPRAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWIYH_READLINE=1 \
-DWIYH_SSL=system \
-DVITH_ZLIB=system \
-DWITH_LOBWRAP=0 \
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DBUILD_LIBPROTOBUF_FROM_SOURCES=ON
make && make install 

如果失敗 使用 rm -rf CMakeCache.txt

3.2.1 配置MariaDB

vim /etc/profile.d/mariadb.sh

add
export PATH=$PATH:/opt/soft/mariadb-10.2/bin

source /etc/profile.d/mariadb.sh

cd /opt/soft/mariadb-10.2
scripts/mysql_install_db --user=mysql --datadir=/usr/local/data/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 localhost.localdomain 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/maria'

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/

複製
cd /opt/soft/mariadb-10.2
cp support-files/my-large.cnf /etc/my.cnf
或者
cp support-files/my-large.cnf /etc/mysql/my.cnf

建立系統啟動指令碼(使用systemd)
cd /etc/systemd/system
vim mysqld.service

[Unit]
Description=MariaDB Server
After=network.target

[Service]
ExecStart=/opt/soft/mariadb-10.2/bin/mysqld --defaults-file=/etc/mysql/my.cnf  --datadir=/usr/local/data/mysql --socket=/tmp/mysql.sock
User=mysql
Group=mysql
WorkingDirectory=/opt/soft/mariadb-10.2

[Install]
WantedBy=multi-user.target

systemctl daemon-reload
systemctl restart mysqld.service
systemctl status mysqld.servie 

如果沒有啟動,請使用journalctl -xn 或者 journalctl -xl來檢視錯誤資訊

如果想開機啟動,請使用systemctl enable mysqld.service

安全設定

$:mysql_secure_installation 

Enter current password for root (enter for none):     輸入當前root密碼(沒有輸入)

Set root password? [Y/n]     設定root密碼?(是/否)

New password:    輸入新root密碼

Re-enter new password:        確認輸入root密碼

Password updated successfully!         密碼更新成功

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.

預設情況下,MariaDB安裝有一個匿名使用者,
允許任何人登入MariaDB而他們無需建立使用者帳戶。
這個目的是隻用於測試,安裝去更平緩一些。
你應該進入前刪除它們生產環境。

Remove anonymous users? [Y/n]         刪除匿名使用者?(是/否)

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

通常情況下,root只應允許從localhost連線。
這確保其他使用者無法從網路猜測root密碼。

Disallow root login remotely? [Y/n]     不允許root登入遠端?(是/否)

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.

預設情況下,MariaDB提供了一個名為“測試”的資料庫,任何人都可以訪問。
這也只用於測試,在進入生產環境之前應該被刪除。

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

重新載入許可權表將確保所有到目前為止所做的更改將立即生效。

Reload privilege tables now? [Y/n]      現在重新載入許可權表(是/否)

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

全部完成!如果你已經完成了以上步驟,MariaDB安裝現在應該安全。

Thanks for using MariaDB!

至此,mariaddb已經安裝完成,可以使用 ps -aux | grep mysql 檢視服務

現在測試一下,mysql -u root -p 或者 mysql -h localhost -P 5001 -u shanechiu -p

3.3 PHP 安裝

PHP 安裝比較簡單,主要是選擇你要安裝的擴充或者需要開啟的功能

可以使用./configure --help 來瀏覽原始碼安裝提供的安裝選項

有些屬於PHP內建的功能,你只需要 enable或者disable,比如php-fpm,是需要啟用的。

有些擴充是可以動態載入的,稱之為shared extension,但是官方也說了,並不是所有的擴充都是能夠shared.

獲取原始碼:
wget http://am1.php.net/distributions/php-7.2.1.tar.bz2
解壓:

tar -xvf php-7.2.1.tar.bz2
cd php-7.2.1
./configure --prefix=/opt/soft/php7.2 \
--with-config-file-path=/opt/soft/php7.2/etc \
--with-mysql-sock=/tmp/mysql.sock \
--with-openssl \
--with-mhash \
--with-mysqli=shared,mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-pdo-pgsql=/opt/soft/pgsql \
--with-gd \
--with-iconv \
--with-zlib \
--enable-exif \
--enable-intl \
--enable-calendar \
--enable-zip \
--enable-inline-optimization \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-xml \
--enable-bcmath \
--enable-shmop \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-pcntl \
--enable-sockets \
--enable-ipv6 \
--with-bz2 \
--with-xmlrpc \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl=/opt/soft/curl7.57--enable-debug \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--enable-opcache \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--with-sodium \
--with-libxml-dir \
--with-password-argon2 \
--without-gdbm \
--with-pcre-regex \
--with-pcre-jit \
--enable-fast-install \
--enable-fileinfo

配置
進入原始碼檔案,cp php.ini.development /opt/soft/php-7.2/php.ini
修改以下部分

extension_dir=/opt/soft/php-7.2/lib/php/extensions/no-debug-non-zts-20170718/
extension=mysqli
time_zone=PRC

同時要新增php-fpm配置檔案,安裝目錄下 etc/cp php-fpm.conf.default php-fpm.confcp php.conf.d/www.conf.default php.conf.d/www.conf

PHP-FPM啟動指令碼(systemd)
PHP 非常人性化,在原始碼目錄下/sapi/fpm下可以找到php-fpm.service檔案,複製到/etc/systemd/system/php-fpm.service
systemdctl start php-fpm.service
systemdctl status php-fpm.service
如果發生錯誤,使用journalctl -xn檢視具體錯誤資訊
開機啟動,sytemctl enable php-fpm.service

3.4 Nginx 原始碼安裝

Nginx的編譯安裝難易程度應該是LNMP環境中最簡單的

首先需要三個原始碼包,一個zlib(壓縮庫),一個pcre(正規表示式庫),一個openssl(加密庫,如果要使用HTTPS,這個庫是必須的),

當然你如果是透過debian本身的包管理器安裝的,這個可以省略,但是一定要安裝兩個,一個是軟體本身,同時還要安裝開發庫,像這種,apt -y install openssl opensll-dev

命令:
--configure --prefix=/opt/soft/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \ # 這個預設是不開啟的,如需使用TLS,請帶上這一項編譯。
--with-pcre=../pcre-8.41 \
--with-zlib=../zlib-1.2.11 \
--with-openssl=../openssl-1.1.0g
然後,makemake install

注意,如果是使用二進位制包安裝了zlib,pcre,openssl,及相應的開發庫,不需要指定路徑。

配置:
編寫nginx守護程式檔案,還是利用systemd工具
vim /etc/sytemd/system/nginx.service

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/soft/nginx/logs/nginx.pid
ExecStartPre=/opt/soft/nginx/sbin/nginx -t
ExecStart=/opt/soft/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target

這個可以在nginx 官網找到,可以按照自己需求修改。注意路徑修改成自己的安裝路徑。

systemctl start nginx.service 啟動Nginx
systemctl enable nginx.service 開機啟動

記得,如果中途修改了service檔案,一定要先執行systemctl daemon-reload重新載入守護程式檔案。然後執行 systemctl start nginx.service重啟服務。

四、APPEND

後續會新增一鍵安裝指令碼。

五、參考資料

  1. systemd 入門教程
  2. CentOS7.3編譯安裝MariaDB10.2.6
  3. CentOS7.3編譯安裝php7.1
  4. GNU bison
  5. GD-support configure PHP
  6. Argon2
  7. The Sodium crypto library (libsodium)
  8. get the mariadb code,buildit,test it
  9. Generic Build Instructions
  10. Installing System Tables (mysql_install_db)
  11. "Compiling MariaDB From Source"
  12. ncurses
  13. CMake
  14. php-manul
  15. PHP7.2 NEW FEATURE
  16. Building nginx from Sources
本作品採用《CC 協議》,轉載必須註明作者和本文連結
禁止轉載

相關文章