编译安装LNMP全程实录

29 篇文章 0 订阅
22 篇文章 0 订阅

此次是在CentOs 7.4上进行安装配置,先把编译环境配置好:

	yum -y install gcc automake autoconf libtool make
	yum install gcc gcc-c++

准备一些软件的安装包:

安装包地址用途
php-7.1.6.tar.bz2http://cn2.php.net/distributions/php-7.1.6.tar.bz2后端语言
nginx-1.12.2.tar.gzhttp://nginx.org/download/nginx-1.12.2.tar.gz服务器
mysql-5.6.39-linux-glibc2.12-x86_64.tar.gzhttps://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz数据库
openssl-1.0.2n.tar.gzhttps://www.openssl.org/source/openssl-1.0.2n.tar.gznginx扩展,安全套接字层密码库
pcre-8.41.tar.gzftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.41.tar.gznginx扩展,支持rewrite功能
zlib-1.2.9.tar.gzhttp://www.zlib.net/fossils/zlib-1.2.9.tar.gznginx扩展,gzip压缩

将表中安装包使用tar命令解压,.tar.gz格式可使用-xf参数解压,.tar.bz2可使用-jxvf参数解压,如果tar.bz2解压报错:
tar: bzip2: Cannot exec: No such file or directory
说明没有安装bzip2,使用yum安装:
yum install bzip2

#现在进行nginx安装:
先安装扩展:
pcre

cd pcre-8.41 
./configure 
make
make install

zlib

cd zlib-1.2.9
./configure 
make 
make install

openssl

cd openssl-1.0.2n
./config 
make
make install

nginx
添加nginx使用的用户组:

groupadd nginx
useradd -g nginx nginx -s /bin/false

编译nginx(下一篇博文有详细编译参数解析):

	cd nginx-1.12.2
	./configure --prefix=/usr/local/nginx --without-http_memcached_module --user=nginx --group=nginx --with-http_gzip_static_module --with-openssl=/usr/local/src/openssl-1.0.2n --with-zlib=/usr/local/src/zlib-1.2.9  --with-pcre=/usr/local/src/pcre-8.41
make
make install

这样就安装好了,使用/usr/local/nginx/sbin/nginx即可启动nginx服务。
在浏览器中输入服务器地址出现“Welcome to nginx!”提示,即说明nginx正常运行:
这里写图片描述
接下来设置nginx开机启动
编辑启动文件:vi /etc/init.d/nginx
写入下面的内容:

#!/bin/bash

# nginx Startup script for the Nginx HTTP Server

# it is v.0.0.2 version.

# chkconfig: - 85 15

# description: Nginx is a high-performance web and proxy server.

#              It has a lot of features, but it's not for everyone.

# processname: nginx

# pidfile: /var/run/nginx.pid

# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/sbin/nginx

nginx_config=/usr/local/nginx/conf/nginx.conf

nginx_pid=/var/run/nginx.pid

RETVAL=0

prog="nginx"

# Source function library.

. /etc/rc.d/init.d/functions

# Source networking configuration.

. /etc/sysconfig/network

# Check that networking is up.

[ [${NETWORKING} = "no"] ] && exit 0

[ -x $nginxd ] || exit 0

# Start nginx daemons functions.

start() {

if [ -e $nginx_pid ];then

   echo "nginx already running...."

   exit 1

fi

   echo -n $"Starting $prog: "

   daemon $nginxd -c ${nginx_config}

   RETVAL=$?

   echo

   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx

   return $RETVAL

}

# Stop nginx daemons functions.

stop() {

        echo -n $"Stopping $prog: "

        killproc $nginxd

        RETVAL=$?

        echo

        [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid

}

# reload nginx service functions.

reload() {

    echo -n $"Reloading $prog: "

    #kill -HUP `cat ${nginx_pid}`

    killproc $nginxd -HUP

    RETVAL=$?

    echo

}

# See how we were called.

case "$1" in

start)

        start

        ;;

stop)

        stop

        ;;

reload)

        reload

        ;;

restart)

        stop

        start

        ;;

status)

        status $prog

        RETVAL=$?

        ;;

*)

        echo $"Usage: $prog {start|stop|restart|reload|status|help}"

        exit 1

esac

exit $RETVAL

赋予文件执行权限:
chmod 775 /etc/init.d/nginx
设置开机启动:
chkconfig nginx on
重启:
/etc/init.d/nginx restart
将nginx服务加入chkconfig管理列表:
chkconfig --add /etc/init.d/nginx

现在可以使用service nginx restart来重启服务了

