月丶
centos配置nginx、php
2020-4-16 月丶


一、安装nginx



安装方式有两种,一种源码安装,一种yum安装。







源码安装>



1、下载源码,网址:http://nginx.org/en/download.html



wget http://nginx.org/download/nginx-1.16.1.tar.gz



若提示没有wget指令,yum wget  安装一下







2、解压压缩包



tar -zxvf nginx-1.16.1.tar.gz,解压目录/usr/local/src,位置自己选择







3、编译,安装



./configure --prefix=/usr/local/nginx --with-http_ssl_module



                                  安装目录               配置ssl需要,如不配置可不加




安装时可能会报错



configure: error: You need a C++ compiler for C++ support



解决:yum install -y gcc gcc-c++







./configure: error: the HTTP rewrite module requires the PCRE library.



解决:yum install -y pcre-devel openssl openssl-devel







最后两步



make



make install



源码安装到此安装完成。



之后可将nginx添加到service



/etc/init.d 目录下创建nginx文件,文件写入以下代码












#!/bin/bash


# nginx Startup script for the Nginx HTTP Server


# this script create it by caffreyxin at 2007.10.15.


# it is v.0.0.1 version.


# if you find any errors on this scripts, please contact caffreyxin.


# and send mail to xinyflove at sina dot com.


#


# chkconfig: - 85 15


# description: Nginx is a high-performance web and proxy server.


#              It has a lot of features, but it's not for everyone.


# processname: nginx


# pidfile: /var/run/nginx.pid


# config: /usr/local/nginx/conf/nginx.conf




nginxd=/usr/local/nginx/sbin/nginx                      #此行填入自己安装nginx目录


nginx_config=/usr/local/nginx/conf/nginx.conf           #此行填入配置文件目录


nginx_pid=/var/run/nginx.pid




RETVAL=0


prog="nginx"




# Source function library.


. /etc/rc.d/init.d/functions




# Source networking configuration.


. /etc/sysconfig/network




# Check that networking is up.


${NETWORKING} = "no" ] && exit 0




[ -x $nginxd ] || exit 0




# Start nginx daemons functions.


start() {




    if [ -e $nginx_pid ];then


        echo "nginx already running...."


        exit 1


    fi




    echo -n $"Starting $prog: "


    daemon $nginxd -c ${nginx_config}


    RETVAL=$?


    echo


    [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx


    return $RETVAL


}




# Stop nginx daemons functions.


stop() {


    echo -n $"Stopping $prog: "


    killproc $nginxd


    RETVAL=$?


    echo


    [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /var/run/nginx.pid


}




# reload nginx service functions.


reload() {




    echo -n $"Reloading $prog: "


    #kill -HUP `cat ${nginx_pid}`


    killproc $nginxd -HUP


    RETVAL=$?


    echo




}




# See how we were called.


case "$1" in


start)


        start


        ;;




stop)


        stop


        ;;




reload)


        reload


        ;;




restart)


        stop


        start


        ;;




status)


        status $prog


        RETVAL=$?


        ;;


*)


        echo $"Usage: $prog {start|stop|restart|reload|status|help}"


        exit 1


esac




exit $RETVAL













更改权限:chmod 777 nginx



添加nginx开机自启:chkconfig nginx on (此处是centos6+版本,cnetos7版本不一样自行查找)



此时应该可用 service nginx start  等命令操作nginx



设置nginx为全局指令:(软连接)ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/nginx











yum安装>



1、yum源配置:



# CentOS 6.5(亲测有效)



yum clean all







yum --disablerepo="epel" update nss













 # CentOS 7.x(未测试)

 yum install epel-release

 rpm -ivh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm



2、安装nginx



yum install -y nginx







添加开机自启



chkconfig nginx on



安装之后可查看版本:nginx -v



yum安装会自动添加到service无需手动添加







二、安装PHP



此处只写yum安装,源码安装自行查找







1、配置yum源




yum install epel-release








# Centos 5.X


rpm -Uvh http://mirror.webtatic.com/yum/el5/latest.rpm


# CentOs 6.x


rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm


# CentOs 7.X


rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm


rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm






若之前安装过php则需先删除:yum -y remove php*











2、安装扩展包(这里直接全部安装)



PHP7.1




yum install php71w php71w-mysql php71w-odbc php71w-common php71w-embedded php71w-pgsql php71w-xml php71w-ldap php71w-fpm php71w-gd php71w-pdo php71w-devel php71w-mbstring php71w-mcrypt php71w-cli php71w-json php71w-imap php71w-bcmath mod_php71w php71w-pear






PHP5.6



yum install php71w php71w-mysql php71w-odbc php71w-common php71w-embedded php71w-pgsql php71w-xml php71w-ldap php71w-fpm php71w-gd php71w-pdo php71w-devel php71w-mbstring php71w-mcrypt php71w-cli php71w-json php71w-imap php71w-bcmath mod_php71w php71w-pear











3、启动



开机自启



chkconfig php-fpm on







service php-fpm start






/etc/init.d/php-fpm start







Nginx配置SSL








    listen       443 ssl


    server_name  example.com;


    ssl_certificate /cert/3770136.com.pem;


    ssl_certificate_key /cert/3770136.com.key;


    ssl_session_timeout 5m;


    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;


    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;


    ssl_prefer_server_ciphers on;












nginx配置php




location ~ .*\.(php|php5)?$ {


        root   /usr/share/nginx/html/;


        fastcgi_pass 127.0.0.1:9000;


        fastcgi_index index.php;


        fastcgi_param HTTPS on;


        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;


        fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加这一句


        fastcgi_param PATH_INFO $fastcgi_path_info;    #增加这一句




        include fastcgi_params;


        #new line


        #include fastcgi.conf;


    }





发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容