CentOS 7安装LEMP stack(Linux, Nginx, MySQL, PHP)

首先安装

sudo yum install epel-release
sudo yum install yum-utils

Nginx

通常系统自带的是旧版本,安装新的版本需要手动设置yum repo

vim /etc/yum.repos.d/nginx.repo

写入以下内容:

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

yum安装Nginx

sudo yum install nginx

查看Nginx版本

nginx -V

启动Nginx

sudo systemctl start nginx

开机启动Nginx

sudo systemctl enable nginx

配置文件:/etc/nginx/nginx.conf

默认目录:/usr/share/nginx/html

官方参考资料:http://nginx.org/en/linux_packages.html#RHEL-CentOS

MySQL

添加 MySQL Yum Repository

MySQL Yum Repository 上找到对应的版本

sudo yum localinstall https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

您可以通过以下命令检查是否已成功添加MySQL Yum Repository 

yum repolist enabled | grep "mysql.*-community.*"

选择发行版本

yum repolist all | grep mysql

禁用MySQL5.7,启用MySQL5.6

sudo yum-config-manager --disable mysql57-community
sudo yum-config-manager --enable mysql56-community

禁用MySQL8.0,启用MySQL5.7

sudo yum-config-manager --disable mysql80-community
sudo yum-config-manager --enable mysql57-community

查看MySQL Yum Repository具体内容

vim /etc/yum.repos.d/mysql-community.repo
yum repolist enabled | grep mysql

yum安装MySQL

sudo yum install mysql-community-server

查看MySQL版本

mysql -V

启动MySQL

sudo systemctl start mysqld

查看MySQL运行状态

sudo systemctl status mysqld

开机启动MySQL

sudo systemctl enable mysqld

MySQL安全设置

mysql_secure_installation

查看MySQL临时密码

sudo grep 'temporary password' /var/log/mysqld.log

配置文件:/etc/my.cnf

默认数据目录:/var/lib/mysql

官方参考资料: https://dev.mysql.com/doc/mysql-repo-excerpt/8.0/en/linux-installation-yum-repo.html

PHP

添加第三方提供的 PHP Yum Repository

rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-7.rpm

选择版本

yum-config-manager --enable remi-php73

安装php-fpm

yum install php-fpm
yum install php-cli php-pear php-mysqlnd

安装php扩展

yum install php-gd php-mbstring php-mcrypt
php --modules

配置PHP

备份PHP默认配置文件

cp /etc/php.ini /etc/php.ini.default

修改配置

vim /etc/php.ini
cgi.fix_pathinfo=0

配置php-fpm

备份php-fpm默认配置文件

cp /etc/php-fpm.d/www.conf /etc/php-fpm.d/www.conf.default

修改配置

vim /etc/php-fpm.d/www.conf
listen = /var/run/php-fpm/php-fpm.sock
listen.owner = nginx
listen.group = nginx
user = nginx
group = nginx

启动PHP

sudo systemctl start php-fpm

开机启动PHP

sudo systemctl enable php-fpm

测试

vim /etc/nginx/conf.d/test.conf

写入以上内容:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    root   /usr/share/nginx/html/test;
    index  index.php index.html index.htm;

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        try_files $uri =404;
        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;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

重启Nginx

sudo systemctl restart nginx
赞(0)
未经允许不得转载:PHPez » CentOS 7安装LEMP stack(Linux, Nginx, MySQL, PHP)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址