#现在进行Mysql安装:
添加mysql使用的用户组:

groupadd mysql
useradd -g mysql mysql  -s /bin/false

将mysql目录移动到默认安装目录:
mv -R /usr/lcoal/src/mysql-5.6.39-linux-glibc2.12-x86_64 /usr/local/mysql
并修改权限:
chown -R mysql:mysql /usr/local/mysql
进入mysql目录:
cd /usr/local/mysql
执行安装脚本:
./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
复制mysql到服务自动启动里面
cp support-files/mysql.server /etc/init.d/mysqld
修改权限为755,增加执行权限:
chmod 755 /etc/init.d/mysqld
添加为开机自启服务:
chkconfig --add mysqld
设定mysqld在各等级为on:
chkconfig mysqld on
启动mysql:
service mysqld start
在执行安装脚本的步骤中,输出日志中有设置密码的命令提示:
这里写图片描述
执行./bin/mysqladmin -u root -h 主机名 password 'new-password'
再测试是否设置成功:./bin/mysql -uroot -p[new-password]
现在将mysql命令加入环境变量:
/etc/profile文件末尾添加一行:export PATH=$PATH:/usr/local/mysql/bin
使配置立刻生效:source /etc/profile
接下来登录mysql后执行:
grant all privileges on *.* to root@'%' identified by 'root';
flush privileges```使数据库可以远程连接。
最后把myslq的库文件链接到系统默认的位置:

ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock

重启mysql就好了:service mysqld restart

#最后安装PHP
依赖包太多,就yum安装吧:

yum install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel

编译

	./configure \
	--prefix=/usr/local/php \
	--with-config-file-path=/etc \
	--enable-fpm \
	--with-fpm-user=nginx \
	--with-fpm-group=nginx \
	--enable-inline-optimization \
	--disable-debug \
	--disable-rpath \
	--enable-shared \
	--enable-soap \
	--with-libxml-dir \
	--with-xmlrpc \
	--with-openssl \
	--with-mcrypt \
	--with-mhash \
	--with-pcre-regex \
	--with-sqlite3 \
	--with-zlib \
	--enable-bcmath \
	--with-iconv \
	--with-bz2 \
	--enable-calendar \
	--with-curl \
	--with-cdb \
	--enable-dom \
	--enable-exif \
	--enable-fileinfo \
	--enable-filter \
	--with-pcre-dir \
	--enable-ftp \
	--with-gd \
	--with-openssl-dir \
	--with-jpeg-dir \
	--with-png-dir \
	--with-zlib-dir \
	--with-freetype-dir \
	--enable-gd-native-ttf \
	--enable-gd-jis-conv \
	--with-gettext \
	--with-gmp \
	--with-mhash \
	--enable-json \
	--enable-mbstring \
	--enable-mbregex \
	--enable-mbregex-backtrack \
	--with-libmbfl \
	--with-onig \
	--enable-pdo \
	--with-mysqli=mysqlnd \
	--with-pdo-mysql=mysqlnd \
	--with-zlib-dir \
	--with-pdo-sqlite \
	--with-readline \
	--enable-session \
	--enable-shmop \
	--enable-simplexml \
	--enable-sockets \
	--enable-sysvmsg \
	--enable-sysvsem \
	--enable-sysvshm \
	--enable-wddx \
	--with-libxml-dir \
	--with-xsl \
	--enable-zip \
	--enable-mysqlnd-compression-support \
	--with-pear \
	--enable-opcache

如果报错:error: mcrypt.h not found,则依次执行下面两个yum安装


yum install -y epel-release
yum install -y libmcrypt-devel

安装
make && make install
配置环境变量:
/etc/profile末尾添加:
export PATH=$PATH:/usr/local/php/bin
使得改动立即生效:
source /etc/profile
配置php-fpm

cp php.ini-production /etc/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

启动php-fpm:
/etc/init.d/php-fpm start
添加为系统服务:

	chkconfig --add php-fpm
	chkconfig php-fpm on

重启php-fpm:

service php-fpm start

#配置nginx支持PHP:
vi /usr/local/nginx/conf/nginx.conf
取消下面这部分注释,并将fastcgi_param这一行改成下面代码提供的参数:

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;
}

重启nginx和php-fpm:
service nginx start
service php-fpm start

#测试
在nginx安装目录中的html文件夹中新建一个php文件:
vi index.php
输入:
<?php phpinfo();

然后再在浏览器中访问http://–ip–/index.php,正常输出如下:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

闲敲代码、落灯花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值