從yum源中獲得官方爲我們提供的mysql、php等其功能有限,而我們自行編譯,是爲了定製化,可以在編譯過程中添加我們需要的功能模塊,需要什麼服務就安裝什麼,這樣系統就比較純淨。
實驗環境
主機(版本:ip) | 功用 |
---|---|
虛擬機server1(rhel6.5:172.25.2.1) | 搭建lnmp |
真機(rhel7.3:172.25.2.250) | 測試機 |
首先,封裝一個rhel6.5的母盤,然後快照一個鏡像
一、源碼編譯MySQL
1、解壓安裝mysql:tar zxf mysql-boost-5.7.17.tar.gz
2、安裝cmake,cmake相當於configure,用來編譯yum install -y cmake-2.8.12.2-4.el6.x86_64.rpm
3、進入解壓目錄,編譯:
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lnmp/mysql \ #安裝目錄
-DMYSQL_DATADIR=/usr/local/lnmp/mysql/data \ #數據庫存放目錄
-DMYSQL_UNIX_ADDR=/usr/local/lnmp/mysql/data/mysql.sock \ #Unix socket 文件路徑
-DWITH_MYISAM_STORAGE_ENGINE=1 \ #安裝 myisam 存儲引擎
-DWITH_INNOBASE_STORAGE_ENGINE=1 \ #安裝 innodb 存儲引擎
-DDEFAULT_CHARSET=utf8 \ #使用 utf8 字符
-DDEFAULT_COLLATION=utf8_general_ci \ #校驗字符
-DEXTRA_CHARSETS=all \ #安裝所有擴展字符集
-DWITH_BOOST=boost/boost_1_59_0/
- 當報錯:
CMake Error at cmake/boost.cmake:81 (MESSAGE):
You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST=<directory>
This CMake script will look for boost in <directory>. If it is not there,
it will download and unpack it (in that directory) for you.
If you are inside a firewall, you may need to use an http proxy:
export http_proxy=http://example.com:80
解決:缺少 -DWITH_BOOST= 參數 編譯時添加-DWITH_BOOST=boost/boost_1_59_0/
4、發現報錯,根據提示解決依賴性問題,需要安裝:yum install -y gcc gcc-c++ ncurses-devel
5、再次重新編譯:
- 重新編譯時,需要清除原來的對象文件和緩存信息:
make clean;rm -fr CmakeCache.txt
6、發現有一個警告:
CMake Warning at cmake/bison.cmake:20 (MESSAGE):
Bison executable not found in PATH
Call Stack (most recent call first):
sql/CMakeLists.txt:549 (INCLUDE)
解決:yum install -y bison
,再次重新編譯。
7、make && make install
過程時間較長,如果make還有問題,刪除原來的目錄,重新解壓編譯
8、複製啓動腳本/root/mysql-5.7.17/support-files/mysql.server
到/etc/init.d/mysqld
,並給可執行權限chmod +x /etc/init.d/mysqld
9、將原來的/etc/my.cnf
備份,重新拷貝安裝包裏的到/etc
下
[root@server1 support-files]# cp my.cnf my.cnf.bak
[root@server1 support-files]# cp my-default.cnf /etc/my.cnf
10、編輯/etc/my.cnf
修改配置:
basedir = /usr/local/lnmp/mysql
datadir = /usr/local/lnmp/mysql/data
socket = /usr/local/lnmp/mysql/data/mysql.sock
11、添加用戶和組,指定id爲27,因爲rpm默認安裝時mysql用戶的id爲27,並非強制
[root@server1 support-files]# groupadd -g 27 mysql
[root@server1 support-files]# useradd -u 27 -g 27 mysql
[root@server1 support-files]# chown mysql.mysql /usr/local/lnmp/mysql -R
# 修改/usr/local/lnmp/mysql目錄的權限,使其對mysql用戶可寫
12、將mysql命令
添加到環境變量
[root@server1 support-files]# vim ~/.bash_profile
添加:PATH=$PATH:$HOME/bin:/usr/local/lnmp/mysql/bin
[root@server1 support-files]# source ~/.bash_profile
13、初始化mysql,會生成一個臨時密碼,用於登錄mysql(要記住此密碼)
14、爲了安全,再將mysql目錄的所有者改爲root,但是 數據目錄所有者必須是mysql,不然mysql用戶不能寫
[root@server1 support-files]# chown root /usr/local/lnmp/mysql -R
[root@server1 support-files]# chown mysql /usr/local/lnmp/mysql/data -R
15、啓動mysql:/etc/init.d/mysqld start
,使用初始化密碼登錄mysql
16、執行show databases;
會出現報錯,這是因爲沒有進行安全初始化
17、執行:mysql_secure_installation
,然後會提示是否啓用密碼檢測插件,直接回車不啓用,否則會要求密碼有大小寫和特殊字符等要求,剩餘全部選 y
此時,再次登錄數據庫,可以查看數據庫了。
可以看到數據目錄下生成了套節字文件
最後,可以看到mysql服務已開啓。
二、源碼編譯安裝PHP
1、解壓php的tar包tar jxf php-5.6.35.tar.bz2
2、進入解壓後的目錄/root/php-5.6.35
,開始編譯
./configure --prefix=/usr/local/lnmp/php \
--with-config-file-path=/usr/local/lnmp/php/etc \ #指定php.ini位置
--with-openssl --with-snmp --with-gd --with-zlib --with-curl \
--with-libxml-dir --with-png-dir \
--with-jpeg-dir --with-freetype-dir --with-gmp \ # --with-freetype-dir 打開對freetype字體庫的支持
--with-gettext --with-pear --enable-mysqlnd \
--with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
# --with-MySQL=mysql安裝目錄,對mysql的支持 ;--with-MySQLi=mysqli文件目錄,優化支持
--enable-inline-optimization --enable-soap --enable-ftp \
--enable-sockets --enable-mbstring --enable-fpm \ #--enable-fpm參數即可開啓PHP-FPM
--with-fpm-user=nginx --with-fpm-group=nginx \
# 編譯的時候指定了php的用戶是nginx用戶,故要建立nginx用戶
--with-mcrypt --with-mhash
3、出現報錯:
- 報錯1:
解決:yum install -y libxml2-devel
,再次編譯
- 報錯2:
解決:yum install -y openssl-devel
,再次編譯
- 報錯3:
解決:yum install -y libcurl-devel
,再次編譯
- 報錯4:
解決:yum install -y libjpeg-turbo-devel-1.2.1-1.el6.x86_64
,再次編譯
- 報錯5:
解決:yum install -y libpng-devel-1.2.49-1.el6_2.x86_64
,再次編譯
- 報錯6,及解決:
- 報錯7:
解決:
- 報錯8,及解決:
4、再次編譯,成功。
5、安裝make && make install
6、編輯配置文件php-fpm.conf
:
[root@server1 php-5.6.35]# cd /usr/local/lnmp/php/etc
[root@server1 etc]# ls
pear.conf php-fpm.conf.default
[root@server1 etc]# cp php-fpm.conf.default php-fpm.conf
# 複製一份配置文件模版
[root@server1 etc]# vim php-fpm.conf # 編輯fastcgi的進程管理文件
25 pid = run/php-fpm.pid
149 user = nginx
150 group = nginx
7、拷貝更改php主配置文件/usr/local/lnmp/php/etc/php.ini
,修改時區
[root@server1 ~]# cd /root/php-5.6.35
[root@server1 php-5.6.35]# cp php.ini-production /usr/local/lnmp/php/etc/php.ini
[root@server1 php-5.6.35]# vim /usr/local/lnmp/php/etc/php.ini
date.timezone = Asia/Shanghai
8、拷貝php啓動腳本至/etc/init.d/
[root@server1 php-5.6.35]# cd /root/php-5.6.35/sapi/fpm
[root@server1 fpm]# ls
config.m4 init.d.php-fpm.in php-fpm.8 php-fpm.service tests
CREDITS LICENSE php-fpm.8.in php-fpm.service.in www.conf.in
fpm Makefile.frag php-fpm.conf status.html
init.d.php-fpm php-fpm php-fpm.conf.in status.html.in
[root@server1 fpm]# cp init.d.php-fpm /etc/init.d/php-fpm
[root@server1 fpm]# chmod +x /etc/init.d/php-fpm
[root@server1 fpm]# useradd nginx
[root@server1 fpm]# id nginx
uid=500(nginx) gid=500(nginx) groups=500(nginx)
[root@server1 fpm]# /etc/init.d/php-fpm start
Starting php-fpm done
9、此時,netstat -tlnp
可以看到php已經啓動,默認php服務開啓9000端口。
三、源碼編譯nginx
1、解壓nginx的tar包:tar zxf nginx-1.17.4.tar.gz
2、進入nginx解壓後的目錄,修改src/core/nginx.h
文件,刪除nginx版本號
3、編輯auto/cc/gcc
文件,關閉debug模式
4、開始編譯安裝
./configure --prefix=/usr/local/lnmp/nginx --with-http_ssl_module --with-http_stub_status_module --with-threads --with-file-aio --user=nginx --group=nginx
5、出現報錯,安裝yum install pcre-devel -y
,然後再次編譯。
6、編譯成功,安裝make && make install
7、編輯nginx配置文件/usr/local/lnmp/nginx/conf/nginx.conf
,開啓php模塊
[root@server1 nginx-1.17.4]# vim /usr/local/lnmp/nginx/conf/nginx.conf
43 location / {
44 root html;
45 index index.php index.html index.htm; (添加php頁面)
46 }
65 location ~ \.php$ {
66 root html;
67 fastcgi_pass 127.0.0.1:9000;
68 fastcgi_index index.php;
69 #fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 include fastcgi.conf;
71 }
8、創建軟鏈接,將nginx啓動腳本鏈接到/usr/local/sbin/
,然後啓動nginx。
[root@server1 nginx-1.17.4]# ln -s /usr/local/lnmp/nginx/sbin/nginx /usr/local/sbin/
[root@server1 nginx-1.17.4]# nginx -t
[root@server1 nginx-1.17.4]# nginx
9、此時,可以看到php、nginx、mysql服務全部開啓
測試:
1、訪問:http://172.25.2.1
,可以看到nginx默認發佈頁面
2、在nginx默認發佈目錄添加 index.php
vim /usr/local/lnmp/nginx/html/index.php
<?php
phpinfo()
?>
訪問:http://172.25.2.1/index.php
,可以看到php默認發佈頁面
至此,lnmp環境就已經搭建完成了。