LNMP 环境部署

in PHPLinuxNginx with 0 comment

前言

最近新买了腾讯的2核4g服务器,之前一直在用阿里的,镜像没办法迁移共享,于是只能重新部署一个.

环境说明

安装 Nginx

安装依赖

Nginx的安装依赖于以下三个包,意思就是在安装Nginx之前首先必须安装一下的三个包,注意安装顺序如下:

修改安装源

$sudo rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

安装 nginx

$sudo yum install nginx

配置文件

安装完成后在 \etc\nginx ,配置文件也在这里

启动关闭

常用指令

service nginx restart // 重启
service nginx start   // 启动
service nginx stop    // 关闭
service nginx reload  // 重载
配置开机自启 chkconfig nginx on

安装 PHP 7.4

下载

$ wget https://www.php.net/distributions/php-7.4.8.tar.gz
tar -zxvf php-7.4.8.tar.gz

安装依赖

yum -y install libxml2-devel openssl-devel sqlite-devel libcurl-devel libicu-devel gcc-c++ openldap openldap-devel oniguruma oniguruma-devel m4 autoconf

编译安装

进入下载后解压的文件夹中

cd php-7.4.8
./configure --prefix=/mnt/php74/ \
 --with-config-file-path=/mnt/php72/etc \ 
 --enable-fpm \ 
 --with-fpm-user=www \ 
 --with-fpm-group=www \ 
 --enable-mysqlnd \ 
 --with-mysqli=mysqlnd \ 
 --with-pdo-mysql=mysqlnd \ 
 --enable-mysqlnd-compression-support \ 
 --with-iconv-dir \ 
 --with-zlib \ 
 --enable-xml \ 
 --disable-rpath \ 
 --enable-bcmath \ 
 --enable-shmop \ 
 --enable-sysvsem \ 
 --enable-inline-optimization \ 
 --with-curl=/usr/bin/curl \ 
 --enable-mbregex \ 
 --enable-mbstring \ 
 --enable-intl \ 
 --enable-ftp \ 
 --enable-gd-jis-conv \ 
 --with-openssl \ 
 --with-mhash \ 
 --enable-pcntl \ 
 --enable-sockets \ 
 --with-xmlrpc \ 
 --enable-soap \ 
 --with-gettext\ 
 --disable-fileinfo\ 
 --enable-opcache\ 
 --with-pear \ 
 --enable-maintainer-zts \ 
 --with-ldap=shared \ 
 --without-gdbm
## 执行安装
make && make install

### php cli

cd php74/bin
cp php /usr/bin/php
cp phpize /usr/bin/phpize

配置 php.ini 文件

一般安装完是没有 php.ini 文件的,源码包根目录下有提供两个版本的文件,分别是php.ini-developmentphp.ini-production

执行以下命令

cp /php-7.4.8/php.ini-development /php74/etc/php.ini

配置 php-fpm

在安装目录下的 etc 文件下会生成一个 php-fpm.conf.default 文件
执行下面命令

cp php-fpm.conf.default php-fpm.conf

编辑 php-fpm.conf 文件

vim php-fpm.conf
#开启pid ,去掉分号注释
pid = run/php-fpm.pid

/php74/etc/php-fpm.d 下有一个 www.conf.default

cd /php74/etc/php-fpm.d
cp www.conf.default www.conf

配置 service 服务

在 php 的源码包里面 php-7.4.8/sapi/fpm 文件夹下的 init.d.php-fpm
执行

// 添加到service
cp init.d.php-fpm /etc/init.d/php-fpm 
// 添加执行权限
chmod a+x /etc/init.d/php-fpm 
// 注册
chkconfig --add php-fpm 
// 开机自启
chkconfig php-fpm on 

启动关闭

#启动服务
service php-fpm start 
#停止服务
service php-fpm stop  
#重启服务
service php-fpm reload

安装 phpredis

下载 phpredis

wget https://github.com/phpredis/phpredis/archive/5.3.1.tar.gz

编译安装

tar zxvf 5.3.1.tar.gz
cd phpredis-5.3.1
phpize // 注意这里的phpize的位置,如果你没有将php74/bin/phpize添加到全局环境的话,需要执行 /php74/bin/phpize
./configure --with-php-config=/php74/bin/php-config
make && make install

配置

配置 php.ini

# 打开 /php74/etc/php.ini
vim /php74/etc/php.ini
# 
extension = redis

