CentOS 7 下安裝 nginx + PHP 執行環境

大毛發表於2020-05-23

一、安裝 PHP

  1. 下載地址

  2. 安裝必要擴充套件包
    更新擴充套件包 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
  3. 編譯安裝 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
  1. 配置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
  1. 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

  1. 安裝必要外掛
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
  1. 配置 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;  
}  
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章