源码搭建 LNMP环境
LNMP环境:Linux + Nginx + Mysql + Php
1、安装 nginx:1.14.0
使用yum安装依赖:
yum -y install gcc gcc-c++ make libtool openssl openssl-devel zlib-devel pcre-devel
添加nginx的运行用户
useradd -r -s /sbin/nologin www
解压源码包,进行编译安装
tar -zxf nginx-1.14.0.tar.gz
cd nginx-1.14.0
./configure --prefix=/usr/local/nginx1.14 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_gzip_static_module --user=www --group=www && make && make install
做nginx命令的软连接,方便使用
ln -s /usr/local/nginx1.14/sbin/nginx /usr/local/sbin/
启动nginx
nginx -t
nginx
netstat -anpt | grep nginx
添加防火墙规则
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --reload
2、安装 mysql:5.7.22
使用打包好的脚本安装,默认用户root,默认密码123
二进制包:mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
sh mysql.sh
mysql -uroot -p123
3、安装 php:5.6.27
1)安装 libmcrypt依赖
tar zxf libmcrypt-2.5.7.tar.gz
cd libmcrypt-2.5.7/
./configure --prefix=/usr/local/libmcrypt && make && make install
2)安装 PHP
yum安装依赖
yum -y install libxml2-devel libcurl-devel openssl-devel bzip2-devel
解压源码包,进行编译安装
tar zxf php-5.6.27.tar.gz
cd php-5.6.27/
./configure --prefix=/usr/local/php5.6 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts && make && make install
3)创建 php-fpm 服务启动脚本:
拷贝php配置文件
cp php.ini-production /etc/php.ini
制作启动脚本
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
拷贝php-fpm配置文件,修改参数
cp /usr/local/php5.6/etc/php-fpm.conf.default /usr/local/php5.6/etc/php-fpm.conf
vi /usr/local/php5.6/etc/php-fpm.conf
*#修改以下内容*
pid = run/php-fpm.pid
user = www
group = www
listen =127.0.0.1:9000
pm.max_children = 300
pm.start_servers = 10
pm.min_spare_servers = 10
pm.max_spare_servers =50
service php-fpm restart
netstat -antpu | grep 9000
4)将nginx配置文件中添加php模块:
vim /usr/local/nginx1.14/conf/nginx.conf
*#修改以下内容*
location / {
root html;
index index.php index.html index.htm; #调整php优先级
}
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.conf;
}
重启nginx:nginx -s reload
4、测试
编辑php测试页面:
<?php phpinfo();?>
编辑数据库测试页面:
<?php
$link=mysql_connect('127.0.0.1','root','123');
if($link) echo "ok";
mysql_close();
?>