title: 編譯LNMP部署動態網站環境
date: 2018-11-08 13:59:59
tags:php
LNMP動態網站部署架構是由一套 Linux+Nginx+MySQL+PHP 組成的動態網站系統解決方案.
如下配置環境爲:Linux=RHEL7 --> Nginx=1.13 --> MySQL=5.6 --> PHP=7.0 無錯誤版.html
在使用源碼包安裝服務程序以前,首先要讓安裝主機具有編譯程序源碼的環境,他須要具有C語言,C++語言的編譯器,以及常見的編譯支持函數庫程序,下面咱們將經過Yum倉庫來快速部署這些包.mysql
[root@localhost ~]# yum install -y gcc gcc-c++ apr apr-util autoconf automake bison bzip2 Loaded plugins: product-id, search-disabled-repos, subscription-manager This system is not registered with an entitlement server. You can use subscription-manager. Package gcc-4.8.5-28.el7.x86_64 already installed and latest version Package gcc-c++-4.8.5-28.el7.x86_64 already installed and latest version Package apr-1.4.8-3.el7_4.1.x86_64 already installed and latest version Package apr-util-1.5.2-6.el7.x86_64 already installed and latest version Package autoconf-2.69-11.el7.noarch already installed and latest version Package automake-1.13.4-3.el7.noarch already installed and latest version Package bison-3.0.4-1.el7.x86_64 already installed and latest version Package bzip2-1.0.6-13.el7.x86_64 already installed and latest version Nothing to do
CMake是Linux系統中一款經常使用的編譯工具,在這裏MySQL的編譯會用到CMake命令,接下來咱們將解壓,並編譯安裝這個包.nginx
[root@localhost ~]# tar -xzvf cmake-2.8.11.2.tar.gz [root@localhost ~]# cd cmake-2.8.11.2 [root@localhost ~]# ./configure [root@localhost ~]# make [root@localhost ~]# make install
Nginx是一款至關優秀的用於部署動態網站的輕量級服務程序,他最初是爲俄羅斯門戶站點開發的,因其性能穩定,功能豐富,佔用內存小且併發能力高而備受用戶的信賴,目前諸如,新浪搜狐,網易,騰訊等門戶站點均已經使用了此服務.c++
坦白來講,雖然Nginx服務程序代碼質量很是高,代碼很規範,技術成熟,模塊擴展也很容易,但任然存在很多的問題,好比官方資料對中文的支持較少,可是Nginx服務程序,在近幾年來的發展勢頭迅猛,相信會在輕量級Web服務器市場可以有不錯的前景.正則表達式
1.在編譯配置Nginx以前咱們還須要解決相關依賴問題,例如用於提供Perl語言兼容的正則表達式庫的軟件包pcre,就是Nginx程序用於實現僞靜態功能必不可少的依賴包,下面咱們先來解壓,並編譯,安裝這個包.算法
[root@localhost ~]# tar -xzvf pcre-8.35.tar.gz [root@localhost ~]# cd pcre-8.35 [root@localhost ~]# ./configure --prefix=/usr/local/pcre [root@localhost ~]# make [root@localhost ~]# make install
2.緊接着繼續編譯安裝openssl,這個工具是用與提供網站加密證書服務的程序文件,安裝時須要自定義服務程序的安裝目錄,以便於後期調用命令是更方便.sql
[root@localhost ~]# tar -xzvf openssl-1.0.1h.tar.gz [root@localhost ~]# cd openssl-1.0.1h [root@localhost ~]# ./config --prefix=/usr/local/openssl [root@localhost ~]# make [root@localhost ~]# make install
3.在openssl安裝後,默認會在/usr/local/openssl/bin目錄中提供不少可用命令,咱們將這個目錄添加到PATH環境變量裏,方便後期的調用.數據庫
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/openssl/bin" >> /etc/profile [root@localhost ~]# source /etc/profile [root@localhost ~]# openssl OpenSSL>
4.zlib軟件包是用戶提供壓縮功能的函數文件,下面咱們開始編譯安裝.apache
[root@localhost ~]# tar -xzvf zlib-1.2.8.tar.gz [root@localhost ~]# cd zlib-1.2.8 [root@localhost ~]# ./configure --prefix=/usr/local/zlib [root@localhost ~]# make [root@localhost ~]# make install
5.在安裝好zlib後,下面咱們進入編譯Nginx的前戲階段,建立一個Nginx用戶
[root@localhost ~]# useradd -s /sbin/nologin -M nginx
6.下面開始編譯安裝Nginx,這裏咱們須要指定--user與--group參數,也就是用戶與組,在使用參數openssl和zlib庫時應該指定具體位置.
[root@localhost ~]# tar -xzvf nginx-1.13.12.tar.gz [root@localhost ~]# cd nginx-1.13.12 [root@localhost ~]# ./configure --prefix=/usr/local/nginx \ > --user=nginx --group=nginx \ > --with-http_stub_status_module --with-http_ssl_module \ > --with-http_gzip_static_module --with-openssl=/root/openssl-1.0.1h \ > --with-zlib=/root/zlib-1.2.8 --with-pcre=/root/pcre-8.35 [root@localhost ~]# make [root@localhost ~]# make install
7.啓動Nginx,並設置開機自啓動
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t #檢測配置文件正確性 [root@localhost ~]# /usr/local/nginx/sbin/nginx #啓動Nginx [root@localhost ~]# kill -QUIT $(cat /usr/local/nginx/logs/nginx.pid) #關閉Nginx [root@localhost ~]# kill -HUP $(cat /usr/local/nginx/logs/nginx.pid) #重啓Nginx [root@localhost ~]# echo "/usr/local/nginx/sbin/nginx 2> /dev/null" >> /etc/profile
MySQL是一個關係型數據庫管理系統,由瑞典MySQL AB公司開發,目前屬於Oracle旗下產品.MySQL是最流行的關係型數據庫管理系統之一,在WEB應用方面,MySQL是最好的RDBMS應用軟件,MySQL是一種關係數據庫管理系統,關係數據庫將數據保存在不一樣的表中,而不是將全部數據放在一個大倉庫內,這樣就增長了速度並提升了靈活性.這裏須要注意的是因爲MySQL5.7之後產品閉源了,後期的話可使用MariaDB替代.
1.在編譯MySQL過程當中,咱們須要建立一個mysql的系統用戶,專門負責運行MySQL數據庫,記得把這個帳戶變成一個不可登陸的用戶,且也無需建立家目錄,提升系統安全性.
[root@localhost ~]# useradd -s /sbin/nologin -M mysql
2.建立一個用於保存MySQL程序,和數據庫文件的目錄,並把該目錄全部者和所屬組改爲mysql,其中/usr/local/mysql用於保存MySQL數據程序,而/usr/local/mysql/data則用來保存數據庫文件.
[root@localhost ~]# mkdir -p /usr/local/mysql [root@localhost ~]# mkdir -p /usr/local/mysql/data [root@localhost ~]# chown -Rf mysql:mysql /usr/local/mysql/
3.安裝Ncurses,若不安裝ncurses編譯MySQL時會報錯
[root@localhost ~]# yum install -y ncurses-devel [root@localhost ~]# tar -xzvf ncurses-6.1.tar.gz [root@localhost ~]# cd ncurses-6.1 [root@localhost ~]# ./configure --with-shared \ --without-debug \ --without-ada \ --enable-overwrite [root@localhost ~]# make [root@localhost ~]# make install # --without-ada參數爲設定不編譯爲ada綁定,因進入chroot環境不能使用ada ; # --enable-overwrite參數爲定義把頭文件安裝到/tools/include下而不是/tools/include/ncurses目錄 # --with-shared 生成共享庫
4.接下來,開始解壓編譯MySQL,因爲MySQL體積過大,編譯時間很長,耐心等待吧
[root@localhost ~]# tar -xzvf mysql-5.6.19.tar.gz [root@localhost ~]# cd mysql-5.6.19 [root@localhost ~]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DSYSCONFDIR=/etc \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DEXTRA_CHARSETS=all \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_MYISAM_STORAGE_ENGINE=1 \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_MEMORY_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DENABLED_LOCAL_INFILE=1 \ -DMYSQL_USER=mysql \ -DMYSQL_TCP_PORT=3306 [root@localhost ~]# make [root@localhost ~]# make install
5.下面進入配置環節,首先刪除/etc/my.cnf默認配置文件,而後在MySQL程序目錄內,找到一個名爲mysql_install_db的腳本文件,執行這個腳本,並使用--user參數指定MySQL服務的對應帳號名稱,使用--basedir指定MySQL程序保存目錄,使用--datadir指定數據庫目錄.
[root@localhost ~]# rm -fr /etc/my.cnf [root@localhost ~]# cd /usr/local/mysql/ [root@localhost mysql]# ./scripts/mysql_install_db --user=mysql \ --basedir=/usr/local/mysql/ \ --datadir=/usr/local/mysql/data/ .... New default config file was created as /usr/local/mysql//my.cnf and will be used by default by the server when you start it. You may edit this file to change server settings
6.接着把,MySQL數據庫配置文件複製到/etc/目錄下,而後把開機程序複製到/etc/rc.d/init.d目錄下,給腳本755的權限.
[root@localhost mysql]# cp -a my.cnf /etc/my.cnf [root@localhost mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld [root@localhost mysql]# chmod 755 -R /etc/rc.d/init.d/mysqld
7.修改剛剛的配置文件,改basedir=/usr/local/mysql,datadir=/usr/local/mysql/data這一行.
[root@localhost mysql]# vim /etc/rc.d/init.d/mysqld 43 # If you change base dir, you must also change datadir. These may get 44 # overwritten by settings in the MySQL configuration files. 45 46 basedir= /usr/local/mysql 47 datadir= /usr/local/mysql/data 48 49 # Default value, in seconds, afterwhich the script should timeout waiting 50 # for server start.
8.啓動MySQL,與設置chkconfig開機自啓動的配置.
[root@localhost ~]# /etc/rc.d/init.d/mysqld start [root@localhost ~]# /usr/local/mysql/bin/mysqld_safe --user=mysql & [root@localhost ~]# chkconfig mysqld on [root@localhost ~]# 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@localhost ~]# echo "/usr/local/mysql/bin/mysqld_safe --user=mysql &" >> /etc/rc.local
9.添加環境變量,使用相對路徑命令,這樣就能直接訪問了.
[root@localhost ~]# echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile [root@localhost ~]# source /etc/profile [root@localhost ~]# mysql -uroot Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.6.19 Source distribution Copyright (c) 2000, 2014, 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>
10.MySQL數據庫還會調用一些函數庫,在這裏咱們把它們複製到指定的位置吧.
[root@localhost ~]# mkdir /var/lib/mysql [root@localhost ~]# ln -s /usr/local/mysql/lib/* /var/lib/mysql [root@localhost ~]# ln /usr/local/mysql/include/mysql /usr/include/ [root@localhost ~]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql
11.接着咱們執行 mysql_secure_installation 命令,給MySQL設置初始密碼,到此配置完畢
[root@localhost ~]# mysql_secure_installation [root@localhost ~]# mysql -uroot -p1233 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 18 Server version: 5.6.19 Source distribution Copyright (c) 2000, 2014, 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>
PHP是一種通用開源腳本語言,語法吸取了C語言、Java和Perl的特色,利於學習使用普遍,主要適用於Web開發領域.PHP獨特的語法混合了C、Java、Perl以及PHP自創的語法.它能夠比CGI或者Perl更快速地執行動態網頁.用PHP作出的動態頁面與其餘的編程語言相比,PHP是將程序嵌入到HTML文檔中去執行,執行效率比徹底生成HTML標記的CGI要高許多,PHP還能夠執行編譯後代碼,編譯能夠達到加密和優化代碼運行,使代碼運行更快.
1.在編譯安裝PHP以前首先要解決依賴的問題,例如yasm是一款常見的開源彙編器.
[root@localhost ~]# tar -xzvf yasm-1.2.0.tar.gz [root@localhost ~]# cd yasm-1.2.0 [root@localhost ~]# ./configure [root@localhost ~]# make [root@localhost ~]# make install
2.libmcrypt是用於加密算法的擴展庫程序.
[root@localhost ~]# tar -xzvf libmcrypt-2.5.8.tar.gz [root@localhost ~]# cd libmcrypt-2.5.8 [root@localhost ~]# ./configure [root@localhost ~]# make [root@localhost ~]# make install
3.libvpx是用於提供視頻編碼器服務的程序.
[root@localhost ~]# tar -xjvf libvpx-v1.3.0.tar.bz2 [root@localhost ~]# cd libvpx-v1.3.0 [root@localhost ~]# ./configure --prefix=/usr/local/libvpx --enable-shared --enable-vp9 [root@localhost ~]# make [root@localhost ~]# make install
4.tiff是用於提供標籤圖像文件格式的服務程序.
[root@localhost ~]# tar -xzvf tiff-4.0.3.tar.gz [root@localhost ~]# cd tiff-4.0.3 [root@localhost ~]# ./configure --prefix=/usr/local/tiff --enable-shared [root@localhost ~]# make [root@localhost ~]# make install
5.libpng是用於提供png圖片格式支持函數庫的服務程序.
[root@localhost ~]# tar -xzvf libpng-1.6.12.tar.gz [root@localhost ~]# yum install -y zlib zlib-devel [root@localhost ~]# ./configure --prefix=/usr/local/libpng --enable-shared [root@localhost ~]# make [root@localhost ~]# make install
6.freetype是用於提供字體支持引擎的服務.
[root@localhost ~]# tar -xzvf freetype-2.5.3.tar.gz [root@localhost ~]# cd freetype-2.5.3 [root@localhost ~]# ./configure --prefix=/usr/local/freetype -enable-shared [root@localhost ~]# make [root@localhost ~]# make install
7.jpeg是用於提供jpeg圖片格式支持的函數庫.
[root@localhost ~]# tar -xzvf jpegsrc.v9a.tar.gz [root@localhost ~]# cd jpeg-9a [root@localhost ~]# ./configure --prefix=/usr/local/jpeg --enable-shared [root@localhost ~]# make [root@localhost ~]# make install
8.libgd是用於提供圖形處理的服務.
[root@localhost ~]# tar -xzvf libgd-2.1.0.tar.gz [root@localhost ~]# cd libgd-2.1.0 [root@localhost ~]# ./configure --prefix=/usr/local/libgd --with-jpeg=/usr/local/jpeg \ --with-freetype=/usr/local/freetype --with-tiff=/usr/local/tiff --with-vpx=/usr/local/libvpx [root@localhost ~]# make [root@localhost ~]# make install
9.tlib是用於提供圖片生成函數庫的服務程序.
[root@localhost ~]# tar -xzvf t1lib-5.1.2.tar.gz [root@localhost ~]# cd t1lib-5.1.2 [root@localhost ~]# ./configure --prefix=/usr/local/t1lib --enable-shared [root@localhost ~]# make [root@localhost ~]# make install [root@localhost ~]# ln -s /usr/lib64/libltdl.so.7 /usr/lib/libltdl.so [root@localhost ~]# cp -a frp /usr/lib64/libXpm.so* /usr/lib/
10.編譯安裝PHP程序.(這裏既可使用5.x也可使用7.x參數通用)
[root@localhost ~]# tar -xzvf php-7.0.0.tar.gz [root@localhost ~]# yum install -y libxml2 libxml2-devel libcurl libcurl-devel libXpm-devel [root@localhost ~]# cd php-7.0.0 [root@localhost ~]# export LD_LIBRARY_PATH=/usr/local/libgd/lib [root@localhost ~]# ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-mysql-sock=/tmp/mysql.sock --with-pdo-mysql=/usr/local/mysql --with-gd --with-png-dir=/usr/local/libpng --with-jpeg-dir=/usr/local/jpeg --with-freetype-dir=/usr/local/freetype --with-xpm-dir=/usr/ --with-vpx-dir=/usr/local/libvpx/ --with-zlib-dir=/usr/local/zlib --with-t1lib=/usr/local/t1lib --with-iconv --enable-libxml --enable-xml --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --enable-opcache --enable-mbregex --enable-fpm --enable-mbstring --enable-ftp --enable-gd-native-ttf --with-openssl --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --without-pear --with-gettext --enable-session --with-mcrypt --with-curl --enable-ctype [root@localhost ~]# make [root@localhost ~]# make install
1.拷貝相應配置文件
cp -a php.ini-development /usr/local/php7/lib/php.ini cp -a /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf cp -a sapi/fpm/php-fpm /usr/local/bin
2.編輯配置文件,在PHP文件末尾追加寫入如下標★語句
vim /usr/local/php7/lib/php.ini cgi.fix_pathinfo=1 #將註釋去掉,開啓PHP的pathinfo僞靜態功能 max_execution_time = 0 #腳本運行的最長時間,默認30秒 max_input_time = 300 #腳本能夠消耗的時間,默認60秒 memory_limit = 256M #腳本運行最大消耗的內存,根據你的需求更改數值,默認128M post_max_size = 100M #單提交的最大數據,默認100M upload_max_filesize = 10M #上載文件的最大許可大小,默認2M
3.修改php-fpm的配置,在PHP-fpm文件中,修改如下標★語句
cd /usr/local/php7/etc/php-fpm.d/ cp -a www.conf.default www.conf vim /usr/local/php7/etc/php-fpm.d/www.conf ★listen.owner = nobody #解除註釋 ★listen.group = nobody #解除註釋 ★user = nginx #將apache修改成nginx ★group = nginx #將apache修改成nginx
4.修改nginx的主配置,在server語句內,寫入如下標★語句
vim /usr/local/nginx/conf/nginx.conf 38 server { 39 listen 80 default_server; 40 listen [::]:80 default_server; 41 server_name _; 42 root /usr/share/nginx/html; 43 44 # Load configuration files for the default server block. 45 include /etc/nginx/default.d/*.conf; 46 ★ location / { ★ ★ root html; ★ index index.php index.html index.htm; ★ 52 } 53 ★ location ~ \.php$ { ★ root /usr/local/nginx/html; ★ try_files $uri =404; ★ fastcgi_pass 127.0.0.1:9000; ★ fastcgi_index index.php; ★ fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ★ include fastcgi_params; ★ } 62 63 error_page 404 /404.html; 64 location = /40x.html { 65 }
5.設置網頁目錄權限
chown -R nginx:nginx /usr/local/nginx/html/
6.新建index.php測試頁
vim /usr/local/nginx/html/index.php <?php phpinfo(); ?>
7.重啓服務,並查看9000端口是否啓動成功
/usr/local/nginx/sbin/nginx /usr/local/php7/sbin/php-fpm netstat -npa | grep 9000
8.配置穩當後,即可以複製php管理腳本,並加入到開機自啓動列表
[root@localhost php-7.0.0]# cp -a sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm [root@localhost php-7.0.0]# chmod 755 -R /etc/rc.d/init.d/php-fpm [root@localhost php-7.0.0]# chkconfig php-fpm on [root@localhost php-7.0.0]# [root@localhost php-7.0.0]# chkconfig --list php-fpm 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]'. php-fpm 0:off 1:off 2:on 3:on 4:on 5:on 6:off