在Ubuntu 14.04上部署 PHP 环境及 WordPress
软件及版本选择
Ubuntu 14.04
Ubuntu 是目前用户数量数一数二的发行版,背后有大土豪维护,可以说是轻量级用户的最佳选择。而 14.04 是目前最新的 LTS 版本,目前已经发布了半年了,基本是目前支持最好的版本。
Nginx
Nginx 是一个轻量级的,配置灵活,擅长并发的 Web 服务器。
PHP-FPM
PHP-FPM 是目前官方推荐的最佳的运行模式。
MariaDB
MySQL 的替代品,毕竟目前 MySQL 的创始人已经不建议我们使用 MySQL 了。
基本配置
通常当你创建了一台 VPS, 你会得到一个 IP 和一个 root 密码,所以,先用 ssh 登上你的服务器:
ssh root@106.186.21.33
# 如果有警告输入 yes 来确认,然后输入你的 root 密码
配置一下公钥登录,省着每次登录都要输入密码,非常建议像我一样把公钥上传到一个公开的地址,这样只要一条命令就可以设置好:
mkdir ~/.ssh; curl 'https://raw.githubusercontent.com/jysperm/meta/master/Key/JyAir.pub' >> ~/.ssh/authorized_keys; chmod -R 700 ~/.ssh;
然后更新一下软件包列表,升级现有软件包:
apt-get updateapt-get upgrade
修改一下主机名,最好改成一个确实可以访问到这台服务器的域名:
vi /etc/hostnamevi /etc/hosts
安装软件包
apt-get install nginx postfix php5-fpm mariadb-server memcachedapt-get install php-pear php5-mysql php5-curl php5-gd php5-mcrypt php5-memcacheapt-get install python make screen git wget zip unzip iftop vim curl htop iptraf nethogsnginx: Web 服务器postfix: SMTP 服务器,用来支持从本地发送邮件php5-fpm: PHP 进程管理器,及 PHP 解释器mariadb-server: 类 MySQL 数据库memcached: 基于内存的缓存,很多程序会用到php-pear: PHP 的包管理器php5-mysql: PHP MySQL 数据库驱动php5-curl: 一个 HTTP 协议库php5-gd: 一个图像处理库php5-mcrypt: 一个加密算法库php5-memcache: Memcached 驱动python: 一个常用的脚本语言解释器make: 一个常用的构建工具screen: 一个常用的 Shell 会话管理工具git: 一个常用的版本控制工具wget, curl: 常用的文件下载工具zip, unzip: ZIP 压缩和解压工具iftop, iptraf, nethogs: 常用的流量监控工具vim: 一个常用的编辑器htop: 一个常用的进程监控工具
安装 WordPress
新建一个普通用户,并切换到该用户
adduser wordpresssu wordpresscd ~
下载 WordPress, 请自行到官网查看最新版本的下载地址:
wget http://cn.wordpress.org/wordpress-3.9-zh_CN.zip
解压文件:
unzip wordpress-*.zip
设置文件权限:
chmod -R 750 wordpress
删除安装包:
rm wordpress-*.zip
回到 root:
exit
配置 PHP-FPM
为 WordPress 创建一个进程池:
vi /etc/php5/fpm/pool.d/wordpress.conf
这是一份很典型的配置文件,通过监听 Unix Socket 来提供服务,动态调节进程数,最高 10 个进程,最低 3 个进程:
[wordpress]user = wordpressgroup = wordpresslisten = /home/wordpress/phpfpm.socklisten.owner = wordpresslisten.group = wordpresslisten.mode = 0660pm = dynamicpm.max_children = 10pm.min_spare_servers = 3pm.max_spare_servers = 5slowlog = /home/wordpress/phpfpm.slowlogrequest_slowlog_timeout = 5srequest_terminate_timeout = 15sphp_admin_value[error_log] = /home/wordpress/phpfpm_error.logphp_admin_flag[log_errors] = On
配置 Nginx
删掉 Nginx 的默认站点:
rm /etc/nginx/sites-enabled/default
新建一个站点:
vi /etc/nginx/sites-enabled/wordpress
这份配置文件已将请求重写到 index.php, 可以直接在 WordPress 中使用「固定链接」功能:
server { listen 80; server_name jysperm.me; root /home/wordpress/wordpress; index index.html index.php; autoindex off; location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { fastcgi_pass unix:///home/wordpress/phpfpm.sock; include fastcgi_params; fastcgi_index index.php; }}
如果你希望把其他所有域名都跳转到你的站点,可以添加这么一段:
server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; rewrite ^/(.*)$ http://jysperm.me permanent;}
然后我们需要修正 Nginx 和 PHP-FPM 配合的一个 Bug:
vi /etc/nginx/fastcgi_params
将 fastcgi_param SCRIPT_FILENAME 开头的行改为:
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
为 Nginx 添加读取 WordPress 文件的权限:
usermod -G wordpress -a www-data
配置 MySQL
进入 MySQL 控制台:
mysql -p
# 需要输入你的 MySQL root 密码
# 创建数据库
CREATE DATABASE `wordpress`;
# 为 WordPress 新建用户
CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'password';
# 授予权限
GRANT ALL PRIVILEGES ON `wordpress` . * TO 'wordpress'@'localhost';
# 退出
QUIT
重启
好了,已经配置完成了,我们直接重启服务器即可,这样所有服务都会重启并使用新的配置:
reboot

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