重启 nginxphp-fpm

service nginx restart

检查

php -m // 查看有没有 redis 模块

安装 composer

 php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
 php composer-setup.php
 cp composer.phar /usr/local/bin/composer

Redis 安装

下载

wget http://download.redis.io/releases/redis-5.0.5.tar.gz

解压

tar zxvf redis-5.0.5.tar.gz

编译安装

cd redis-5.0.5
make

make 完成之后安装

make install PREFIX=/redis5 // PREFIX 是指定安装目录

安装完成之后进入 /redis5/bin 执行 ./redis-server 就可以运行redis

bin 目录介绍

redis-cli // redis客户端
redis-server // redis 服务

添加 service 服务

redis 有提供相应的 sapi 生成脚本,在下载的源码包里 utils

进入该文件夹

cd /redis-5.0.5/utils
./install_server.sh // 执行后生成相应配置文件

Welcome to the redis service installer
This script will help you easily set up a running redis server

Please select the redis port for this instance: [6379]  // 配置端口
Selecting default: 6379                                                            
Please select the redis config file name [/etc/redis/6379.conf]  // redis配置文件
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]  // redis日志
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379] // redis数据文件
Selected default - /var/lib/redis/6379
Please select the redis executable path [/usr/local/redis/bin/redis-server] // redis服务
Selected config:
Port           : 6379
Config file    : et/etc/redis/6379.conf
Log file       : /var/log/redis_6379.log
Data dir       : /var/lib/redis/6379
Executable     : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.

按回车 enter 执行生成,成功之后会显示以下内容

Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!

redis 默认帮将服务添加入开机启动项,将服务管理脚本命名为 redis_6379

启动操作命令如下:

service redis_6379 status 查看端口号6379对应redis实例的运行状态
service redis_6379 stop 停止端口号6379对应redis实例服务
service redis_6379 start 启动端口号6379对应redis实例服务

额外说明

查看 serivce 开机启动项

chkconfig --list

疑难杂症

问题一

configure: error: Package requirements (libxml-2.0 >= 2.7.6) were not met:

No package 'libxml-2.0' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables LIBXML_CFLAGS
and LIBXML_LIBS to avoid the need to call pkg-config.
See the pkg-config man page for more details.
解决方案: yum install libxml2-devel

问题二

configure: error: Package requirements (openssl >= 1.0.1) were not met:

No package 'openssl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables OPENSSL_CFLAGS
and OPENSSL_LIBS to avoid the need to call pkg-config.
解决方案 yum install openssl-devel

问题三

configure: error: Package requirements (sqlite3 > 3.7.4) were not met:

No package 'sqlite3' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables SQLITE_CFLAGS
and SQLITE_LIBS to avoid the need to call pkg-config.
解决方案 yum install sqlite-devel

问题四

configure: error: Package requirements (libcurl >= 7.15.5) were not met:

No package 'libcurl' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables CURL_CFLAGS
and CURL_LIBS to avoid the need to call pkg-config.
解决方案 sudo yum install libcurl-devel

问题五

configure: error: Package requirements (icu-uc >= 50.1 icu-io icu-i18n) were not met:

No package 'icu-uc' found
No package 'icu-io' found
No package 'icu-i18n' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables ICU_CFLAGS
and ICU_LIBS to avoid the need to call pkg-config.
解决方案 yum install libicu-devel

问题六

checking whether we are using the GNU C++ compiler... no
checking whether g++ accepts -g... no
checking how to run the C++ preprocessor... /lib/cpp
configure: error: in `/mnt/php-7.4.8':
configure: error: C++ preprocessor "/lib/cpp" fails sanity check
解决方案 yum install gcc-c++

问题七

configure: error: Cannot find ldap.h
解决方案 yum install openldap openldap-devel

问题八

configure: error: Cannot find ldap libraries in /usr/lib
解决方案 cp -frp /usr/lib64/libldap* /usr/lib/

问题九

configure: error: Package requirements (oniguruma) were not met:

No package 'oniguruma' found

Consider adjusting the PKG_CONFIG_PATH environment variable if you
installed software in a non-standard prefix.

Alternatively, you may set the environment variables ONIG_CFLAGS
and ONIG_LIBS to avoid the need to call pkg-config.
解决方案
yum install oniguruma oniguruma-devel

问题十 安装 phpredis 报错

Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.
解决方案 yum install m4 && yum -y install autoconf
Responses