CentOS 7 下安装 nginx + PHP 运行环境

PHP 安装步骤
一、安装 PHP
下载地址

安装必要扩展包
更新扩展包 yum install epel-release

yum install -y gcc gcc-c++ make cmake bison autoconf wget lrzsz \
yum install -y libtool libtool-ltdl-devel  \
yum install -y freetype-devel libjpeg.x86_64 libjpeg-devel libpng-devel gd-devel \
yum install -y python-devel  patch  sudo  \
yum install -y openssl* openssl openssl-devel ncurses-devel \
yum install -y bzip* bzip2 unzip zlib-devel \
yum install -y libevent* \
yum install -y libxml* libxml2-devel \
yum install -y libcurl* curl-devel  \
yum install -y readline-devel \
yum install -y libmcrypt libmcrypt-devel mcrypt mhash
编译安装 PHP

./configure \
--prefix=/usr/local/php7 \
--with-config-file-path=/usr/local/php7/etc \
--with-config-file-scan-dir=/usr/local/php7/etc/php.d \
--enable-mysqlnd \
--with-mysqli \
--with-pdo-mysql \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-gd \
--with-iconv \
--with-zlib \
--enable-xml \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-ftp \
--with-openssl \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--without-pear \
--with-gettext \
--enable-session \
--with-curl \
--with-jpeg-dir \
--with-freetype-dir \
--enable-opcache \
--enable-bcmath
make && make install
配置 PHP
vim /usr/local/php7/etc/php.ini
[php]
expose_php=off
max_file_uploads=30
post_max_size=30M
[opcache]
zend_extension=opcache.so
opcache.enable=1
opcache.memory_consumption=128
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
[date]
date.timezone=Asia/Shanghai
[session]
session.sid_length=40
vim /usr/local/php7/etc/php-fpm.cof
[global]
pid = /var/run/php-fpm.pid
error_log = log/php-fpm.log
daemonize = yes
include=/usr/local/php7/etc/php-fpm.d/*.conf
vim /usr/local/php7/etc/php-fpm.d/www.conf
[www]
user = www
group = www
listen = /dev/shm/php-cgi.sock
listen.owner = www
listen.group = www
listen.mode = 0660
listen.allowed_clients = 127.0.0.1
pm = static
pm.max_children = 内存/20M
pm.start_servers = 30
pm.min_spare_servers = 20
pm.max_spare_servers = 40
pm.max_requests = 500
request_terminate_timeout = 300
php-fpm 加入系统服务
vim /usr/lib/systemd/system/php-fpm.service
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/var/run/php-fpm.pid
ExecStart=/usr/local/php7/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
systemctl daemon-reload

常规操作命令

systemctl (start|stop|restart|status) php-fpm.service

二、安装 nginx
下载地址
安装必要插件
yum -y install gcc gcc-c++ autoconf automake \
yum -y install zlib zlib-devel openssl openssl-devel pcre-devel \
yum -y install geoip-devel
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/usr/local/nginx/config/nginx.conf \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module \
--http-log-path=/var/log/nginx/access.log \
--http-client-body-temp-path=/var/tmp/nginx/client \
--http-proxy-temp-path=/var/tmp/nginx/proxy \
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
--with-http_stub_status_module \
--with-http_geoip_module
配置 nginx
nginx.conf

user  www www;
worker_processes  4; # cpu 核数
error_log  /home/wwwlogs/nginx_error.log  error;
pid        /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 65535;
events
        {
                use epoll;
                worker_connections 65535;
        }

http
        {
                include       mime.types;
                default_type  application/octet-stream;

                server_names_hash_bucket_size 128;
                client_header_buffer_size 32k;
                large_client_header_buffers 4 32k;
                client_max_body_size 50m;

                sendfile on;
                tcp_nopush     on;

                keepalive_timeout 60;

                tcp_nodelay on;

                fastcgi_connect_timeout 600;
                fastcgi_send_timeout 600;
                fastcgi_read_timeout 600;
                fastcgi_buffer_size 64k;
                fastcgi_buffers 4 64k;
                fastcgi_busy_buffers_size 128k;
                fastcgi_temp_file_write_size 256k;

                gzip on;
                gzip_min_length  1k;
                gzip_buffers     4 16k;
                gzip_http_version 1.0;
                gzip_comp_level 2;
                gzip_types       text/plain application/x-javascript text/css application/xml;
                gzip_vary on;
                gzip_proxied        expired no-cache no-store private auth;
                gzip_disable        "MSIE [1-6]\.";

                server_tokens off;
                log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
          include vhost/*.conf;
}
vhost 目录下加入站点配置

server {
    listen 80;
    server_name xxx.com;
    access_log  /alidata/log/nginx/api.log combined;
    index index.html index.htm index.php;
    root /home/www/api;
    if ( $query_string ~* ".*[\;'\<\>].*" ){
            return 404;
    }
    location / {
                try_files $uri $uri/ /index.php;
    }
    location ~ .*\.(php|php5)?$  {
            fastcgi_pass unix:/dev/shm/php-cgi.sock;
            fastcgi_index index.php;
            include fastcgi.conf;
    }

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
            expires 30d;
    }

    location ~ .*\.(js|css)?$ {
            expires 7d;
    }
}
vim /usr/lib/systemd/system/nginx.service

[Unit]
Description=Nginx HTTP server
After=local-fs.target php-fpm.target

[Service]
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -HUP $MAINPID
Type=forking

[Install]
WantedBy=multi-user.target
###
nginx 强制使用 https 访问 (http 跳转到 https)

server {  
    listen  192.168.1.111:80;  
    server_name test.com; 
    rewrite ^(.*)$  https://$host$1 permanent;  
}  

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值