华为云服务器php安装与配置
时间: 2025-07-07 22:06:05 浏览: 9
### 华为云服务器 PHP 安装与配置教程
#### 选择合适的操作系统
为了顺利安装PHP,建议先确认所使用的华为云弹性云服务器的操作系统版本。通常情况下,Linux发行版如CentOS、Ubuntu等都是不错的选择[^1]。
#### 更新软件包列表
无论选用哪种Linux发行版,都应首先更新现有的软件包列表以确保获取最新的可用资源。对于基于Red Hat的系统(例如CentOS),可以执行如下命令来刷新缓存:
```bash
sudo yum update -y
```
而对于基于Debian的系统(比如Ubuntu),则应该使用apt-get工具来进行相同的操作:
```bash
sudo apt-get update && sudo apt-get upgrade -y
```
#### 安装必要的依赖项
在正式安装PHP之前,还需要准备一些辅助性的组件和服务。这一步骤同样取决于具体采用的是哪一种Linux内核衍生品:
- **针对CentOS**
```bash
sudo yum install epel-release httpd mariadb-server -y
```
- **面向Ubuntu**
```bash
sudo apt-get install apache2 mysql-server -y
```
上述指令不仅会下载并安装Apache Web Server以及MariaDB/MySQL数据库服务,还会自动处理它们之间的相互依存关系。
#### 下载并编译最新稳定版PHP源码
虽然大多数主流Linux发行商都会预置一定数量的PHP模块供快速部署之用,但有时可能需要更高级别的定制化选项或是特定的功能集。此时可以从官方GitHub仓库克隆目标分支或者直接从官方网站下载压缩包形式发布的二进制文件。这里仅给出基本示例:
```bash
cd /usr/local/src/
wget https://www.php.net/distributions/php-8.1.12.tar.gz # 版本号需根据实际情况调整
tar zxvf php-*.gz
cd php-*
./configure --prefix=/usr/local/php \
--with-config-file-path=/etc \
--enable-mbstring \
--disable-debug \
--disable-rpath \
--enable-inline-optimization \
--with-bz2 \
--with-curl \
--enable-ftp \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-zlib-dir \
--with-freetype-dir \
--without-xpm \
--with-webp-dir \
--with-tidy \
--enable-exif \
--enable-sysvsem \
--enable-sysvshm \
--enable-shmop \
--enable-wddx \
--with-libxml-dir=/usr \
--enable-soap \
--enable-sockets \
--enable-zip \
--with-iconv \
--with-imap \
--with-imap-ssl \
--with-kerberos \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--with-snmp \
--with-openssl \
--with-pspell \
--with-enchant \
--with-recode \
--with-xsl \
--with-zlib \
--enable-opcache=no \
--with-gettext=shared \
--enable-intl \
--with-password-argon2 \
--with-system-temp-dir=/tmp \
--with-unixODBC=shared,/usr \
--with-ldap=shared \
--with-ldap-sasl \
--with-db4=shared \
--with-qdbm=shared \
--with-litespeed \
--enable-phpdbg \
--with-tsrm-pthreads \
--enable-calendar \
--enable-dba=shared \
--with-flatfile=shared \
--with-inifile=shared \
--with-crack=shared \
--with-gmp=shared \
--with-interbase=shared \
--with-pgsql=shared \
--with-pdo-pgsql=shared,/usr \
--with-firebird=shared,/opt/firebird \
--with-oci8=shared,instantclient,/usr/lib/oracle/19.3/client64/lib \
--with-oraclepdb=shared,/u01/app/oracle/product/19c/dbhome_1 \
--with-vpx=shared,/usr \
--with-webp=shared,/usr \
--with-xpm=shared,/usr/X11R6 \
--with-zstd=shared,/usr \
--with-mailparse=shared,/usr \
--with-apxs2=/usr/sbin/apxs \
--with-pear \
--enable-cli \
--enable-cgi \
--enable-fpm \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
&& make clean all test install
```
注意:以上`./configure`参数仅为示范用途;实际环境中应当依据个人需求灵活增减相应开关。
#### 配置Web服务器支持PHP解析能力
为了让HTTP请求能够触发相应的PHP脚本解释过程,还需进一步修改Web server的相关设定。此处分别介绍两种常见的做法——即针对Apache HTTPD和Nginx反向代理的情况下的操作方法。
##### Apache HTTPD集成方案
编辑默认站点配置文件(一般位于/etc/httpd/conf.d目录下),加入以下几行内容以便开启mod_php7handler加载器的支持:
```apacheconf
<FilesMatch \.(php|phar)$>
SetHandler application/x-httpd-php
</FilesMatch>
AddType text/html .php
DirectoryIndex index.html index.htm index.php
```
重启httpd进程使改动生效即可:
```bash
systemctl restart httpd.service
```
##### Nginx FastCGI模式接入策略
相比之下,当选择了高性能轻量级webserver时,则要依靠fastcgi_pass机制将动态页面交给后端worker process去负责渲染工作。打开对应位置处的.conf模板,参照下面范例补充进去适当语句片段:
```nginx
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
fast
阅读全文
相关推荐

















