您的位置:首页 > 运维架构 > Nginx

lnmp环境搭建之nginx 的安装配置和 php 的安装配置

2018-08-18 10:02 543 查看

1、利用CentOS Linux系统自带的yum命令安装、升级所需的程序库:

sudo -s
LANG=C
yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servers patch git libtool automake telnet cmake bison bind-utils jwhois setuptool ntsysv

2、下载所需要的源码包
http://nginx.org (nginx官网)
http://nginx.org/download/nginx-1.14.0.tar.gz (nginx 下载地址)
http://www.php.net (php官网)
http://cn2.php.net/get/php-7.2.8.tar.gz/from/this/mirror (php 下载地址)

2.1 下载nginx 源码包
nginx 的官方网站下载合适的稳定版本的 nginx 源码包,到 php 的官方网站下载合适的 php 源码包

创建软件下载的存放目录, 并使用 wget 下载所需的源码包

mkdir -p /data0/software
cd /data0/software

wget http://nginx.org/download/nginx-1.14.0.tar.gz
wget http://cn2.php.net/get/php-7.2.8.tar.gz/from/this/mirror

3、创建www用户和组

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

4、编译安装 nginx
这里 nginx 采用最简单的配置, 默认的安装目录为 /usr/local/nginx,请根据情况修改

cd /data0/softwate
tar -zxvf nginx-1.14.0.tar.gz
cd nginx-1.14.0

./configure --user=www --group=www --prefix=/usr/local/nginx  --with-http_ssl_module --with-http_sub_module --with-http_v2_module
//编译安装
make && make install

创建Nginx日志目录

mkdir -p /data1/logs
chmod +w /data1/logs
chown -R www:www /data1/logs

从新编写 nginx.conf 文件,内容如下

user  www www;
worker_processes  8;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        logs/nginx.pid;

events {
use epoll;
worker_connections 65535;
}

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

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

log_format  access '$remote_addr - $remote_user [$time_local] "$request" "$uri" $status $body_bytes_sent $request_time $upstream_response_time "$http_referer" "$http_user_agent" $http_x_forwarded_for "$server_name" "$http_host" "$cookie_userid" "$http_cookie" "$request_body"';

#gzip  on;

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

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

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 application/javascript;
gzip_vary on;

upstream myfastcgi {
server 127.0.0.1:9000 weight=2;
}

server {
listen       80;
server_name  localhost;

#charset koi8-r;

#access_log  logs/host.access.log  main;

location / {
root   html;
index  index.html index.htm;
}

#error_page  404              /404.html;

# redirect server error pages to the static page /50x.html
#
error_page   500 502 503 504  /50x.html;
location = /50x.html {
root   html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
#    proxy_pass   http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000

location ~ \.php$ {
root           html;
fastcgi_pass   127.0.0.1:9000;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include        fastcgi_params;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#    deny  all;
#}
}

# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

# HTTPS server
#
#server {
#    listen       443 ssl;
#    server_name  localhost;

#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_cache    shared:SSL:1m;
#    ssl_session_timeout  5m;

#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers  on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}

include vhost/*.conf;

}

在/usr/local/nginx/conf/目录中创建fcgi.conf文件:

vi /usr/local/nginx/conf/fcgi.conf

输入以下内容:

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

建立 nginx.service 文件

vi /usr/lib/systemd/system/nginx.service

在末尾增加以下内容:

[Unit]
Description=nginx
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target

启动nginx

ulimit -SHn 65535
systemctl start nginx.service
// 把 nginx 加入到开机启动项
systemctl enable nginx.service

5、编译安装 php

cd /data0/softwate
mv mirror php-7.2.8.tar.gz
tar -zxvf php-7.2.8.tar.gz
cd php-7.2.8

./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysqli=mysqlnd --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-zlib --with-libxml-dir --with-gd --with-iconv --with-jpeg-dir --with-png-dir --enable-sockets --enable-mbstring --enable-inline-optimization --enable-zend-multibyte --with-freetype-dir --with-openssl --with-curl --enable-opcache --enable-gd-native-ttf --enable-fpm
// 编译安装
make && make install
//复制源码包中的生产环境的 php.ini-production 到 php 配置文件目录下 命名为 php.ini 文件
cp php.ini-production /usr/local/php/etc/php.ini
// 建立并修改 www.conf 文件
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
vi /usr/local/php/etc/php-fpm.d/www.conf

把 www.conf 中的 user 和 group 修改为 安装 nginx 时使用的 user 和 group 的用户

user = www
group = www

创建php-fpm配置文件

cd /usr/local/php/etc/
mv php-fpm.conf.default php-fpm.conf
vi /usr/local/php/etc/php-fpm.conf

需要修改的地方如下

pid = run/php-fpm.pid
error_log = log/php-fpm.log
process_control_timeout = 5s
rlimit_files = 65535
rlimit_core = 0

拷贝 php-fpm.service 到 /usr/lib/systemd/

cp /data0/software/php-7.2.8/sapi/fpm/php-fpm.service /usr/lib/systemd/system/
ulimit -SHn 65535
systemctl start php-fpm.service
// 把 php-fpm.service 加入开机启动项
systemctl enable php-fpm.service

php-fpm重启命令

systemctl restart php-fpm.service

php-fpm关闭命令

systemctl stop php-fpm.service

附:
打开 php 报错需修改 php-fpm.conf

# 如果存在则修改成如下,如果不存在就在末尾添加
php_flag[display_errors] = on
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