源码编译安装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.178linux.com/56843