介绍
以下是一个简单的Python脚本,用于在CentOS系统上使用Yum自动安装并配置LNMP(Linux、Nginx、MySQL、PHP)环境。
脚本实现
import os
# Step 1: Install EPEL repository
os.system('yum install -y epel-release')
# Step 2: Install Nginx
os.system('yum install -y nginx')
# Step 3: Start Nginx
os.system('systemctl start nginx')
os.system('systemctl enable nginx')
# Step 4: Install MySQL (MariaDB)
os.system('yum install -y mariadb-server mariadb')
os.system('systemctl start mariadb')
os.system('systemctl enable mariadb')
# Step 5: Install PHP
os.system('yum install -y php php-mysql php-fpm')
# Step 6: Configure PHP-FPM
os.system("sed -i 's/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g' /etc/php.ini")
os.system("sed -i 's/listen = 127.0.0.1:9000/listen = /var/run/php-fpm/php-fpm.sock/g' /etc/php-fpm.d/www.conf")
os.system("sed -i 's/;listen.owner = nobody/listen.owner = nginx/g' /etc/php-fpm.d/www.conf")
os.system("sed -i 's/;listen.group = nobody/listen.group = nginx/g' /etc/php-fpm.d/www.conf")
os.system("sed -i 's/user = apache/user = nginx/g' /etc/php-fpm.d/www.conf")
os.system("sed -i 's/group = apache/group = nginx/g' /etc/php-fpm.d/www.conf")
# Step 7: Start PHP-FPM
os.system('systemctl start php-fpm')
os.system('systemctl enable php-fpm')
# Step 8: Configure Nginx to use PHP Processor
nginx_conf = """
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.php index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
root /usr/share/nginx/html;
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;
}
}
"""
with open('/etc/nginx/conf.d/default.conf', 'w') as f:
f.write(nginx_conf)
# Step 9: Restart Nginx
os.system('systemctl restart nginx')
请注意,这个脚本需要在有root权限的系统上运行。
请注意,这个脚本只完成了基本的安装和配置工作,你可能需要根据自己的需求对其进行进一步的修改和配置。
注意:在生产环境中,你还需要考虑如何设置防火墙规则,如何配置MySQL的安全选项,如何处理PHP的错误报告,如何限制PHP的资源使用等等。