1. 程式人生 > >源碼編譯安裝LNMP

源碼編譯安裝LNMP

lnmp 編譯安裝 nginx php mysql

LNMP

LNMP就是:Linux系統下Nginx+MySQL+PHP這種網站服務器架構。

Linux是一類Unix計算機操作系統的統稱,是目前最流行的免費操作系統。代表版本有:debian、centos、ubuntu、fedora、gentoo等。

Nginx是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP代理服務器。

Mysql是一個小型關系型數據庫管理系統。

PHP是一種在服務器端執行的嵌入HTML文檔的腳本語言。

這四種軟件均為免費開源軟件,組合到一起,成為一個免費、高效、擴展性強的網站服務系統。


開始搭建LNMP環境

註:由於系統是最小化安裝的CentOS 7,所以在編譯安裝之前需要安裝gcc


[root@localhost ~]# yum install -y gcc*


1、編譯安裝Nginx

(1)、安裝軟件依賴包

[root@localhost ~]# yum -y groupinstall "Development Tools" "Server Platform Deveopment"
[root@localhost ~]# yum -y install openssl-devel pcre-devel


(2)、下載Nginx安裝包並解壓


[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
[root@localhost src]# useradd nginx    ##創建Nginx用戶
[root@localhost src]# tar -xf nginx-1.12.0.tar.gz


(3)、編譯安裝Nginx


[root@localhost src]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module  --with-pcre
[root@localhost nginx-1.12.0]# make && make install

註:
--prefix:Nginx安裝目錄
--user:Nginx用戶
--group:Nginx用戶所屬組
--with-http_ssl_module:提供https支持
--with-http_flv_module:搭建flv視頻服務器使用的
--with-http_stub_status_module:開啟Stub Status模塊,該模塊會產生一個服務器狀態和信息頁
--with-http_gzip_static_module:開啟Gzip靜態模塊,該模塊用於發送預壓縮文件
--with-pcre:perl執行文件路徑


(4)、配置Nginx啟動腳本

[root@localhost ~]# 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=/usr/local/nginx/logs/nginx.pid  
ExecStartPre=/usr/bin/rm -f /run/nginx.pid  
ExecStartPre=/usr/local/nginx/sbin/nginx -t  
ExecStart=/usr/local/nginx/sbin/nginx  
ExecReload=/bin/kill -s HUP $MAINPID  
KillMode=process  
KillSignal=SIGQUIT  
TimeoutStopSec=5  
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod a+x /usr/lib/systemd/system/nginx.service   ##給權限


(5)、啟動Nginx

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t      ##檢查Nginx的配置是否有問題
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
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# ps -ef | grep nginx
root      10072      1  0 10:52 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nginx     10073  10072  0 10:52 ?        00:00:00 nginx: worker process
root      10075    963  0 10:52 pts/0    00:00:00 grep --color=auto nginx


(6)、訪問Nginx,如圖1所示

技術分享圖片

圖1


2、編譯安裝php-fpm

(1)、安裝依賴包

[root@localhost ~]# yum -y install libmcrypt-devel bzip2-devel gcc openssl-devel php-mcrypt libmcrypt libxml2-devel libjpeg-devel libpng-devel freetype-devel


(2)、下載php-fpm安裝包並解壓

[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget http://cn2.php.net/distributions/php-5.5.38.tar.gz
[root@localhost src]# tar -xf php-5.5.38.tar.gz


(3)、編譯安裝php-fpm


[root@localhost src]# cd php-5.5.38
[root@localhost php-5.5.38]# ./configure --prefix=/usr/local/php --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --with-mcrypt  --with-bz2 --enable-fpm --with-gd
[root@localhost php-5.5.38]# make && make install    ##這一步時間有點久


(4)、修改一些配置

[root@localhost php-5.5.38]# cp /usr/local/src/php-5.5.38/php.ini-production /usr/local/php/etc/php.ini
[root@localhost php-5.5.38]# mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-5.5.38]# useradd -M -s /sbin/nologin php    ##創建用戶
[root@localhost php-5.5.38]# sed -i -e 's\;pid = run/php-fpm.pid\pid = run/php-fpm.pid\g' -e 's\nobody\php\g' -e 's\listen = 127.0.0.1:9000\listen = 0.0.0.0:9000\g' /usr/local/php/etc/php-fpm.conf
[root@localhost php-5.5.38]# sed -i 's\;daemonize = yes\daemonize = no\g' /usr/local/php/etc/php-fpm.conf


(5)、配置啟動腳本

[root@localhost ~]# cd /usr/local/src/php-5.5.38/sapi/fpm     ##php-fpm自帶的啟動腳本
[root@localhost fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@localhost fpm]# cd /etc/init.d/
[root@localhost init.d]# chmod +x php-fpm


(6)、啟動php-fpm

[root@localhost ~]# /usr/local/php/sbin/php-fpm -t    ##檢查php-fpm配置是否正常
[22-Dec-2017 11:13:16] NOTICE: configuration file /usr/local/php/etc/php-fpm.conf test is successful
[root@localhost ~]# service php-fpm start
Unit php-fpm.service could not be found.
Starting php-fpm  done
[root@localhost ~]# ps -ef | grep php-fpm
root       4677      1  0 11:12 ?        00:00:00 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
php        4678   4677  0 11:12 ?        00:00:00 php-fpm: pool www
php        4679   4677  0 11:12 ?        00:00:00 php-fpm: pool www
root       4681    963  0 11:12 pts/0    00:00:00 grep --color=auto php-fpm


(7)、訪問PHP

首先先修改Nginx,使其支持PHP

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        location / {
            root   html;
            index  index.html index.htm index.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;
        }
        
##添加PHP代碼
[root@localhost html]# cat /usr/local/nginx/html/index.php 
<?php
  phpinfo();
?>

訪問網頁,如圖2所示

技術分享圖片

圖2


3、編譯安裝MySQL

(1)、安裝依賴包

[root@localhost ~]# yum install -y cmake gcc-c++ ncurses-devel perl-Data-Dumper boost boost-doc boost-devel


(2)、下載MySQL安裝包並解壓


[root@localhost ~]# cd /usr/local/src/
[root@localhost src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-boost-5.7.20.tar.gz
[root@localhost src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.20.tar.gz
[root@localhost src]# tar -xf mysql-boost-5.7.20.tar.gz
[root@localhost src]# tar -xf mysql-5.7.20.tar.gz


(3)、創建用戶並授權


[root@localhost src]# useradd -M -s /sbin/nologin mysql
[root@localhost src]# mkdir -p /usr/local/mysql/mydata
[root@localhost src]# mkdir -p /usr/local/mysql/conf
[root@localhost src]# chown -R mysql:mysql /usr/local/mysql
[root@localhost src]# rm -rf /etc/my.cnf   ##如果不刪除會有沖突


(4)、編譯安裝

[root@localhost src]# cd mysql-5.7.20
[root@localhost mysql-5.7.20]# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/mydata -DSYSCONFDIR=/usr/local/mysql/conf -DMYSQL_USER=mysql -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock -DMYSQL_TCP_PORT=3306 -DEXTRA_CHARSETS=all -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_DEBUG=0 -DMYSQL_MAINTAINER_MODE=0 -DWITH_SSL:STRING=bundled -DWITH_ZLIB:STRING=bundled -DDOWNLOAD_BOOST=1 -DWITH_BOOST=./boost
[root@localhost mysql-5.7.20]# make && make install    ##耗時比較長

註:
-DCMAKE_INSTALL_PREFIX=/usr/local/server/mysql-5.6.12 設置安裝目錄
-DMYSQL_DATADIR=/data/mysql 設置數據庫存放目錄
-DMYSQL_UNIX_ADDR=/tmp/mysql.sock 設置UNIX socket 目錄
-DMYSQL_USER=mysql 設置運行用戶
-DDEFAULT_CHARSET=utf8 設置默認字符集,默認latin1
-DEFAULT_COLLATION=utf8_general_ci 設置默認校對規則,默認latin1_general_ci
-DWITH_INNOBASE_STORAGE_ENGINE=1 添加InnoDB引擎支持
-DENABLE_DOWNLOADS=1 自動下載可選文件,比如自動下載谷歌的測試包
-DMYSQL_TCP_PORT=3306 設置服務器監聽端口,默認3306
-DSYSCONFDIR=/data/etc 設置my.cnf所在目錄,默認為安裝目錄
更多參數執行 # cmake . -LH 或者查看官方說明


(5)、修改配置

##設置添加到系統服務並設置開機啟動

[root@localhost ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost ~]# chmod +x /etc/init.d/mysqld
[root@localhost ~]# cp /usr/local/mysql/bin/mysql /usr/bin/mysql    ##將MySQL添加到bash裏面


##初始化MySQL,如圖3(初始密碼為圖3中劃線部分)

[root@localhost ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mydata

技術分享圖片

圖3

##修改密碼(需要啟動MySQL服務後才能進入數據庫修改密碼,密碼為初始化後的密碼,即圖3劃線部分),如圖4所示

SET PASSWORD = PASSWORD('00000000');

技術分享圖片

圖4


(6)、配置啟動腳本

[root@localhost ~]# cat /usr/lib/systemd/system/mysql.service
[Unit]
Description=MariaDB server and services
After=syslog.target
After=network.target
[Service]
Type=simple
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld_safe --basedir=/usr/local/mysql
TimeoutSec=300
PrivateTmp=false
[Install]
WantedBy=multi-user.target

[root@localhost ~]# chmod a+x /usr/lib/systemd/system/mysql.service   ##授權
[root@localhost ~]# systemctl start mysql
[root@localhost ~]# ps -ef | grep mysql
mysql     23300      1  0 12:22 ?        00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --basedir=/usr/local/mysql
mysql     23378  23300  3 12:22 ?        00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mydata --plugin-dir=/usr/local/mysql/lib/plugin --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid
root      23408    963  0 12:22 pts/0    00:00:00 grep --color=auto mysql


至此,LNMP環境就已經搭建完成了




源碼編譯安裝LNMP