Centos 6.8編譯安裝LNMP環(huán)境(Nginx+MySQL+PHP)教程
前言
對(duì)于新手的一點(diǎn)建議:
- 最好熟悉一下linux 的基本命令,vim的常用命令
- 千萬不要無腦復(fù)制,先看一下命令,特別是路徑要注意
- 學(xué)會(huì)排查錯(cuò)誤
本篇安裝的軟件版本為:
- Linux:Centos6.8
- Nginx:1.10.3
- MySQL:5.7.17
- PHP:7.0.16
最近研究了Linux系統(tǒng)下的PHP環(huán)境搭建,個(gè)人感覺最好最好不要用yum默認(rèn)的程序包安裝,因?yàn)榘姹径急容^低,下載最新的穩(wěn)定版自行安裝比較好。現(xiàn)在網(wǎng)上教程很多,之所以還記這篇,原因有一點(diǎn),當(dāng)你重復(fù)網(wǎng)上的教程自行安裝時(shí),90%還是會(huì)出現(xiàn)各種各樣的問題,因?yàn)槟憧赡躭inux的系統(tǒng)版本不同,你想裝的軟件版本不同,安裝的方法不同,你下錯(cuò)了安裝包的版本,還有其它亂七八糟的。舉個(gè)例,比如你看著5.6的mysql安裝教程,裝5.7的,你感覺沒問題,但是事實(shí)就是,5.7的不一樣了!而且網(wǎng)上還沒有新的這方面內(nèi)容,不好找,這就需要你去摸索了,親身經(jīng)歷啊。這里面,Niginx感覺最好配,MySQL最坑。

一 準(zhǔn)備工作
1. 關(guān)閉SELINUX
修改配置文件,重啟服務(wù)后永久生效。
# sed -i ‘s/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
命令行設(shè)置立即生效。
# setenforce 0
2. 如果是阿里云ECS用戶,安全組設(shè)置中開啟80端口方便調(diào)試。
二 安裝Nginx
1. 下載源碼包
上Nginx官網(wǎng),復(fù)制最新穩(wěn)定版的下載地址過來,然后用wget下載(接下來需要下載安裝包的都可以用wget):
# cd /usr/local/src # wget http://nginx.org/download/nginx-1.10.3.tar.gz

下載完成的狀態(tài)基本都是以下這樣的:

2. 進(jìn)行解壓編譯
# tar xvf nginx-1.10.3.tar.gz # yum groupinstall “Development tools” # yum -y install gcc wget gcc-c++ automake autoconf libtool libxml2-devel libxslt-devel perl-devel perl-ExtUtils-Embed pcre-devel openssl-devel

執(zhí)行完成。
進(jìn)入解壓后的nginx-1.10.3文件夾:
cd /usr/local/src/nginx-1.10.3
執(zhí)行以下語句:
./configure \ --prefix=/usr/local/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/tmp/nginx/client \ --http-proxy-temp-path=/var/tmp/nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --user=nginx \ --group=nginx \ --with-pcre \ --with-http_v2_module \ --with-http_ssl_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_sub_module \ --with-http_dav_module \ --with-http_flv_module \ --with-http_mp4_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_stub_status_module \ --with-http_auth_request_module \ --with-mail \ --with-mail_ssl_module \ --with-file-aio \ --with-ipv6 \ --with-http_v2_module \ --with-threads \ --with-stream \ --with-stream_ssl_module
完成后執(zhí)行編譯:
# make && make install # mkdir -pv /var/tmp/nginx/client
3. 添加SysV啟動(dòng)腳本。
用vim編輯腳本:
# vim /etc/init.d/nginx
寫入以下內(nèi)容:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
killall -9 nginx
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
保存退出(按:wq!);可能你得稍微查一下vim的一些命令,不然操作時(shí)可能會(huì)出現(xiàn)一點(diǎn)小問題。
賦予腳本執(zhí)行權(quán)限:
# chmod +x /etc/init.d/nginx
添加至服務(wù)管理列表,設(shè)置開機(jī)自啟:
# chkconfig –add nginx # chkconfig nginx on
4. 啟動(dòng)服務(wù)。
# service nginx start

