[Linux] PHP programmers play with the Linux series-lnmp environment, linux-lnmp
1. PHP programmers turn to the Linux series-how to install and use CentOS
In my normal work, I often build an environment as a PHP programmer. In this environment, I install nginx, php, and mysql in Linux and configure the software, then, you can open and run the project in the browser. There are several ways to install software in CentOS. I often use the package installation method, because this is a very simple command that can be successfully installed, this is also the biggest difference with installing software in windows. The key word for package installation is yum.
Solve the problem that the nginx package cannot be found
First, install nginx on the web server. After the software is installed, it can listen to port 80, bind a domain name, provide web Services, and access our project in the browser. When I run the command yum install nginx, the error "No package nginx available" is returned. This package does not exist. In this case, install the Enterprise Linux Extension package epel and run the command yum install epel-release, then try installing nginx again. At this time, you can install nginx and solve the dependency problem. Enable nginx with service nginx start
yum install epel-releaseyum install nginxservice nginx start
Solve the problem that web services cannot be accessed by ip addresses
In this case, I open the windows browser and access the address set in the previous article: http: // 192.168.2.100, which cannot be accessed. In this case, run the ps command to check whether the nginx process is enabled, and then run the netstat command to check whether port 80 is listening.
ps aux|grep nginxnetstat -altupn|grep 80
After checking the two commands, I found that they were both normal. It should have been blocked by the firewall. I disabled the firewall using service iptables stop. When I access the ip address, the nginx test page will appear, it is indeed a firewall problem. I want to open port 80 of the firewall. The open port configuration file of iptables is/etc/sysconfig/iptables, I copied the built-in 22 port rule and changed it to 80. After saving the rule, the service iptables start is normal.
Bind Domain Name
I want to access this site through the domain name, for example, I access the http://www.qingguow.cn, you can view the site directory on the server, this time it is necessary to configure nginx to bind the domain name. Enter the/etc/nginx/conf directory. d. Copy the built-in configuration file virtual. conf, changed to www.qingguow.cn. conf. Note that the suffix must be. conf, edit the configuration file, and restart nginx.
server { listen 80; server_name www.qingguow.cn; location / { root /usr/local/web; index index.html index.htm; }}
I created a new index.html file in the/usr/local/webdirectory. I wrote a sentence in it, modified the host file of windows, and pointed the domain name to the ip address of my virtual machine.
192.168.2.100 www.qingguow.cn
Then, I can directly retrieve the domain name to see the new index.html file, and the browser will prompt you That the domain name has been redirected.
Now I can check nginx access and error logs. The default value is/var/log/nginx/access. log. Use the tail-f command to view this log file in real time.
Tail-f/var/log/nginx/access. log, which helps me know that nginx is running normally at this time.
Install and configure PHP
I created a new index. php file under the directory just now and accessed the file directly. The download box will pop up in the browser, and the file will be downloaded directly. At this time, PHP will be installed.
Yum install php # Check the php-v version.
This version is still very old in PHP 5.3. I will update PHP later. At this time, PHP has been installed successfully, but it has not been matched with nginx. You also need to install php-fpm.
Yum install php-fpm # enable php-fpmservice php-fpm start
Now php-fpm is enabled and listening to port 9000 by default. Configure nginx to support PHP, edit www.qingguow.cn. conf, and change it to the following configuration file. Note the following points in the configuration file:
1. The root command mentioned the location command to solve the file not found error.
2. fastcgi_pass command, nginx connection fastcgi
3. fastcgi_param SCRIPT_FILENAME specifies this parameter, which also solves the file not found error.
4. When the file not found error occurs, the error. log of nginx is FastCGI sent in stderr: "Primary script unknown". You can find the solution by searching this log.
5. When an error occurs, check the error log and search for it.
Server {listen 80; server_name www.qingguow.cn; root/usr/local/web; location/{index index.html index.htm index. php;} location ~ \. Php {fastcgi_pass 127.0.0.1: 9000; # specify the FastCGI server listening port and address fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; # specify the script path include fastcgi_params; # load some parameters, this file already exists }}
Create an index. php file and use the phpinfo () function to print the php parameters. It is normal to access the file in the browser.
Install mysql database
Next, I will install the mysql database again, so that I can use PHP to connect to and use the mysql database. I want to install mysql and mysql-server. One is the client and the other is the server. By default, the mysql database is installed. The user name is root and the password is blank, which is not safe, of course, this database can only be connected locally and cannot be remotely connected. There is no problem, so we will try again later to solve the problem of Adding users and repairing passwords.
# Install the client yum install mysql # install the server yum install mysql-server # enable mysql service mysqld start # connect to the database mysql-hlocalhost-uroot-p
At this time, the lnmp environment is complete.