CentOS7搭建Nginx+PHP环境

添加用户及用户组

查看是否存在

[root@localhost ~]# id www
id: www: no such user

添加组

[root@localhost ~]# groupadd www

添加www用户

[root@localhost ~]# useradd -g www -s /sbin/nologin www

再查看一下

[root@localhost ~]# id www
uid=1001(www) gid=1000(www) 组=1000(www)

把/var/www目录的权限给www用户

[root@localhost ~]# chown -R www.www /var/www

Nginx编译安装

查看是否安装gcc(执行完命令什么都没出现即为没有安装) 否则安装

[root@localhost ~]# rpm -qa gcc

[root@localhost ~]# yum install gcc gcc-c++

安装其他依赖库

[root@localhost ~]# yum install perl perl-devel openssl openssl-devel pcre pcre-devel zlib zlib-devel

进入到 /usr/local 目录下

[root@localhost ~]# cd /usr/local

到nginx官网获取下载链接

[root@localhost local]# wget http://nginx.org/download/nginx-1.17.8.tar.gz

解压并进入文件夹

[root@localhost local]# tar -zxvf nginx-1.17.8.tar.gz
[root@localhost local]# cd nginx-1.17.8

安装前配置nginx

[root@localhost nginx-1.17.8]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module

如果该步出现error或者warring 一般为缺少相应库或者依赖,yum install 相应库或者依赖即可

注:http_ssl_module为https模块,http_v2_module为http2.0模块,需要现启用https才可用http2.0

安装nginx

[root@localhost nginx-1.17.8]# make && make install

进入到安装后的nginx目录下启动

[root@localhost nginx-1.17.8]# cd /usr/local/nginx
[root@localhost nginx]# ./sbin/nginx

开放80端口

[root@localhost nginx]# firewall-cmd --zone=public --add-port=80/tcp --permanent

PHP编译安装

到官方复制php安装包下载链接后使用wget下载并解压,然后进入目录

[root@localhost nginx]# cd /usr/local
[root@localhost local]# wget http://am1.php.net/distributions/php-7.2.12.tar.gz
[root@localhost local]# tar -zxvf php-7.2.12.tar.gz
[root@localhost local]# cd php-7.2.12

安装依赖

[root@localhost php-7.2.12]# 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

预编译

[root@localhost php-7.2.12]# ./configure --prefix=/usr/local/php --enable-fpm --with-openssl --with-curl --with-zlib-dir --with-png-dir --with-freetype-dir --with-jpeg-dir --with-gd --enable-gd-jis-conv --with-zlib  --enable-bcmath --enable-mbstring

如果此处出现error或warring,一般为缺少相应依赖,查找对应依赖并使用yum命令安装

编译并安装

[root@localhost php-7.2.12]# make && make install

复制一份php.ini-production 到/usr/local/php/etc 目录下 并命名为php.ini

[root@localhost php-7.2.12]# cp php.ini-production /usr/local/php/etc/php.ini

添加软链接到 /etc目录

[root@localhost php-7.2.12]# ln -s /usr/local/php/etc/php.ini  /etc/php.ini
[root@localhost php-7.2.12]# ls -l /etc/php.ini

拷贝php-fpm.conf.default 并重命名为php-fpm.conf

[root@localhost php-7.2.12]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf

添加软链接到 /etc目录

[root@localhost php-7.2.12]# ln -s /usr/local/php/etc/php-fpm.conf  /etc/php-fpm.conf

编辑php配置文件php-fpm.conf

[root@localhost php-7.2.12]# vim /usr/local/php/etc/php-fpm.conf

pid = run/php-fpm.pid //如果这个选项在配置文件存在就取消掉注释使用,如果不存在可以忽略,请勿手动添加,否则PHP启动会报错。

在文件最后添加,也可在修改/usr/local/php/etc/php-fpm.d/www.conf:

user = www
group = www

:wq! #保存并退出

设置php-fpm开机自启动

[root@localhost php-7.2.12]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm

赋予php-fpm执行权限

[root@localhost php-7.2.12]# chmod 755 /etc/init.d/php-fpm

设置php-fpm开机启动

[root@localhost php-7.2.12]# chkconfig php-fpm on
[root@localhost php-7.2.12]# chkconfig --list php-fpm
php-fpm        0:off   1:off   2:on    3:on    4:on    5:on    6:off

编辑php配置文件php.ini

[root@localhost php-7.2.12]# vim /usr/local/php/etc/php.ini
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix_getppid,posix_getpwnam,posix_getpwuid, posix_getrlimit,posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
date.timezone = PRC #设置时区
expose_php = Off #禁止显示php版本的信息
short_open_tag = On #支持php短标签
opcache.enable=1 #php支持opcode缓存
opcache.enable_cli=0
#在最后一行添加:
zend_extension=opcache.so #开启opcode缓存功能 (在文本按Shift+G就能跳到最后一行)

:wq! #保存并退出

配置nginx支持php

[root@localhost php-7.2.12]# vim /usr/local/nginx/conf/nginx.conf

#首行user去掉注释,修改Nginx运行组为www www;必须与/usr/local/php/etc/php-fpm.d/www.conf中的user,group配置相同,否则php运行出错

    location / {
        root           /var/www;
        index  index.html index.htm index.php;
    }
    
    location ~ \.php$ {
        root           /var/www;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

重启nginx服务

[root@localhost php-7.2.12]# ./usr/local/nginx/sbin/nginx -s reload

启动php服务

[root@localhost php-7.2.12]# /etc/init.d/php-fpm start

完成!欢迎访问我的网站

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值