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

完成!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章