出現(xiàn)這玩意說明成功了!
注:如果報(bào)錯(cuò) [emerg]: getpwnam(“nginx”) failed ;
解決方法:
# useradd -s /sbin/nologin -M nginx # id nginx
三 安裝mysql
1. 版本選擇
在安裝之前必須明白一件事情,mysql有很多種安裝方式,每種不一樣,不要弄混了。
比如源碼編譯安裝(mysql-5.7.17.tar.gz),二進(jìn)制安裝(mysql-5.7.17-linux-glibc2.5-i686.tar),nmp安裝(最簡單的)。這里我們用源碼自己編譯安裝。
2. 準(zhǔn)備編譯環(huán)境
# yum groupinstall “Server Platform Development” “Development tools” -y # yum install cmake -y
cmake在現(xiàn)在的版本是必須要安裝的,你可以下載camke之后編譯,也可以直接yum安裝。接下來的編譯過程如果報(bào)錯(cuò)缺少什么就補(bǔ)什么。
3. 準(zhǔn)備mysql數(shù)據(jù)庫存放目錄
# mkdir /mnt/data # groupadd -r mysql # useradd -r -g mysql -s /sbin/nologin mysql # id mysql
4. 更改數(shù)據(jù)目錄權(quán)限。
# chown -R mysql:mysql /mnt/data
5. 下載并解壓編譯官網(wǎng)下載的穩(wěn)定版的源碼包。
在下載的時(shí)候注意一下版本,下載對(duì)應(yīng)的版本。我們?cè)创a編譯,要下載長這樣的安裝包:mysql-5.7.17.tar.gz,同時(shí)在安裝的時(shí)候我們需要boost庫,5.7需要1.59版本的庫;你可以下載boost庫然后編譯boost庫,或者像我一樣,下載帶有boost庫的mysql版本。
開始解壓編譯:
# tar xvf mysql-boost-5.7.17.tar.gz -C /usr/local/src # cd /usr/local/src/mysql-5.7.17 # cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_DATADIR=/mnt/data \ -DSYSCONFDIR=/etc \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_READLINE=1 \ -DWITH_SSL=system \ -DWITH_ZLIB=system \ -DWITH_LIBWRAP=0 \ -DMYSQL_TCP_PORT=3306 \ -DMYSQL_UNIX_ADDR=/tmp/mysql.sock \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci -DDOWNLOAD_BOOST=1 \ -DWITH_BOOST=/usr/local/mysql/boost/boost_1_59_0 \ # make && make install
6. 修改安裝目錄的權(quán)限屬組
# chown -R mysql:mysql /usr/local/mysql/
7. 初始化數(shù)據(jù)庫。
# /usr/local/mysql/bin/mysqld –initialize –user=mysql –basedir=/usr/local/mysql –datadir=/mnt/data/
需要注意這里是mysql5.7的初始化命令,而5.7以下的都是用:
# /usr/local/mysql/scripts/mysql_install_db –user=mysql –datadir=/mnt/data/
在初始化成功之后,5.7的initial命令會(huì)產(chǎn)生一個(gè)隨機(jī)的root登錄密碼,你要用這個(gè)密碼登錄,然后修改(必須修改生成的隨機(jī)密碼不然無法后續(xù)操作)。在最后有一個(gè)類似這樣的密碼:

8. 復(fù)制配置文件
# cp support-files/my-default.cnf /etc/my.cnf
這里又有一點(diǎn)要注意:mysql5.7配置文件需要修改my.cnf關(guān)鍵配置, mysql5.7之前默認(rèn)配置文件中是有配置項(xiàng)的,不用手動(dòng)修改。以下為配置,根據(jù)實(shí)際情況修改:
</div> <div>[mysqld]</div> <div>basedir = /usr/local/mysql</div> <div>datadir = /mnt/data</div> <div>port = 3306</div> <div>socket = /Ultrapower/test/mysql/tmp/mysql.sock</div> <div></div> <div>sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES</div> <div>[client]</div> <div>socket = /Ultrapower/test/mysql/tmp/mysql.sock</div> <div>
如果添加[client]下 的內(nèi)容,注意sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES要放在[mysqld]下。
如果報(bào)錯(cuò)tmp目錄不錯(cuò)在,到對(duì)應(yīng)的地方去創(chuàng)建目錄,然后創(chuàng)建后要賦予mysql權(quán)限,chown -R mysql:mysql tmp。
9. 設(shè)置開機(jī)啟動(dòng)
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld # chmod +x /etc/init.d/mysql
注冊(cè)為開機(jī)啟動(dòng)服務(wù):
# chkconfig mysqld on # chkconfig –add mysqld
查看是否設(shè)置成功:
# chkconfig –list mysql
10. 設(shè)置PATH環(huán)境變量。
# echo “export PATH=$PATH:/usr/local/mysql/bin” > /etc/profile.d/mysql.sh # source /etc/profile.d/mysql.sh
11. 啟動(dòng)服務(wù)
# service mysqld start

這樣基本上,這個(gè)mysql就裝好了。
12. 登錄mysql并修改密碼
mysql -uroot -p生成的密碼
執(zhí)行修改密碼:
alter user ‘root'@'localhost' identified by ‘newpassword';
四 安裝php-fpm
1. 安裝依賴包:
yum install libmcrypt libmcrypt-devel mhash mhash-devel libxml2 libxml2-devel bzip2 bzip2-devel
這里還漏了幾個(gè),如果報(bào)錯(cuò)了提示缺少了什么就yum補(bǔ)上。
2. 到官網(wǎng)下載源碼包后,開始編譯安裝:
# tar xvf php-7.0.16.tar.bz2 -C /usr/local/src # cd /usr/local/src/php-7.0.16 執(zhí)行下面的配置文件: # ./configure --prefix=/usr/local/php \ --with-config-file-scan-dir=/etc/php.d \ --with-config-file-path=/etc \ --with-mysql=/usr/local/mysql \ --with-mysqli=/usr/local/mysql/bin/mysql_config \ --enable-fpm \ --enable-opcache \ --disable-fileinfo \ --with-jpeg-dir \ --with-iconv-dir=/usr/local \ --with-freetype-dir \ --with-png-dir \ --with-zlib \ --with-libxml-dir=/usr \ --enable-xml \ --enable-bcmath \ --enable-shmop \ --enable-exif \ --with-curl \ --enable-sysvsem \ --enable-inline-optimization \ --enable-mbregex \ --enable-inline-optimization \ --enable-mbstring \ --with-mcrypt \ --with-gd \ --enable-gd-native-ttf \ --with-openssl \ --with-mhash \ --enable-pcntl \ --enable-sockets \ --with-xmlrpc \ --enable-ftp \ --with-gettext \ --enable-zip \ --enable-soap \ --with-bz2
執(zhí)行以上的配置,如果出現(xiàn)下面這樣的license,才是正確的,才可以開始編譯,如果出問題,就解決,一般是少了什么庫。

