Centos7安装nginx+php7运行环境

准备工作

yum update -y
yum install -y epel-release
# 如果找不到epel-release包,则进行下面两步操作
# wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm
# rpm -ivh epel-release-7-8.noarch.rpm

开始安装

  • 安装nginx
yum install -y nginx
# 配置开机启动
systemctl enable nginx.service
  • 防火墙开启80端口
 yum install -y firewalld firewall-config
 systemctl start firewalld.service
 firewall-cmd --permanent --add-service=http
 firewall-cmd --permanent --add-service=https #如果用不到https服务,此步跳过
 firewall-cmd --permanent --zone=trusted --add-port=80/tcp
 firewall-cmd --permanent --zone=trusted --add-port=443/tcp #如果用不到443端口,此步跳过
 firewall-cmd --reload
 firewall-cmd --permanent --zone=trusted --list-port
  • 安装必备的依赖
yum install -y \
gcc-c++ autoconf \
libjpeg libjpeg-devel libpng \
libpng-devel freetype freetype-devel \
libpng libpng-devel libxml2 libxml2-devel \
zlib zlib-devel glibc glibc-devel \
glib2 glib2-devel bzip2 bzip2-devel \
ncurses curl openssl-devel \
gdbm-devel db4-devel libXpm-devel \
libX11-devel gd-devel gmp-devel \
readline-devel libxslt-devel \
expat-devel xmlrpc-c xmlrpc-c-devel \
libicu-devel libmcrypt-devel \
libmemcached-devel \
curl-devel
  • 下载php7

官方下载地址 http://php.net/downloads.php

cd /usr/src/
wget http://nz2.php.net/distributions/php-7.1.5.tar.bz2 -O php7.bz2
tar -xjf php7.bz2
mv php-7.1.5 php7
cd /usr/src/php7

#注意,因为php7.1以后[]不支持字符串操作,凡是这种形式的$arr[]=(string)$someString; 的写法
#在php7.1版本都以后会报"[] operator not supported for strings" 的错误
  • 编译php
# 视情况增删参数,以下为一些常用的
./configure --prefix=/usr/local/php7 \
--with-mysql-sock --with-mysqli \
--enable-fpm --enable-soap \
--with-libxml-dir --with-openssl \
--with-mcrypt --with-mhash \
--with-pcre-regex --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-pdo-mysql \
--with-zlib-dir --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-intl --enable-pcntl
  • 安装php
make
make install
  • 测试是否安装成功
/usr/local/php7/bin/php -v
  • 做软链,以便直接用php运行
 ln -sf /usr/local/php7/bin/php /usr/local/bin/php
 ln -sf /usr/local/php7/bin/php-config /usr/bin/php-config
  • 验证
php -v
  • 复制配置文件
cp /usr/src/php7/php.ini-development /usr/local/php7/lib/php.ini
  • 配置php-fpm
cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf
cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf
cp /usr/src/php7/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm
  • 启动php-fpm
service php-fpm start
# 配置开机启动
chkconfig php-fpm on

相关配置

配置nginx

  • 修改nginx.conf文件
vi /etc/nginx/nginx.conf

注意:server 中需添加root 配置,否则$document_root无效,下面的是/etc/nginx/nginx.conf示例配置

# /etc/nginx/nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
worker_rlimit_nofile 65535;
events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    client_max_body_size 20m;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    #include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        location  ~ \.php(/|$) {
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }
    include vhosts/*.conf;
}
  • 重启nginx
service nginx restart
  • 在web目录下新建一个php文件
echo '<?php phpinfo();' > /usr/share/nginx/html/info.php

nginx虚拟机配置

以 www.test.com 为例

# 编辑虚拟机配置文件 vhosts若不存在则需创建
mkdir /etc/nginx/vhosts
vi /etc/nginx/vhosts/test.com.conf

输入以下内容

server {
        listen       80;
        server_name  www.test.com; # 网站域名
        root   "/usr/share/nginx/html/test";  #网站路径
        location / {
            index  index.html index.htm index.php;
            if (!-e $request_filename) { #地址重写规则,可在地址栏中隐藏index.php
                rewrite  ^(.*)$  /index.php?s=$1  last;
                break;
            }
            #autoindex  on;  # 自动索引
        }
        location ~ \.php(.*)$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            fastcgi_param  PATH_INFO  $fastcgi_path_info;
            fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
            include        fastcgi_params;
        }
}

安装php7扩展

  • 以memcached举例,下载php-memcached,从github上下载,记住一定要选择对应的php7分支
cd /usr/src
git clone https://github.com/php-memcached-dev/php-memcached.git
cd php-memcached/
git checkout php7
/usr/local/php7/bin/phpize
 ./configure --with-php-config=/usr/local/php7/bin/php-config
make
make install
  • 修改php.ini
vi /usr/local/php7/lib/php.ini
#在END前加上
extension=memcached.so
  • 保存并退出,重启php-fpm
service php-fpm restart
  • 再次访问info.php,就可以看到memcached已经安装成功了。

安装mysql

sudo yum -y install mariadb-server mariadb
systemctl start mariadb.service  #开启
systemctl enable mariadb.service #开机启动服务

# 查看
mysql --version

# 初始化
mysql_secure_installation
#第一次初始化时,密码为空,直接敲空格即可

#进入mysql控制台
mysql -u root -p
# 然后输入刚刚初始化时的密码

#创建可远程登陆的管理员账户(进行mysql命令行模式)
#(注意UserName和UserPassword要替换成想创建的用户名密码)
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY "root" WITH GRANT OPTION;

#创建可本地登陆的管理员账户(进行mysql命令行模式)
GRANT ALL PRIVILEGES ON *.* TO UserName@'localhost' IDENTIFIED BY "UserPassword" WITH GRANT OPTION;

#修改生效
FLUSH PRIVILEGES;

#重启mysql服务
systemctl restart mariadb.service

#远程连接注意防火墙设置

其它注意事项

如果php需要用socket方式连接mysql,可以进行如下操作

#注意php.ini文件中关于mysql_socket的设置
#查看mysql.sock地址
cat /etc/my.cnf
socket=/var/lib/mysql/mysql.sock # 看这行

#修改php.ini
pdo_mysql.default_socket= /var/lib/mysql/mysql.sock
mysqli.default_socket =/var/lib/mysql/mysql.sock

如有其它问题,请在下方留言^_^

已有 4 条评论
  1. xia xia

    按你的这种安装方式是不是只支持PDO连接MySQL数据库了?

    1. axios axios

      是 php7 不支持mysql扩展了,可以用mysqli

  2. xia xia

    PHP如何关联MySQL模块?

    1. axios axios

      在./configure编译时 ,有这样两个参数 --with-mysql-sock 和--with-mysqli ,就是关联mysql模块的

添加新评论