Featured image of post Quickly Set Up an LNMP Environment

Quickly Set Up an LNMP Environment

Quickly set up a PHP environment on a newly purchased server

Environment Specifications

  • Linux version: CentOS7
  • Nginx: Default version
  • MySQL: MariaDB
  • PHP: Version PHP71

Linux

  • Open required ports after server setup:
    • 80 for HTTP
    • 443 for HTTPS (optional)
    • 3306 for remote MySQL connections

Nginx

  • Install Nginx on CentOS:
    yum install nginx -y
    
  • Start Nginx:
    nginx
    
  • Enable auto-start:
    systemctl enable nginx
    
  • Configuration:
    • Default config file: /etc/nginx/nginx.conf
    • Add include /etc/nginx/conf.d/*.conf; to load custom configurations
    • Remove default server block and create new config:
    vi /etc/nginx/conf.d/blog.conf
    
  • Sample configuration:
    server {
        listen      80;
        server_name localhost;
        root        /var/www/blog;
        index       index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        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;
        }
    }
    
  • Reload Nginx:
    nginx -s reload
    

MySQL

  • Install MariaDB:
    yum -y install mariadb mariadb-server
    
  • Start service:
    systemctl start mariadb
    
  • Enable auto-start:
    systemctl enable mariadb
    
  • Initialize configuration:
    mysql_secure_installation
    
  • Create remote user:
    CREATE USER 'username'@'localhost' IDENTIFIED BY 'password';
    GRANT ALL PRIVILEGES ON *.* TO 'username'@'%' IDENTIFIED BY 'password';
    FLUSH PRIVILEGES;
    

PHP

  • Add repositories:
    rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
    rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
    
  • Install PHP71 and extensions:
    yum install php71w php71w-fpm php71w-mysqlnd
    
  • Start PHP-FPM:
    systemctl start php-fpm
    
  • Enable auto-start:
    systemctl enable php-fpm
    

Web Test

  • Create test file:
    mkdir -p /var/www/blog
    echo "<?php phpinfo();" > /var/www/blog/index.php
    
  • Visit server IP to verify PHP configuration

END