SoFunction
Updated on 2025-04-04

Deploy PHP environment and WordPress on Ubuntu 14.04

Software and version selection

Ubuntu 14.04

Ubuntu is currently one of the most user-oriented distributions, with the maintenance of the rich and powerful people behind it, which can be said to be the best choice for lightweight users. 14.04 is the latest LTS version, which has been released for half a year and is basically the best version currently supported.

Nginx

Nginx is a lightweight, flexible configuration and specializes in concurrent web server.

PHP-FPM

PHP-FPM is currently the best operating mode officially recommended.

MariaDB

A replacement for MySQL. After all, the founder of MySQL no longer recommends that we use MySQL.

Basic configuration

Usually when you create a VPS, you will get an IP and a root password, so first use ssh to log on to your server:

# If there is a warning, enter yes to confirm, and then enter your root password
Configure the public key login, which saves you from entering your password every time you log in. It is very recommended to upload the public key to a public address like me, so that you can set it with just one command:

mkdir ~/.ssh; curl '/jysperm/meta/master/Key/' >> ~/.ssh/authorized_keys; chmod -R 700 ~/.ssh;

Then update the package list and upgrade the existing package:

apt-get update
apt-get upgrade

Change the host name, and it is best to change it to a domain name that can indeed access this server:

vi /etc/hostname
vi /etc/hosts

Install the software package

apt-get install nginx postfix php5-fpm mariadb-server memcached
apt-get install php-pear php5-mysql php5-curl php5-gd php5-mcrypt php5-memcache
apt-get install python make screen git wget zip unzip iftop vim curl htop iptraf nethogs
nginx: Web server
postfix: SMTP server,Used to support sending mail from local
php5-fpm: PHP Process Manager,and PHP Interpreter
mariadb-server: kind MySQL database
memcached: Memory-based cache,Many programs will be used
php-pear: PHP Package Manager
php5-mysql: PHP MySQL databasedrive
php5-curl: one HTTP Protocol library
php5-gd: one图像处理库
php5-mcrypt: one加密算法库
php5-memcache: Memcached drive
python: one常用的脚本语言Interpreter
make: one常用的构建工具
screen: one常用的 Shell Session Management Tools
git: one常用的版本控制工具
wget, curl: Common file download tools
zip, unzip: ZIP Compression and decompression tools
iftop, iptraf, nethogs: Commonly used traffic monitoring tools
vim: one常用的编辑器
htop: one常用的进程监控工具

Install WordPress

Create a new normal user and switch to that user

adduser wordpress
su wordpress
cd ~

Download WordPress, please go to the official website to view the latest version of the download address:

wget /wordpress-3.9-zh_CN.zip

Unzip the file:

unzip wordpress-*.zip


Set file permissions:

chmod -R 750 wordpress


Delete the installation package:

rm wordpress-*.zip


Back to root:

exit


Configure PHP-FPM

Create a process pool for WordPress:

vi /etc/php5/fpm//


This is a very typical configuration file that provides services by listening to Unix Sockets, dynamically adjusts the number of processes, up to 10 processes, and at least 3 processes:

[wordpress]

user = wordpress
group = wordpress

listen = /home/wordpress/

 = wordpress
 = wordpress
 = 0660

pm = dynamic
pm.max_children = 10
pm.min_spare_servers = 3
pm.max_spare_servers = 5

slowlog = /home/wordpress/
request_slowlog_timeout = 5s
request_terminate_timeout = 15s

php_admin_value[error_log] = /home/wordpress/phpfpm_error.log
php_admin_flag[log_errors] = On

Configure Nginx

Delete Nginx's default site:

rm /etc/nginx/sites-enabled/default


Create a new site:

vi /etc/nginx/sites-enabled/wordpress

This configuration file has rewritten the request to, and you can use the "permanent link" function directly in WordPress:

server {
  listen 80;
  server_name ;

  root /home/wordpress/wordpress;
  index  ;
  autoindex off;

  location / {
    try_files $uri $uri/ /;
  } 

  location ~ \.php$ { 
    fastcgi_pass unix:///home/wordpress/;
    include fastcgi_params;
    fastcgi_index ;
  }
}

If you want to redirect all other domain names to your site, you can add this paragraph:

server {
  listen 80 default_server;
  listen [::]:80 default_server ipv6only=on;
  rewrite ^/(.*)$  permanent;
}


Then we need to fix a bug that cooperates with Nginx and PHP-FPM:

vi /etc/nginx/fastcgi_params


Change the line starting with fastcgi_param SCRIPT_FILENAME to:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;


Add permissions to read WordPress files for Nginx:

usermod -G wordpress -a www-data


Configure MySQL

Enter the MySQL console:

mysql -p


# You need to enter your MySQL root password

# Create a database

CREATE DATABASE `wordpress`;

# Create a new user for WordPress

CREATE USER 'wordpress'@'localhost' IDENTIFIED BY 'password';

# Grant permissions

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

# quit
QUIT
Restart

OK, the configuration has been completed, we can restart the server directly, so that all services will be restarted and the new configuration will be used:

reboot