執(zhí)行編譯:
# make && make install
3. 添加php和php-fpm配置文件。
# cp /usr/local/src/php-7.0.16/php.ini-production /etc/php.ini # cd /usr/local/php/etc/ # cp php-fpm.conf.default php-fpm.conf # sed -i ‘s@;pid = run/php-fpm.pid@pid = /usr/local/php/var/run/php-fpm.pid@' php-fpm.conf
4. 添加php-fpm啟動(dòng)腳本。
# cp /usr/local/src/php-7.0.16/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm # chmod +x /etc/init.d/php-fpm
5. 添加php-fpm至服務(wù)列表并設(shè)置開機(jī)自啟。
# chkconfig –add php-fpm # chkconfig –list php-fpm # chkconfig php-fpm on
6. 啟動(dòng)服務(wù)。
# service php-fpm start
注:啟動(dòng)時(shí)如出現(xiàn)錯(cuò)誤:
WARNING: Nothing matches the include pattern ‘/usr/local/etc/php-fpm.d/*.conf' from /usr/local/etc/php-fpm.conf at line 125. ERROR:. No pool defined at least one pool section must be specified in config file ERROR: failed to post process the configuration ERROR: FPM initialization failed
解決:到指定目錄執(zhí)行cp www.conf.default www.conf
7. 添加nginx對(duì)fastcgi的支持,
首先備份默認(rèn)的配置文件。
# cp /etc/nginx/nginx.conf /etc/nginx/nginx.confbak # cp /etc/nginx/nginx.conf.default /etc/nginx/nginx.conf
編輯/etc/nginx/nginx.conf,在所支持的主頁面格式中添加php格式的主頁,類似如下:
</div>
<div>location / {</div>
<div>root /usr/local/nginx/html;</div>
<div>index index.php index.html index.htm;</div>
<div>}</div>
<div>
取消以下內(nèi)容前面的注釋:
</div>
<div>location ~ \.php$ {</div>
<div>root /usr/local/nginx/html;</div>
<div>fastcgi_pass 127.0.0.1:9000;</div>
<div>fastcgi_index index.php;</div>
<div>fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;</div>
<div>include fastcgi_params;</div>
<div>}</div>
<div>
8. 重啟nginx
# service nginx reload
9. 測試是否成功
在/usr/local/nginx/html/新建index.php的測試頁面,內(nèi)容如下:
<?php phpinfo(); ?>
如果出現(xiàn)這個(gè)熟悉的界面,說明就大功告成了!Linux下一個(gè)基本的LNMP就搭建完畢了。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- 詳解Centos7源碼編譯安裝 php7.2之生產(chǎn)篇
- Centos7.2 編譯安裝方式搭建 phpMyAdmin
- Centos7.2 編譯安裝PHP7.0.2的步驟
- Linux 6 下編譯安裝 PHP 5.6實(shí)例詳解
- 解決PHP 7編譯安裝錯(cuò)誤:cannot stat ‘phar.phar’: No such file or directory
- Centos7下編譯安裝配置Nginx+PHP+MySql環(huán)境
- CentOS 6.5編譯安裝Nginx 1.10.2+MySQL 5.5.52+PHP5.5.38
- Ubuntu 16.04源碼編譯安裝PHP 5.6.29的教程
- Centos 6.5系統(tǒng)下編譯安裝PHP 7.0.13的方法
- PHP7.3.10編譯安裝教程
相關(guān)文章
Deepin系統(tǒng)中g(shù)rub配置的說明和修改方式
GRUB是一種多操作系統(tǒng)啟動(dòng)程序,主配置文件位于/boot/grub/grub.cfg,但通常通過編輯/etc/default/grub文件來修改配置,該文件允許用戶設(shè)置默認(rèn)啟動(dòng)操作系統(tǒng)、啟動(dòng)超時(shí)時(shí)間等,修改后需運(yùn)行特定命令更新配置2024-09-09
16個(gè)簡單實(shí)用的.htaccess使用技巧
這篇文章包括了16個(gè)非常有用的小技巧。另外,因?yàn)?htaccess 是一個(gè)相當(dāng)強(qiáng)大的配置文件,所以,一個(gè)輕微的語法錯(cuò)誤會(huì)造成你整個(gè)網(wǎng)站的故障,所以,在你修改或是替換原有的文件時(shí),一定要備份舊的文件,以便出現(xiàn)問題的時(shí)候可以方便的恢復(fù)。2011-04-04
使用Apache commons-cli包進(jìn)行命令行參數(shù)解析的示例代碼
Apache的commons-cli包是專門用于解析命令行參數(shù)格式的包。這篇文章給大家介紹使用Apache commons-cli包進(jìn)行命令行參數(shù)解析的示例代碼,感興趣的朋友跟隨腳本之家小編一起學(xué)習(xí)吧2018-05-05
deepin20 安裝英偉達(dá)閉源驅(qū)動(dòng)的步驟詳解
這篇文章主要介紹了deepin20 安裝英偉達(dá)閉源驅(qū)動(dòng)的步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
判斷Unix系統(tǒng)及庫文件是32位還是64位的詳解
這篇文章主要介紹了判斷Unix系統(tǒng)及庫文件是32位還是64位的的相關(guān)資料,這里整理下查看系統(tǒng)位數(shù)的命令,需要的朋友可以參考下2016-11-11

