针对CentOS7安装PHP及Nginx的教程,可以按照以下步骤进行操作:
安装Nginx- 更新系统软件包
yum update
- 安装EPEL源
yum install epel-release
- 安装Nginx
yum install nginx
- 启动Nginx服务,并设置开机启动
systemctl start nginx
systemctl enable nginx
- 安装PHP及相关扩展
yum install php php-fpm php-mysqlnd php-pdo php-pecl-memcached php-pgsql php-gd
- 配置php-fpm服务
systemctl start php-fpm
systemctl enable php-fpm
- 配置php-fpm的pool
打开/etc/php-fpm.d/www.conf并进行如下修改:
user = nginx
group = nginx
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
- 配置Nginx的虚拟主机
“`
server {
listen 80;
server_name example.com;
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
“`
- 重启Nginx
systemctl restart nginx
至此,CentOS7上Nginx及PHP安装并配置完成。可以在PHP-FPM的pool中设置不同的用户和组权限,来实现更严格的文件权限控制。
示例1:在Nginx上部署WordPress博客
- 安装WordPress及相关扩展
yum install php-mysqlnd php-gd php-xml
wget https://wordpress.org/latest.tar.gz
tar -xzvf latest.tar.gz
mv wordpress /usr/share/nginx/html/
- 配置WordPress的数据库
创建数据库并授权给一个新的用户:
mysql -u root -p
create database mywp;
grant all privileges on mywp.* to mywpuser@localhost identified by 'mypassword';
exit
修改WordPress配置文件wp-config.php以保证与数据库连接:
“`
/
* MySQL settings – You can get this info from your web host
*/
/ The name of the database for WordPress */
define(‘DB_NAME’, ‘mywp’);
/* MySQL database username /
define(‘DB_USER’, ‘mywpuser’);
/* MySQL database password /
define(‘DB_PASSWORD’, ‘mypassword’);
/* MySQL hostname /
define(‘DB_HOST’, ‘localhost’);
“`
- 访问博客前台
访问http://your_domain/wp-admin/install.php 进行WordPress的初始化配置,配置完成后即可正常访问博客前台。
示例2:在Nginx上部署Laravel应用
- 安装Laravel及相关扩展
yum install php-mbstring php-xml php-tokenizer php-zip
composer create-project --prefer-dist laravel/laravel myapp
mv myapp /usr/share/nginx/html/
- 配置Laravel的APP_KEY
php artisan key:generate
- 配置Nginx的虚拟主机
“`
server {
listen 80;
server_name myapp.example.com;
root /usr/share/nginx/html/myapp/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
“`
- 重启Nginx
systemctl restart nginx
现在,就可以访问http://myapp.example.com来访问Laravel应用了。