接上一篇文章 VMware 安装 CentOS 和 LNMP(1)
注:参考文章 阿里云 Centos 7 PHP7环境配置 LNMP
搭建LNMP
LNMP代表的就是:Linux系统下Nginx+MySQL+PHP这种网站服务器架构。
- 更新系统软件
[root@localhost ~]# yum update
- 安装 nginx
# 安装 nginx 源
[root@localhost ~]# yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# 安装 nginx
[root@localhost ~]# yum install nginx
# 启动成功则有以下输出
[root@localhost ~]# service nginx start
Redirecting to /bin/systemctl start nginx.service
也可以编译安装
# 安装 gcc 和 make
[root@localhost ~]# yum -y install gcc gcc-c++ kernel-devel make
# 安装 wget 工具
[root@localhost ~]# yum -y install wget
# 安装依赖包
[root@localhost ~]# yum -y install pcre pcre-devel zlib zlib-devel openssl openssl-devel
# 下载 nginx
[root@localhost ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz
# 解压
[root@localhost ~]# tar -zxvf nginx-1.14.0.tar.gz
[root@localhost ~]# cd nginx-1.14.0
# 写入配置文件
[root@localhost nginx-1.14.0]# ./configure --prefix=/usr/local/nginx
# 编译安装
[root@localhost nginx-1.14.0]# make && make install
# 启动
[root@localhost nginx-1.14.0]# /usr/local/nginx
成功之后在浏览器中访问虚拟机的 ip,可以看到 nginx 默认的欢迎页面
假如浏览器提示无法访问,则可能是防火墙原因,如下:
[root@localhost ~]# netstat -an|grep :80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
查看端口, 80 端口正在监听,执行以下命令
[root@localhost ~]# firewalld-cmd --state
running
返回结果表示防火墙确实在运行,尝试关闭防火墙
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# firewalld-cmd --state
not running
刷新浏览器,页面恢复正常显示
直接把防火墙关闭是一种不靠谱的做法,所以需要指定防火墙开放 80 端口,如下
[root@localhost ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent
- --add-port=80/tcp 指定端口和协议
- --permanent 指定永久性开放(否则重启失效)
再重启防火墙
[root@localhost ~]# systemctl restart firewalld
注:VMware 中 NAT 模式可以在本地环境中直接访问虚拟机 ip
- 安装 mysql
# 安装 mysql 源
[root@localhost ~]# yum localinstall http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
# 安装 mysql 和开发工具
[root@localhost ~]# yum install mysql-community-server mysql-community-devel
# 启动
[root@localhost ~]# service mysqld start
Redirecting to /bin/systemctl start mysqld.service
# 获取密码,密码为最后一段 f_MP2gt*VfUG
[root@localhost ~]# grep 'temporary password' /var/log/mysqld.log
2018-09-06T10:46:12.152948Z 1 [Note] A temporary password is generated for root@localhost: f_MP2gt*VfUG
使用之前获取的密码登录 MySQL root 用户并修改密码
# 密码必须有英文大小写数字及符号,并且保证长度
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourNewPass4!';
- 安装 php
# 安装 php 的依赖包
[root@localhost ~]# 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
# 下载 php 源码包
[root@localhost ~]# wget -O php7.tar.gz http://cn2.php.net/get/php-7.0.1.tar.gz/from/this/mirror
# 解压
[root@localhost ~]# tar -zxvf php7.tar.gz
[root@localhost ~]# cd php-7.0.1
# 写入配置,后面加的都是参数
[root@localhost php-7.0.1]# ./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
# 编译安装
[root@localhost php-7.0.1]# make && make install
要注意 ./configure
这一步可能会出现一些的错误, 如:
configure: error: Don't know how to define struct flock on this system, set --enable-opcache=no
解决办法是在执行 ./cnofigure
时把 --enable-opcache
修改为 --enable-opcache=no
configure: error: mcrypt.h not found. Please reinstall libmcrypt
解决办法是执行 yum -y install libmcrypt libmcrypy-devel
安装缺失的依赖包
3.如果出现第二个错误并且安装的时候出现以下错误
No package libmcrypt available.
No package libmcrypt-devel available
解决办法是直接下载源码包进行编译安装
[root@localhost ~]# wget https://sourceforge.net/projects/mcrypt/files/Libmcrypt/2.5.8/libmcrypt-2.5.8.tar.gz/download
[root@localhost ~]# tar -zxvf download
[root@localhost ~]# cd libmcrypt-2.5.8/
[root@localhost libmcrypt-2.5.8]# ./configure
[root@localhost libmcrypt-2.5.8]# make && make install
configure: error: C++ compiler cannot create executables
解决办法是执行 yum -y install gcc gcc-c++ kernel-devel
安装编译环境
/root/php-7.0.1/ext/zip/lib/zipint.h:126:2: error: #error unsupported size of off_t
#error unsupported size of off_t
^
make: *** [ext/zip/lib/zip_add.lo] Error 1
解决办法如下
# 创建 local.conf 文件
[root@localhost ~]# vi /etc/ld.so.conf.d/local.conf
# 写入以下路径并保存退出
/usr/local/lib
# 执行以下命令
[root@localhost ~]# ldconfig -v
# 之后再次执行 ./configure 命令即可
[root@localhost php-7.0.1]# ./configure ....
错误都会提示,一般只要根据提示操作就可以了
将 php 路径添加到环境变量
# /etc/profile
...
PATH=$PATH:/usr/local/php/bin
export PATH
# 激活配置
[root@localhost ~]# source /etc/profile
[root@localhost ~]# php -v
PHP 7.0.1 (cli) (built: Sep 7 2018 05:17:50) ( NTS )
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2015 Zend Technologies
- 配置 php-fpm
nginx 通过 php-fpm 来解释 php 代码
[root@localhost php-7.0.1]# cp php.ini-production /etc/php.ini
[root@localhost php-7.0.1]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@localhost php-7.0.1]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@localhost php-7.0.1]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@localhost php-7.0.1]# chmod +x /etc/init.d/php-fpm
# 启动
[root@localhost php-7.0.1]#/etc/init.d/php-fpm start
Starting php-fpm done
- 测试
修改 nginx 配置
[root@localhost ~]# vi /etc/nginx/conf.d/default.conf
...
#location ~ .php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /script$fastcgi_script_name;
# include fastcgi_params;
#}
# 找到该段代码,将注释去掉,并修改为以下
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
测试 php 脚本
[root@localhost ~]# vi /usr/share/nginx/html/index.php
<?php
echo phpinfo();
?>
在浏览器中访问 ip/index.php ,可以看到 浏览器中输出了 php 的信息
网友评论