CentOS 6.4安装配置LNMP服务器(Nginx+PHP+MySQL)

  • Post category:Linux

CentOS6.4安装配置LNMP服务器(Nginx+PHP+MySQL)

步骤一:安装Nginx

  1. 安装epel-release和nginx:

    yum install epel-release -y
    yum install nginx -y

  2. 设置Nginx自启动并开启:

    chkconfig nginx on
    service nginx start

  3. 检查Nginx是否成功安装:

    nginx -v
    service nginx status

步骤二:安装MySQL

  1. 安装MariaDB数据库:

    yum install mariadb-server mariadb-client -y

  2. 设置MySQL自启动并开启:

    chkconfig --levels 235 mariadb on
    service mariadb start

  3. 进行MySQL安全配置:

    mysql_secure_installation

    根据提示输入密码并按需求进行配置。

步骤三:安装PHP

  1. 安装PHP及相关扩展:

    yum install php php-fpm php-mysql php-common php-gd php-mbstring php-mcrypt php-devel php-xml -y

  2. 设置PHP自启动并开启:

    systemctl enable php-fpm.service
    service php-fpm start

步骤四:配置Nginx

  1. 修改Nginx配置文件/etc/nginx/nginx.conf:

    “`
    user nginx;
    worker_processes auto;

    error_log /var/log/nginx/error.log warn;
    pid /var/run/nginx.pid;

    events {
    worker_connections 1024;
    }

    http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    
    access_log  /var/log/nginx/access.log  main;
    
    sendfile        on;
    keepalive_timeout  65;
    
    #gzip  on;
    
    include /etc/nginx/conf.d/*.conf;
    

    }
    “`

  2. 在/etc/nginx/conf.d目录下添加配置文件mywebsite.conf:

    “`
    server {
    listen 80;
    server_name mywebsite.com;
    root /usr/share/nginx/html/;
    index index.html index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    

    }
    “`

  3. 重新装载配置:

    nginx -s reload

示例一:安装一个WordPress网站

  1. 创建MySQL数据库和用户:

    “`
    mysql -u root -p

    CREATE DATABASE wp_mywebsite;
    GRANT ALL PRIVILEGES ON wp_mywebsite.* TO ‘wp_mywebsite_user’@’localhost’ IDENTIFIED BY ‘your_password’;
    FLUSH PRIVILEGES;
    “`

  2. 下载和解压WordPress至指定目录:

    cd /usr/share/nginx/html/
    wget https://wordpress.org/latest.tar.gz
    tar -xzvf latest.tar.gz

  3. 在浏览器中打开网站:http://mywebsite.com/,按照提示进行安装。在数据库配置中输入之前创建的数据库和用户信息。

示例二:安装一个Discuz!论坛

  1. 下载和解压Discuz!至指定目录:

    cd /usr/share/nginx/html/
    wget http://download.comsenz.com/DiscuzX/3.4/Discuz_X3.4_SC_UTF8.zip
    unzip Discuz_X3.4_SC_UTF8.zip

  2. 创建MySQL数据库和用户:

    “`
    mysql -u root -p

    CREATE DATABASE discuz_mywebsite;
    GRANT ALL PRIVILEGES ON discuz_mywebsite.* TO ‘discuz_mywebsite_user’@’localhost’ IDENTIFIED BY ‘your_password’;
    FLUSH PRIVILEGES;
    “`

  3. 在浏览器中打开网站:http://mywebsite.com/discuz/install,按照提示进行安装。在数据库配置中输入之前创建的数据库和用户信息。