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

PHP 安装步骤

一、安装 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 协议》,转载必须注明作者和本文链接
讨论数量: 1

lnmp或者宝塔不好用么?

2天前 评论

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!