首页 运维 nginx CentOS环境中怎么部署nginx、php和虚拟主机

CentOS环境中怎么部署nginx、php和虚拟主机

May 13, 2023 pm 12:40 PM
php centos nginx

os环境:centos 6.1
nginx:nginx-1.2.2
php:php5.3.14
0、安装依赖包

复制代码 代码如下:

yum install openssl-devel pcre-devel zlib-devel libjpeg-devel libpng-devel freetype-devel gcc make

1、添加 www 用户用来执行nginx

复制代码 代码如下:

useradd -m -r -s /sbin/nologin -d /opt/web/ www

2、创建临时目录

复制代码 代码如下:

mkdir -p /var/tmp/nginx/client/
mkdir -p /var/tmp/nginx/proxy/
mkdir -p /var/tmp/nginx/fcgi/

3、下载nginx最新稳定版源代码

复制代码 代码如下:

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.2.2.tar.gz

4、解压,编译,安装

复制代码 代码如下:

tar vxzf nginx-1.2.2.tar.gz
cd nginx-1.2.2/
./configure \
--prefix=/opt/web/nginx \
--error-log-path=/var/log/nginx/error.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=www \
--group=www \
--with-http_ssl_module \
--with-http_stub_status_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/ \
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi/
make
make install

5、配置nginx

复制代码 代码如下:

