About Lemp
LEMP stack is a group of open source software to get web servers up and running. The acronym stands for Linux, nginx (pronounced Engine x), MySQL, and PHP. Since the server is already running CentOS, the linux part is taken care of. Here is how to install the rest.Step One—Open the Firewall
#/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #/sbin/iptables -I INPUT -p tcp --dport 22 -j ACCEPT #/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT #/etc/rc.d/init.d/iptables save
Step Two—Install the Required Repositories
We will be installing all of the required software with Yum. However, because nginx is not available straight from CentOS, we’ll need to install the epel repository.sudo yum install epel-release
Step Three—Install MySQL
The next step is to begin installing the server software on the virtual private server, starting with MySQL and dependancies.sudo yum install mysql-serverOnce the download is complete, restart MySQL:
sudo /etc/init.d/mysqld restartYou can do some configuration of MySQL with this command:
sudo /usr/bin/mysql_secure_installationThe prompt will ask you for your current root password. Since you just installed MySQL, you most likely won’t have one, so leave it blank by pressing enter.
Enter current password for root (enter for none): OK, successfully used password, moving on...Then the prompt will ask you if you want to set a root password. Go ahead and choose Y and follow the instructions. CentOS automates the process of setting up MySQL, asking you a series of yes or no questions. It’s easiest just to say Yes to all the options. At the end, MySQL will reload and implement the changes.
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] y ... Success! Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] y ... Success! By default, MySQL comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] y - Dropping test database... ... Success! - Removing privileges on test database... ... Success! Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MySQL installation should now be secure. Thanks for using MySQL!
Step Four—Install nginx
As with MySQL, we will install nginx on our virtual private server using yum:sudo yum install nginxnginx does not start on its own. To get nginx running, type:
sudo /etc/init.d/nginx startYou can confirm that nginx has installed on your virtual private server by directing your browser to your IP address. You can run the following command to reveal your server’s IP address.
ifconfig eth0 | grep inet | awk '{ print $2 }'
Step Five—Install PHP
The php-fpm package is located within the REMI repository, which, at this point, is disabled. The first thing we need to do is enable the REMI repository and install php and php-fpm:sudo yum install php yum install php-mysql php-gd libjpeg* php-imap php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-mcrypt php-bcmath php-mhash libmcrypt libmcrypt-devel php-fpm
Configure the PHP Processor
We now have our PHP components installed, but we need to make a slight configuration change to make our setup more secure. Open the main php-fpm configuration file with root privileges:- sudo vi /etc/php.ini
/etc/php.ini excerpt
cgi.fix_pathinfo=0
Save and close the file when you are finished.
Next, open the php-fpm configuration file www.conf
:
- sudo vi /etc/php-fpm.d/www.conf
listen
parameter, and change it so it looks like the following:
/etc/php-php.d/www.conf — 1 of 3
listen = /var/run/php-fpm/php-fpm.sock
Next, find the lines that set the listen.owner
and listen.group
and uncomment them. They should look like this:
/etc/php-php.d/www.conf — 2 of 3
listen.owner = nobody
listen.group = nobody
Lastly, find the lines that set the user
and group
and change their values from “apache” to “nginx”:
/etc/php-php.d/www.conf — 3 of 3
user = nginx
group = nginx
Then save and quit.
Now, we just need to start our PHP processor by typing:
- sudo systemctl start php-fpm
- sudo systemctl enable php-fpm
Step Six — Configure Nginx to Process PHP Pages
Now, we have all of the required components installed. The only configuration change we still need to do is tell Nginx to use our PHP processor for dynamic content. We do this on the server block level (server blocks are similar to Apache’s virtual hosts). Open the default Nginx server block configuration file by typing:- sudo vi /etc/nginx/conf.d/default.conf
/etc/nginx/conf.d/default.conf — original
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
We need to make some changes to this file for our site.
- First, we need to add an index.php option as the first value of our index directive to allow PHP index files to be served when a directory is requested
- We also need to modify the server_name directive to point to our server’s domain name or public IP address
- The actual configuration file includes some commented out lines that define error processing routines. We will uncomment those to include that functionality
- For the actual PHP processing, we will need to uncomment a portion of another section. We will also need to add a try_files directive to make sure Nginx doesn’t pass bad requests to our PHP processor
server_name
with the appropriate domain name or IP address:
/etc/nginx/conf.d/default.conf — updated
server {
listen 80;
server_name server_domain_name_or_IP;
# note that these lines are originally from the "location /" block
root /usr/share/nginx/html;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
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;
}
}
When you’ve made the above changes, you can save and close the file.
Restart Nginx to make the necessary changes:
- sudo systemctl restart nginx
Step Seven — Test PHP Processing on your Web Server
In order to test that our system is configured properly for PHP, we can create a very basic PHP script. We will call this scriptinfo.php
. In order for Apache to find the file and serve it correctly, it must be saved to a very specific directory, which is called the “web root”.
In CentOS 7, this directory is located at /usr/share/nginx/html/
. We can create the file at that location by typing:
- sudo vi /usr/share/nginx/html/info.php
Test PHP Script
<?php phpinfo(); ?>
When you are finished, save and close the file.
Now we can test whether our web server can correctly display content generated by a PHP script. To try this out, we just have to visit this page in our web browser. You’ll need your server’s public IP address again.
The address you want to visit will be:
Open in a web browser:
http://your_server_IP_address/info.php
The page that you come to should look something like this:

- sudo rm /usr/share/nginx/html/info.php
Step Eight—Set Up Autostart
You are almost done. The last step is to set all of the newly installed programs to automatically begin when the VPS boots.sudo chkconfig --levels 235 mysqld on sudo chkconfig --levels 235 nginx on sudo chkconfig --levels 235 php-fpm onYou are almost done. The last step is to set all of the newly installed
Conclusion
Now that you have a LEMP stack installed, you have many choices for what to do next. Basically, you’ve installed a platform that will allow you to install most kinds of websites and web software on your server.Other Codes:
chown nginx.nginx /usr/share/nginx/html/ -R #设置目录所有者
chmod 700 /usr/share/nginx/html/ -R #设置目录权限
Reference:
How To Install Linux, nginx, MySQL, PHP (LEMP) stack on CentOS 6 | DigitalOcean How To Install Linux, Nginx, MySQL, PHP (LEMP) stack On CentOS 7 | DigitalOcean CentOS 6.2 yum安装配置lnmp服务器(Nginx+PHP+MySQL) | 系统运维 CentOS 6.5系统中iptables防火墙开放端口80 3306 22端口 – Linux – 服务器之家Edit by Michael