Home Backend Development PHP Tutorial 在Ubuntu 14.04上部署 PHP 环境及 WordPress

在Ubuntu 14.04上部署 PHP 环境及 WordPress

Jun 23, 2016 pm 01:43 PM

软件及版本选择

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
Copy after login

# 如果有警告输入 yes 来确认,然后输入你的 root 密码
配置一下公钥登录,省着每次登录都要输入密码,非常建议像我一样把公钥上传到一个公开的地址,这样只要一条命令就可以设置好:

mkdir ~/.ssh; curl 'https://raw.githubusercontent.com/jysperm/meta/master/Key/JyAir.pub' >> ~/.ssh/authorized_keys; chmod -R 700 ~/.ssh;
Copy after login

然后更新一下软件包列表,升级现有软件包:

apt-get updateapt-get upgrade
Copy after login

修改一下主机名,最好改成一个确实可以访问到这台服务器的域名:

vi /etc/hostnamevi /etc/hosts
Copy after login

安装软件包

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: 一个常用的进程监控工具
Copy after login

安装 WordPress

新建一个普通用户,并切换到该用户

adduser wordpresssu wordpresscd ~
Copy after login

下载 WordPress, 请自行到官网查看最新版本的下载地址:

wget http://cn.wordpress.org/wordpress-3.9-zh_CN.zip
Copy after login

解压文件:

unzip wordpress-*.zip
Copy after login


设置文件权限:

chmod -R 750 wordpress
Copy after login


删除安装包:

rm wordpress-*.zip
Copy after login


回到 root:

exit
Copy after login


配置 PHP-FPM

为 WordPress 创建一个进程池:

vi /etc/php5/fpm/pool.d/wordpress.conf
Copy after login


这是一份很典型的配置文件,通过监听 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
Copy after login

配置 Nginx

删掉 Nginx 的默认站点:

rm /etc/nginx/sites-enabled/default
Copy after login


新建一个站点:

vi /etc/nginx/sites-enabled/wordpress
Copy after login

这份配置文件已将请求重写到 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;  }}
Copy after login

如果你希望把其他所有域名都跳转到你的站点,可以添加这么一段:

server {  listen 80 default_server;  listen [::]:80 default_server ipv6only=on;  rewrite ^/(.*)$ http://jysperm.me permanent;}
Copy after login


然后我们需要修正 Nginx 和 PHP-FPM 配合的一个 Bug:

vi /etc/nginx/fastcgi_params
Copy after login


将 fastcgi_param SCRIPT_FILENAME 开头的行改为:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Copy after login


为 Nginx 添加读取 WordPress 文件的权限:

usermod -G wordpress -a www-data
Copy after login


配置 MySQL

进入 MySQL 控制台:

mysql -p
Copy after login


# 需要输入你的 MySQL root 密码

# 创建数据库

CREATE DATABASE `wordpress`;
Copy after login

# 为 WordPress 新建用户

CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'password';
Copy after login

# 授予权限

GRANT ALL PRIVILEGES ON `wordpress` . * TO 'wordpress'@'localhost';
Copy after login

# 退出
QUIT
重启

好了,已经配置完成了,我们直接重启服务器即可,这样所有服务都会重启并使用新的配置:

reboot


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

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.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

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? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

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 permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

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...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

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�...

See all articles