源碼編譯安裝LNMP
環境:CentOS6.6
IP: 172.16.10.10/16 GW:172.16.0.2
主機名稱: lnmp.test.net
一、常規設置:
網卡:
臨時
ifconfig eth0 172.16.10.10/16 up
永久
[root@www ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
NAME=eth0
ONBOOT=yes
HWADDR=00:0C:29:7F:31:BD
IPADDR=172.16.10.10
PREFIX=16
GATEWAY=172.16.0.2
主機名:
臨時
[root@www ~]# hostname lnmp.test.net
永久
[root@www ~]# vim /etc/sysconfig/network
HOSTNAME=lnmp.test.net
網卡名稱設定為eth0
[root@www ~]# vim /etc/udev/rules.d/70-persistent-net.rules
# PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="00:0c:29:7f:31:bd", ATTR{type}=="1", KER
NEL=="eth*", NAME="eth0"
禁用selinux及iptables防火墻
臨時
[root@lnmp ~]# setenforce 0
永久
[root@lnmp ~]# vim /etc/selinux/config
SELINUX=disabled
[root@lnmp ~]# service iptables stop
[root@lnmp ~]# service ip6tables stop
[root@lnmp ~]# chkconfig iptables off
[root@lnmp ~]# chkconfig ip6tables off
二、安裝規劃
1)nginx、mysql、php的安裝順序:
因為nginx無法像apache一樣以內置模法方式與php工作,因而只能將PHP以fastCGI方式與nginx進行協同,亦即安裝php-fpm。
安裝順序為先安裝nginx,然后再安裝php-fpm。至于mysql,本質上只是php程序內置的php-mysql組件在軟件運行時以mysql客戶端的身份向mysql發起連接,
因而mysql的安裝順序并不重要,我們將mysql放在最后才安裝。
2)服務器規劃:
如上所述,php-fpm以fastCGI與nginx通訊,以mysql client與mysql server通訊,因而上述幾個軟件是各自獨立的存在,可以按實際的應用需求將它們組合在一起,
可以將三個角色都安裝在一臺服務器上,也可以分別將每個角色安裝于單獨的一臺服務器。甚至安裝于一個服務器群集內。這是按實際的業務需求來做規劃擴展的。在本
例中,簡單起見,我將三個角色都安裝于同一臺服務器lnmp上,如需安裝于2臺或3臺服務器,只需在配置更改一下相應軟件的監聽IP即可,安裝過程都是一樣的。至于安
裝多于3臺的服務器集群,基于數據一致性及同可用協同的原因,通常均需要另外的部署方法,后面我會再另行擬文介紹。
服務器名稱:nginx.test.net
服務器IP:172.16.10.10/16
服務器角色:nginx服務器、PHP fastCGI服務器及MariaDB服務器
三、安裝nginx
安裝準備:
nginx的編譯安裝比較簡單,但它依賴gcc,openssl,pcre,zlib等幾個模塊,需提前安裝這幾個軟件,免得等下安裝nginx時報錯。
[root@lnmp yum.repos.d]# yum install gcc openssl-devel pcre-devel zlib-devel -y
##然后,nginx的工作進程運行在用戶空間,一般是建一個專門的用戶用于其運行,這里我們新建nginx用戶
[root@lnmp ~]# groupadd -r nginx
[root@lnmp ~]# useradd -r -g nginx -s /sbin/nologin -M nginx
編譯安裝:
##解壓
[root@lnmp nginx-1.10.1]# tar xzvf nginx-1.10.1.tar.gz
[root@lnmp LAMP]# cd nginx-1.10.1
##配置
[root@lnmp nginx-1.10.1]# ./configure \
–prefix=/usr \
–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/nginx.pid \
–lock-path=/var/lock/nginx.lock \
–user=nginx \
–group=nginx \
–with-http_ssl_module \
–with-http_flv_module \
–with-http_stub_status_module \
–with-http_gzip_static_module \
–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 \
–with-pcre
##編譯&安裝
[root@lnmp nginx-1.10.1]# make && make install
##其中編譯的選項大致信息如下:
[root@lnmp nginx-1.10.1]# ./configure \
–prefix=/usr \ ##安裝位置 /usr
–sbin-path=/usr/sbin/nginx \ ##二進制文件安裝位置 /usr/sbin/nginx
–conf-path=/etc/nginx/nginx.conf \ ##配置文件路徑
–error-log-path=/var/log/nginx/error.log \ ##error錯誤日志文件路徑
–http-log-path=/var/log/nginx/access.log \ ##http訪問日志文件路徑
–pid-path=/var/run/nginx/nginx.pid \ ##pid文件路徑
–lock-path=/var/lock/nginx.lock \ ##lock文件路徑
–user=nginx \ ##使用nginx用戶、用戶組運行程序
–group=nginx \
–with-http_ssl_module \ ##以下是開啟各種功能
–with-http_flv_module \
–with-http_stub_status_module \
–with-http_gzip_static_module \
–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 \
–with-pcre
##安裝完畢
##構建nginx的啟動服務腳本,新建nginx文件,將以下內容復制到該文件中
[root@lnmp nginx-1.10.1]# vim /etc/rc.d/init.d/nginx
#!/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
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*–user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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
}
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
##授予該腳本可執行權限,加入開機自動啟動服務
[root@lnmp nginx-1.10.1]# chmod +x /etc/rc.d/init.d/nginx
[root@lnmp nginx-1.10.1]# chkconfig –add nginx
[root@lnmp nginx-1.10.1]# chkconfig nginx on
##啟動服務,檢查80端口是否正常
[root@lnmp nginx-1.10.1]# service nginx start
Starting nginx: [ OK ]
[root@lnmp nginx-1.10.1]# ss -ntlp | grep nginx
LISTEN 0 128 *:80 *:* users:(("nginx",49816,6),("nginx",49818,6))
[root@lnmp nginx-1.10.1]#
##訪問網頁
[root@lnmp nginx-1.10.1]# curl -I http://localhost
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Mon, 31 Oct 2016 08:41:35 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 31 Oct 2016 08:05:04 GMT
Connection: keep-alive
ETag: "5816fb30-264"
Accept-Ranges: bytes
[root@lnmp nginx-1.10.1]#
##返回狀態HTTP/1.1 200 OK,nginx已能正常工作
三、安裝php-fpm
安裝準備:
##php安裝成fcgi模式
##壓解
[root@lnmp LAMP]# tar xzvf php-5.6.14.tar.gz
[root@lnmp LAMP]# cd php-5.6.14
##說明:如果使用PHP5.3以上版本,為了鏈接MySQL數據庫,可以指定mysqlnd,這樣在本機就不需要先安裝MySQL或MySQL開發包了。mysqlnd從php 5.3開始可用,可以編譯時綁定到它(而不用和具體的MySQL客戶端庫綁定形成依賴),但從PHP 5.4開始它就是默認設置了。
# ./configure –with-mysql=mysqlnd –with-pdo-mysql=mysqlnd –with-mysqli=mysqlnd
[root@lnmp php-5.6.14]# ./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-bz2 –with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d –enable-fpm
[root@lnmp php-5.6.14]# make && make install
##其中的參數說明:
[root@lnmp php-5.6.14]# ./configure –prefix=/usr/local/php ##php安裝路徑
–with-mysql=mysqlnd –with-pdo-mysql=mysqlnd –with-mysqli=mysqlnd ##php與mysql連接的參數
##各種功能的啟用
–with-openssl –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –enable-sockets –with-bz2
–with-config-file-path=/etc –with-config-file-scan-dir=/etc/php.d ##php配置文件路徑
–enable-fpm ##此參數設置php以fpm(fastCGI)方式運行
##安裝完畢,配置php,將fpm服務加入自啟動中
[root@lnmp php-5.6.14]# cp php.ini-production /etc/php.ini
[root@lnmp php-5.6.14]# cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
[root@lnmp php-5.6.14]# chmod +x /etc/rc.d/init.d/php-fpm
[root@lnmp php-5.6.14]# chkconfig –add php-fpm
[root@lnmp php-5.6.14]# chkconfig php-fpm on
##為php-fpm提供配置文件
[root@lnmp php-5.6.14]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
##在php-fpm.conf文件中去掉pid的注釋(其它參數因你的環境而定)
pid = run/php-fpm.pid
##啟動服務,測試進程能否正常啟動
[root@lnmp etc]# ss -ntlp | grep php-fpm
LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",26553,7),("php-fpm",26554,0),("php-fpm",26555,0),("php-fpm",26556,0),("php-fpm",26557,0),("php-fpm",26558,0))
[root@lnmp etc]#
##9000端口已經監聽,php-fpm服務正常運行
三、安裝MariaDB
安裝準備:
##壓解
[root@lnmp LAMP]# tar xzvf mariadb-10.1.16.tar.gz
[root@lnmp LAMP]# cd mariadb-10.1.16
##同理,創建mariadb的安全運行用戶mysql
[root@lnmp mariadb-10.1.16]# groupadd -r mysql
[root@lnmp mariadb-10.1.16]# useradd -r mysql -g mysql -s /sbin/nologin mysql
[root@lnmp mariadb-10.1.16]# useradd -r -g mysql -s /sbin/nologin mysql
##編譯
[root@lnmp mariadb-10.1.16]#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_DATADIR=/usr/local/mysql/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_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
##安裝
[root@lnmp mariadb-10.1.16]# make && make install
##編譯選項
[root@lnmp mariadb-10.1.16]#cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ ##安裝路徑
-DMYSQL_DATADIR=/usr/local/mysql/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_UNIX_ADDR=/tmp/mysql.sock \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci
##安裝完成后,繼續完成相關的配置工作
[root@lnmp mariadb-10.1.16]# cd /usr/local/mysql/
##設定mysqld服務,設成開機自啟動
[root@lnmp support-files]# cp mysql.server /etc/rc.d/init.d/mysqld
[root@lnmp support-files]# chkconfig –add mysqld
[root@lnmp support-files]# chkconfig mysqld on
##設定mysql配置文件
[root@lnmp support-files]# cp my-innodb-heavy-4G.cnf /etc/my.cnf
##創建用戶
[root@lnmp mysql]# groupadd -g 3306 -r mysql
[root@lnmp mysql]# useradd -g 3306 -u 3306 -r -s /sbin/nologin mysql
##將mysql目錄設成mysql用戶所有
[root@lnmp local]# chown -R mysql.mysql mysql/
##執行腳本生成管理數據庫
[root@lnmp mysql]# ./scripts/mysql_install_db –user=mysql –datadir=/usr/local/mysql/data –basedir=/usr/local/mysql
##啟動數據庫
[root@lnmp mysql]# service mysqld start
Starting MySQL. [ OK ]
##檢測安裝正常否
[root@lnmp mysql]# ss -ntlp | grep mysqld
LISTEN 0 80 :::3306 :::* users:(("mysqld",26828,21))
[root@lnmp mysql]# vim /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@lnmp mysql]# . /etc/profile.d/mysql.sh
##登錄mysql數據庫,將所有用戶密碼修改為'redhat'
[root@lnmp mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 7
Server version: 10.1.16-MariaDB Source distribution
Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use mysql
Database changed
MariaDB [mysql]> update user set password = password('redhat');
Query OK, 6 rows affected (0.00 sec)
Rows matched: 6 Changed: 6 Warnings: 0
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
##編輯nginx.conf配置文件,去掉php,fastcgi相關的注釋,使其生效
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
2、fastcgi_params文件默認不能使用,需重新編輯,vim /etc/nginx/fastcgi_params,將其內容更改為如下內容:
[root@lnmp html]# vim /etc/nginx/fastcgi_params
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
##重新加載nginx配置
[root@lnmp html]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Reloading nginx: [ OK ]
[root@lnmp html]#
##構造php測試頁面
[root@lnmp html]# vim index.php
<?php
$conn=mysql_connect('localhost','root','redhat');
if ($conn)
echo "OK";
else
echo "false";
mysql_close();
echo "<br>";
echo "<br>";
phpinfo();
?>
##訪問測試
[root@lnmp html]# curl -I http://172.16.10.10/index.php
HTTP/1.1 200 OK
Server: nginx/1.10.1
Date: Tue, 01 Nov 2016 10:10:35 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/5.6.14
##通過curl訪問,返回200,正常
##至此,成功完成編譯nginx,php,mariadb搭建LNMP的配置環境。
原創文章,作者:馬哥Net19_小斌斌,如若轉載,請注明出處:http://www.www58058.com/56843