vim /opt/web/nginx/conf/nginx.conf
# 指定启动用户:
user www www;
# 进程数量,nginx作者认为一个就可以,根据自己的访问量修改
worker_processes 1;
# 设置错误日志:
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /var/log/nginx/error.default.log;
pid /opt/web/nginx/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
charset utf-8;
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;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
gzip on;
gzip_min_length 1000;
gzip_proxied any;
gzip_types text/plain text/css text/xml
application/x-javascript application/xml
application/atom+xml text/javascript;
server {
listen 80;
server_name localhost;
charset utf-8;
#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 /scripts$fastcgi_script_name;
#include fastcgi_params;
include fastcgi.conf;
}
# 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;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols sslv2 sslv3 tlsv1;
# ssl_ciphers high:!anull:!md5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
proxy_read_timeout 200;
# only retry if there was a communication error, not a timeout
# on the tornado server (to avoid propagating "queries of death"
# to all frontends)
proxy_next_upstream error;
proxy_set_header x-scheme $scheme;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header host $host;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
# 引入虚拟主机文件
include /opt/web/nginx/conf/sites/*.conf;
}

6、建立虚拟机配置文件存放的目录

复制代码 代码如下:

mkdir /opt/web/nginx/conf/sites

这样配置后,需要新增加虚拟主机的直接在 nginx/conf/sites/目录下,添加配置文件即可
例如:现在有 www.jb51.net 域名
建立:/opt/web/nginx/conf/sites/www.jb51.net.conf 文件
内容如下:

复制代码 代码如下:

server {
listen 80;
client_max_body_size 10m;
#多个域名用空格分割,第一个为默认
server_name www.jb51.net jb51.net;
charset utf-8;
index index.html index.htm index.php;
# 定义根目录
set $root /var/webroot/www.jb51.net/;
# 设置站点路径
root $root;
# 防止目录浏览
autoindex off;
if ($host != 'www.jb51.net') {
rewrite ^/(.*)$ //www.jb51.net/$1 permanent;
}
# 防止.htaccess文件被请求
location ~ /\.ht {
deny all;
}
error_page 404 /404.html;
index index.html index.htm;
location /uploads/ {
alias /data/webroot/www.jb51.net/uploads/;
}
try_files $uri @uwsgi;
location @uwsgi{
# 将其它的请求转交给uwsgi
include uwsgi_params;
uwsgi_pass unix:/tmp/360ito_uwsgi.sock;
proxy_set_header x-real-ip $remote_addr;
proxy_set_header host $host;
proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for;
#proxy_pass http://localhost:5000;
}
# 将php类型的请求转交给fastcgi
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
# 访问日志:
access_log /var/log/nginx/access.www.jb51.net.log;
# 加载.htaccess重写文件,注意,这里不支持变量路径
# 不能写成 include $root/www.jb51.net/.htaccess;
# include /var/webroot/www.jb51.net/.htaccess;
# 开启域名跳转,则当访问出错后,其他域名会自动跳转到 www.jb51.net
# 注意,这里我说的是,仅仅当访问出错后,才会跳转,所以,这里并不能实现301重定向!
server_name_in_redirect on;
}

7、安装最新版本php( php5.3.14 )

复制代码 代码如下:

cd /usr/local/src/
wget http://cn.php.net/get/php-5.3.14.tar.bz2/from/this/mirror
tar xjvf php-5.3.14.tar.bz2
cd php-5.3.14

执行:

复制代码 代码如下:

./buildconf --force

如果报错,可能是你的 autoconf不是 2.13 版本的,php5.3.系列的bug,需要安装 autoconf为2.13的版本:

复制代码 代码如下:

centos : # yum install autoconf213
debian : # apt-get install autoconf2.13

设置环境变量

复制代码 代码如下:

# centos :
export php_autoconf="/usr/bin/autoconf-2.13"
# debian :
export php_autoconf="/usr/bin/autoconf2.13"

再次运行:./buildconf --force ,出现 buildconf: autoconf version 2.13 (ok)
,则表示成功。
编译安装 php

复制代码 代码如下:

./configure \
--prefix=/opt/web/php \
--with-config-file-path=/opt/web/php/etc \
--with-config-file-scan-dir=/opt/web/php/etc/conf.d \
--enable-fpm \
--with-fpm-user=www \
--with-fpm-group=www \
--with-mysql=/opt/db/percona-server-5.5.14-rel20.5 \
--with-mysqli=/opt/db/percona-server-5.5.14-rel20.5/bin/mysql_config \
--with-iconv-dir \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir \
--enable-xml \
--enable-mbstring \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--enable-inline-optimization
make && make install
cp php.ini-production /opt/web/php/etc/php.ini
cd /opt/web/php/etc
cp php-fpm.conf.default php-fpm.conf

修改php-fpm.conf 启用如下几行,即去掉前面的分号(;)

复制代码 代码如下:

pid = run/php-fpm.pid
error_log = log/php-fpm.log
log_level = notice
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
listen.owner = www
listen.group = www
listen.mode = 0666
user = www
group = www
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
env[hostname] = $hostname
env[path] = /usr/local/bin:/usr/bin:/bin
env[tmp] = /tmp
env[tmpdir] = /tmp
env[temp] = /tmp

8、启动php-fpm

复制代码 代码如下:

/opt/web/php/sbin/php-fpm

启动nginx

复制代码 代码如下:

/opt/web/nginx/sbin/nginx

9、测试一下

复制代码 代码如下:

vim /var/webroot/www.jb51.net/tz.php

输入和保存

复制代码 代码如下:

phpinfo();
?>

10、在浏览器地址栏输入:http://php.jb51.net/tz.php
成功的话,可以看到phpinfo()输出的信息                                            

以上是CentOS环境中怎么部署nginx、php和虚拟主机的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PHP的目的:构建动态网站 PHP的目的:构建动态网站 Apr 15, 2025 am 12:18 AM

PHP用于构建动态网站,其核心功能包括:1.生成动态内容,通过与数据库对接实时生成网页;2.处理用户交互和表单提交,验证输入并响应操作;3.管理会话和用户认证,提供个性化体验;4.优化性能和遵循最佳实践,提升网站效率和安全性。

PHP和Python:代码示例和比较 PHP和Python:代码示例和比较 Apr 15, 2025 am 12:07 AM

PHP和Python各有优劣,选择取决于项目需求和个人偏好。1.PHP适合快速开发和维护大型Web应用。2.Python在数据科学和机器学习领域占据主导地位。

怎样优化CentOS HDFS配置 怎样优化CentOS HDFS配置 Apr 14, 2025 pm 07:15 PM

提升CentOS上HDFS性能:全方位优化指南优化CentOS上的HDFS(Hadoop分布式文件系统)需要综合考虑硬件、系统配置和网络设置等多个方面。本文提供一系列优化策略,助您提升HDFS性能。一、硬件升级与选型资源扩容:尽可能增加服务器的CPU、内存和存储容量。高性能硬件:采用高性能网卡和交换机,提升网络吞吐量。二、系统配置精调内核参数调整:修改/etc/sysctl.conf文件,优化TCP连接数、文件句柄数和内存管理等内核参数。例如,调整TCP连接状态和缓冲区大小

PHP:处理数据库和服务器端逻辑 PHP:处理数据库和服务器端逻辑 Apr 15, 2025 am 12:15 AM

PHP在数据库操作和服务器端逻辑处理中使用MySQLi和PDO扩展进行数据库交互,并通过会话管理等功能处理服务器端逻辑。1)使用MySQLi或PDO连接数据库,执行SQL查询。2)通过会话管理等功能处理HTTP请求和用户状态。3)使用事务确保数据库操作的原子性。4)防止SQL注入,使用异常处理和关闭连接来调试。5)通过索引和缓存优化性能,编写可读性高的代码并进行错误处理。

Centos停止维护2024 Centos停止维护2024 Apr 14, 2025 pm 08:39 PM

CentOS将于2024年停止维护,原因是其上游发行版RHEL 8已停止维护。该停更将影响CentOS 8系统,使其无法继续接收更新。用户应规划迁移,建议选项包括CentOS Stream、AlmaLinux和Rocky Linux,以保持系统安全和稳定。

centos关机命令行 centos关机命令行 Apr 14, 2025 pm 09:12 PM

CentOS 关机命令为 shutdown,语法为 shutdown [选项] 时间 [信息]。选项包括:-h 立即停止系统;-P 关机后关电源;-r 重新启动;-t 等待时间。时间可指定为立即 (now)、分钟数 ( minutes) 或特定时间 (hh:mm)。可添加信息在系统消息中显示。

PHP和Python:解释了不同的范例 PHP和Python:解释了不同的范例 Apr 18, 2025 am 12:26 AM

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

docker容器名称怎么查 docker容器名称怎么查 Apr 15, 2025 pm 12:21 PM

可以通过以下步骤查询 Docker 容器名称:列出所有容器(docker ps)。筛选容器列表(使用 grep 命令)。获取容器名称(位于 "NAMES" 列中)。

See all articles